将bash脚本作为守护进程运行 [英] Run bash script as daemon
问题描述
我有一个脚本,每次运行我的PHP脚本X次:
I have a script, which runs my PHP script each X times:
#!/bin/bash
while true; do
/usr/bin/php -f ./my-script.php
echo "Waiting..."
sleep 3
done
如何以守护进程启动它?
How can I start it as daemon?
推荐答案
要从shell运行它作为完整的守护进程,你需要使用 setsid
并重定向它的输出。您可以将输出重定向到日志文件,或 / dev / null
以将其丢弃。假设您的脚本被称为myscript.sh,使用以下命令:
To run it as a full daemon from a shell, you'll need to use setsid
and redirect its output. You can redirect the output to a logfile, or to /dev/null
to discard it. Assuming your script is called myscript.sh, use the following command:
setsid myscript.sh >/dev/null 2>&1 < /dev/null &
这将完全从当前shell(stdin,stdout和stderr)中分离进程。如果要将输出保存在日志文件中,请将第一个 / dev / null
替换为/ path / to / logfile。
This will completely detach the process from your current shell (stdin, stdout and stderr). If you want to keep the output in a logfile, replace the first /dev/null
with your /path/to/logfile.
您必须重定向输出,否则它不会作为真正的守护进程运行(它将取决于您的shell读写输出)。
You have to redirect the output, otherwise it will not run as a true daemon (it will depend on your shell to read and write output).
这篇关于将bash脚本作为守护进程运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!