将bash脚本作为守护进程运行

编程入门 行业动态 更新时间:2024-10-06 04:10:13
本文介绍了将bash脚本作为守护进程运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个脚本,每次运行我的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脚本作为守护进程运行

本文发布于:2023-11-23 22:01:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   进程   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!