This question is not strictly programming related, but for sure important for programmers.
I wrote a simple smtp server, when I run it from console all is fine, except i
There are already good answers but I will add some additional information.
You do not need to install additional software such as supervisord
on Debian to take care of backgrounding the process.
Debian comes with a tool called start-stop-daemon
which is a standard way for starting daemons in init.d
scripts. It can also also put the process in background for you if the program does not do it on its own. Have a look at the --background
option.
Use /etc/init.d/skeleton
as the basis of your init script, but change the do_start()
function as follows:
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \
--background --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \
--background --exec $DAEMON -- $DAEMON_ARGS \
|| return 2
I also added the --make-pidfile
option which creates the PID file for you.
In case you need to switch to a different user in a secure way, there is also --chuid
option.
On Ubuntu and RHEL/CentOS/SL 6.X the simplest way is to write an upstart
job configuration file. Just put exec /usr/sbin/yourprogram
in the /etc/init/yourprogram.conf
configuration file. With upstart there is no need to force the program in background. Do not add expect fork
or expect daemon
which you need with traditional daemons. With upstart it is better if the process does not fork.