# Starting a shell script as a daemon



## dcole (Mar 26, 2013)

Below is a crosspost from stackoverflow.com

http://stackoverflow.com/questions/15646797/creating-a-startup-daemon-for-a-shell-script-in-freebsd

But it wasn*'*t seemi*ng* to get much visibility. Feel free to answer here and/or there and I'll accept. 

-----

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

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

Thanks.


----------



## ta0kira (Mar 27, 2013)

My guess is that the program has to set it because many daemons will fork and setsid to completely detach from the calling process, resulting in a different pid than what the process created. rc.subr wouldn't know which pid to put in the file. I also think `# daemon -p ...` is meant to do what you're trying to do with &.

Kevin Barry


----------

