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

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

我有一个脚本,每 X 次运行我的 PHP 脚本:

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

发布评论

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

>www.elefans.com

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