Ubuntu – How to port a FreeBSD daemon to Ubuntu

services

I had a FreeBSD machine and it had a /usr/sbin/daemon command which could run my program in the background.

The question is how to do something similar in Ubuntu? I would like something I can put in a startup script.
What I mean is my program can't put itself in a background when starts.

I tried:

start-stop-daemon --start --quiet --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON

But it just starts the program and I must wait for it to finish.

Best Answer

Use the --background option of start-stop-daemon. From the manual page of start-stop-daemon(8):

-b, --background
Typically used with programs that don't detach on their own. This option will force start-stop-daemon to fork before starting the process, and force it into the background. WARNING: start-stop-daemon cannot check the exit status if the process fails to execute for any reason. This is a last resort, and is only meant for programs that either make no sense forking on their own, or where it's not feasible to add the code for them to do this themselves.

In a shell, programs can be backgrounded with & as in:

$ myprogram &
[1] 5042
Related Question