- 02
- Oct

Have a Linux executable that you would like to run as a daemon? Making your own init.d scripts can be a bit tricky, but I can help you out.
For details on Linux run levels look here.
The run levels that are most important to us are 2, 3 and 5.
VERY basic sample init.d script (Replace italics respectively):
### BEGIN INIT INFO
# Provides: My Daemon
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Description: My Daemon Desc
### END INIT INFO
case “$1″ in
’start’)
/path/to/my/executable -startcommand
;;
’stop’)
/path/to/my/executable -stopcommand
;;
*)
echo “Usage: $0 { start | stop }”
;;
esac
exit 0
Even though most of the top part is commented out, Linux still reads it to determine what daemons need to be running before this can start, the name and desc, and also what runlevels this is suppose to start in:
# Provides: My Daemon
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Description: My Daemon Desc
### END INIT INFO
After writing the file save it to /etc/mydaemon. Now you should be able to stop and start your daemon with the following command:
And likewise for stopping:
Sometimes executables in Linux are picky about what directory you call them from. I would label that as a bug, but to get around that you can enter the cd /path/to/my/executable command before your stop and start command and just write it like so:
’start’)
cd /path/to/my/executable
./executable -startcommand
;;
’stop’)
cd /path/to/my/executable
./executable -stopcommand
;;
*)
echo “Usage: $0 { start | stop }”
;;
esac
exit 0
EDIT: Don’t forget to run chkconfig –add name or update-rc.d on Debian based systems after making your init.d script.
Hopefully that will get you started so you don’t have to worry about manually starting anything when your Linux server boots. Again, this is very basic. You can also enter a restart and status function, but these are optional and would vary depending on the executable. Have fun.
Related Posts
Tags: Daemon, init.d, Linux, Run level, Server, Service



October 30th, 2007 at 4:12 am
[...] Read the rest of this entry ยป [...]
December 1st, 2007 at 12:35 pm
[...] ( >> chatlog.txt). There are many other possibilities including setting this up as a daemon with an init.d script. possibilities. If you have any netcat tips or ways to implement this with OpenSSL, I would love to [...]
December 13th, 2007 at 9:26 am
[...] to enter null routes from within the Vyatta CLI. But this should work just fine when using an init.d script or entered directly in the bash shell: ip route add blackhole 192.168.0.0/16 ip route add blackhole [...]