Linux 将 bash 脚本作为守护进程运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19233529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run bash script as daemon
提问by Sergey B.
I have a script, which runs my PHP script each X times:
我有一个脚本,它每 X 次运行我的 PHP 脚本:
#!/bin/bash
while true; do
/usr/bin/php -f ./my-script.php
echo "Waiting..."
sleep 3
done
How can I start it as daemon?
我怎样才能将它作为守护进程启动?
采纳答案by micromoses
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:
要从 shell 作为完整守护程序运行它,您需要使用setsid
并重定向其输出。您可以将输出重定向到日志文件,或将/dev/null
其丢弃。假设您的脚本名为 myscript.sh,请使用以下命令:
setsid myscript.sh >/dev/null 2>&1 < /dev/null &
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(stdin、stdout 和 stderr)中分离进程。如果要将输出保留在日志文件中,请将第一个替换为/dev/null
您的 /path/to/logfile。
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).
您必须重定向输出,否则它将不会作为真正的守护进程运行(它将取决于您的 shell 来读取和写入输出)。
回答by Refael
You can go to /etc/init.d/ - you will see a daemon template called skeleton.
您可以转到 /etc/init.d/ - 您将看到一个名为 skeleton 的守护程序模板。
You can duplicate it and then enter your script under the start function.
您可以复制它,然后在 start 函数下输入您的脚本。
回答by CMP
A Daemon is just program that runs as a background process, rather than being under the direct control of an interactive user...
守护进程只是作为后台进程运行的程序,而不是在交互式用户的直接控制下......
[The below bash code is for Debian systems - Ubuntu, Linux Mint distros and so on]
[以下 bash 代码适用于 Debian 系统 - Ubuntu、Linux Mint 发行版等]
The simple way:
简单的方法:
The simple way would be to edit your /etc/rc.local file and then just have your script run from there (i.e. everytime you boot up the system):
简单的方法是编辑您的 /etc/rc.local 文件,然后从那里运行您的脚本(即每次启动系统时):
sudo nano /etc/rc.local
Add the following and save:
添加以下内容并保存:
#For a BASH script
/bin/sh TheNameOfYourScript.sh > /dev/null &
The better way to do this would be to create a Daemon via Upstart:
更好的方法是通过 Upstart 创建一个守护进程:
sudo nano /etc/init/TheNameOfYourDaemon.conf
add the following:
添加以下内容:
description "My Daemon Job"
author "Your Name"
start on runlevel [2345]
pre-start script
echo "[`date`] My Daemon Starting" >> /var/log/TheNameOfYourDaemonJobLog.log
end script
exec /bin/sh TheNameOfYourScript.sh > /dev/null &
Save this.
保存这个。
Confirm that it looks ok:
确认它看起来没问题:
init-checkconf /etc/init/TheNameOfYourDaemon.conf
Now reboot the machine:
现在重启机器:
sudo reboot
Now when you boot up your system, you can see the log file stating that your Daemon is running:
现在,当您启动系统时,您可以看到日志文件表明您的守护进程正在运行:
cat /var/log/TheNameOfYourDaemonJobLog.log
• Now you may start/stop/restart/get the status of your Daemon via:
• 现在您可以通过以下方式启动/停止/重启/获取守护程序的状态:
restart: this will stop, then start a service
重启:这将停止,然后启动一个服务
sudo service TheNameOfYourDaemonrestart restart
start: this will start a service, if it's not running
开始:这将启动一个服务,如果它没有运行
sudo service TheNameOfYourDaemonstart start
stop: this will stop a service, if it's running
停止:这将停止服务,如果它正在运行
sudo service TheNameOfYourDaemonstop stop
status: this will display the status of a service
status:这将显示服务的状态
sudo service TheNameOfYourDaemonstatus status
回答by Luis Mu?oz
Another cool trick is to run functions or subshells in background, not always feasible though
另一个很酷的技巧是在后台运行函数或子shell,但并不总是可行
name(){
echo "Do something"
sleep 1
}
# put a function in the background
name &
#Example taken from here
#https://bash.cyberciti.biz/guide/Putting_functions_in_background
Running a subshell in the background
在后台运行子shell
(echo "started"; sleep 15; echo "stopped") &
回答by Niko
Some commentors already stated that answers to your question will not work for all distributions. Since you did not include CentOS in the question but only in the tags, I'd like to post here the topics one has to understand in order to have a control over his/her proceeding regardless of the distribution:
一些评论者已经表示,您的问题的答案不适用于所有发行版。由于您没有在问题中包含 CentOS,而只是在标签中,我想在这里发布人们必须了解的主题,以便无论发行版如何都能控制他/她的进程:
- what is the init daemon (optional)
- what is the inittab file (/etc/inittab)
- what does the inittab file do in your distro (e.g. does it actually run all scripts in /etc/init.d ?)
- 什么是 init 守护进程(可选)
- 什么是 inittab 文件 (/etc/inittab)
- inittab 文件在您的发行版中做了什么(例如,它实际上运行 /etc/init.d 中的所有脚本吗?)
For your problem, one could start the script on sysinit by adding this line in /etc/inittab and make it respawn in case it terminates:
对于您的问题,可以通过在 /etc/inittab 中添加这一行来启动 sysinit 上的脚本,并使其在终止时重新生成:
# start and respawn after termination
ttyS0::respawn:/bin/sh /path/to/my_script.sh
The script has to be made executable in advance of course:
当然,必须提前使脚本可执行:
chmod +x /path/to/my_script.sh
Hope this helps
希望这可以帮助