简体   繁体   中英

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. I am following some examples found here:

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. Does my script need to be the one that creates the pid file, or does it automatically get created? 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.

If my script is supposed to make it, what is the proper way to make the pid file?

Thanks

Look at the existing daemons for example (such as /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.

In other words, you can declare in the daemon-starting script, what the PID-file is, but creating it is up to the daemon. Speaking of the daemons, you may wish to use the daemon(8) utility, if your daemon is, in fact, a shell script. The utility will take care of the PID-file creation for you. (If the daemon is written in C, you can/should use daemon(3) function.)

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)). 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.

In general, a daemon is supposed to create and clean up its own PID file.

From a shell-script you can give the following command to create it;

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. Also call cleanup just before exiting the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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