linux - 如果在后台运行,从脚本调用子 shell 不起作用

标签 linux bash

我有一个脚本在后台运行,比如:

su - insite1 -c "invokeTest.sh" &

现在这个脚本,invokeTest.sh 有内容:

while [ 1 ]
do
  echo "Hello World from invokeTest" >> /tmp/invokeTest
  ( exec "/tmp/test.sh" )
done

虽然它会打印“Hello World from invokeTest”,但它不会调用 test.sh。

我的问题: 有什么方法可以直接从 su - insite1 -c"" 无限循环调用 test.sh,这样我们就可以完全跳过 invokeTest.sh 本身(但 test. sh 需要在后台运行"?

如果不是,如何确保从 invokeTest.sh 调用 test.sh?

最佳答案

要回答关于在没有脚本的情况下运行命令的问题,您可以将任何 shell 命令放入 -c 参数中,包括 while 循环。所以你可以这样写:

su - insite1 -c 'while :; do echo "Hello World" >> /tmp/invokeTest; /tmp/test.sh; done' &

关于linux - 如果在后台运行,从脚本调用子 shell 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269583/

相关文章:

node.js - 如何阻止 Jenkins 在每次构建后重置用户权限?

linux - 如何在固定时间内保持 linux 机器关闭

c++ - 使用字符串输入和输出运行进程

linux - 抓取屏幕输出并解析为 csv 的 bash 脚本

linux - bash shell : How can I know if TeamViewer has disconnected?

C++ fork() 在 linux 终端上看起来很奇怪

linux - 如何在 go 中正确实现挂载命名空间

Git可以 pull 好但不能推送 "could not read username"

linux - 仅复制可执行文件(跨平台)

linux - Perl匿名管道没有输出