简体   繁体   English

在FreeBSD中为shell脚本创建启动守护进程

[英]Creating a startup daemon for a shell script in FreeBSD

I am trying to create a file in rc.d/ that will start up a /bin/sh script that I have written. 我试图在rc.d /中创建一个文件,该文件将启动我编写的/ bin / sh脚本。 I am following some examples found here: 我正在跟踪一些在这里找到的示例:

http://www.freebsd.org/doc/en/articles/rc-scripting/article.html#rc-flags http://www.freebsd.org/doc/en/articles/rc-scripting/article.html#rc-flags

#!/bin/sh -x

# PROVIDE: copyfiles

. /etc/rc.subr

name=copyfiles
rcvar=copyfiles_enable
pidfile="/var/run/${name}.pid"

command="/var/etc/copy_dat_files.sh -f /var/etc/copydatafiles.conf"
command_args="&"


load_rc_config $name
run_rc_command "$1"

It seems like I am having a problem with the pidfile. 好像我的pidfile有问题。 Does my script need to be the one that creates the pid file, or does it automatically get created? 我的脚本需要成为创建pid文件的脚本,还是会自动创建? I have tried both ways, and whether or not i make my script create a pid file, I get an error that the pid file is not readable. 我已经尝试了两种方式,无论我是否让我的脚本创建一个pid文件,我得到一个错误,pid文件是不可读的。

If my script is supposed to make it, what is the proper way to make the pid file? 如果我的脚本应该制作,那么制作pid文件的正确方法是什么?

Thanks 谢谢

Look at the existing daemons for example (such as /etc/rc.d/mountd). 例如,查看现有的守护程序(例如/etc/rc.d/mountd)。 Then look at the subroutines in /etc/rc.subr -- there is code in there to check the PID-file, but nothing creates it. 然后查看/etc/rc.subr中的子例程-那里有代码可以检查PID文件,但是什么也没有创建。

In other words, you can declare in the daemon-starting script, what the PID-file is, but creating it is up to the daemon. 换句话说,您可以在守护程序启动脚本中声明PID文件是什么,但创建该文件取决于守护程序。 Speaking of the daemons, you may wish to use the daemon(8) utility, if your daemon is, in fact, a shell script. 说到守护程序,如果您的守护程序实际上是一个shell脚本,那么您可能希望使用daemon(8)实用程序。 The utility will take care of the PID-file creation for you. 该实用程序将为您处理PID文件的创建。 (If the daemon is written in C, you can/should use daemon(3) function.) (如果守护程序是用C编写的,则可以/应该使用daemon(3)函数。)

BTW, in my own opinion, daemons, when opening up the PID-files for creation, should also lock them (with flock(3) or fcntl(2) or lockf(3)). BTW,在我看来,守护进程在打开用于创建的PID文件时,也应该锁定它们(使用flock(3)或fcntl(2)或lockf(3))。 This way, if an instance crashes (or is killed) without removing the PID-file, the next instance will have no problem determining, the file is stale. 这样,如果一个实例崩溃了(或被杀死)而没有删除PID文件,则下一个实例将毫无问题地确定该文件是陈旧的。

In general, a daemon is supposed to create and clean up its own PID file. 通常,守护程序应该创建并清理自己的PID文件。

From a shell-script you can give the following command to create it; 从shell脚本中,您可以提供以下命令来创建它;

echo $$ >/var/run/${name}.pid

Do not forget to remove the file before exiting the script. 不要忘记在退出脚本之前删除文件。 Write a cleanup() function that does that and let trap call that function when certain signals occur. 编写一个cleanup()函数来执行该操作,并让trap在发生某些信号时调用该函数。 Also call cleanup just before exiting the script. 还要在退出脚本之前调用cleanup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM