Linux 如何对脚本进行后台监控
我试图在Ubuntu上使用Linux 如何对脚本进行后台监控,linux,daemon,Linux,Daemon,我试图在Ubuntu上使用守护进程,但即使在阅读手册之后,我也不知道如何使用它 我有以下测试脚本foo.sh #!/bin/bash while true; do echo 'hi' >> ~/hihihi sleep 10 done 然后我尝试了这个命令,但什么也没发生: daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- foo.sh 文件hihihi未更新,我在错误日志中发现: 20161221
守护进程,但即使在阅读手册之后,我也不知道如何使用它
我有以下测试脚本foo.sh
#!/bin/bash
while true; do
echo 'hi' >> ~/hihihi
sleep 10
done
然后我尝试了这个命令,但什么也没发生:
daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- foo.sh
文件hihihi
未更新,我在错误日志中发现:
20161221 12:12:36 foo: client (pid 176193) exited with 1 status
如何正确使用daemon
命令?在bash中不必使用daemon命令。您可以手动对脚本进行后台监控。例如:
#!/bin/bash
# At first you have to redirect stdout and stderr to /dev/null
exec >/dev/null
exec 2>/dev/null
# Fork and go to background
(
while true; do
echo 'hi' >> ~/hihihi
sleep 10
done
)&
# Parent process finished but child still working
好的,大多数守护进程
或解除监控
程序将当前目录更改为根目录,作为守护进程的一部分。这意味着您必须给出命令的完整路径:
daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- /path/to/foo.sh
如果仍不起作用,可以尝试指定一个shell:
daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- /bin/bash -c /path/to/foo.sh
看看诺胡普。nohup script.sh&非常高效,simpleI无法访问Ubuntu系统进行测试,但您可以尝试启动一个shell来解释foo.sh
:守护进程/bin/sh~/foo.sh