我正在尝试在Ubuntu上使用daemon
,但即使在阅读了手册页后我也不确定如何使用它。
我有以下测试脚本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
未更新,我在errlog中找到了这个:
20161221 12:12:36 foo: client (pid 176193) exited with 1 status
我如何正确使用daemon
命令?
答案 0 :(得分:1)
AFAIK,大多数daemon
或deamonize
程序将当前目录更改为root,作为守护进程的一部分。这意味着您必须提供命令的完整路径:
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
答案 1 :(得分:0)
没有必要在bash中使用守护进程命令。您可以手动守护脚本。例如:
#!/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