Linux – Make daemon start up with Linux

daemonlinuxstartup

I'm writing a simple daemon application using C/C++ and I want it to start when Linux starts up.

I have tried to modify /etc/init.d/skeleton to add a script into the init.d directory as follows

  1. added my daemon application in /usr/sbin/ directory and changed NAME=myDaemon

  2. write update-rc.d myDaemon default in Terminal

  3. and it added symbolic links to rc#.d directories

But it didn't work.

My second try was to modify rc.local as

/usr/sbin/myDaemon start 

But this didn't work either.

How can I make my daemon start with the OS? I want to do everything programmatically.

I use Ubuntu 10.10 but if there exists a general solution for all distributions, that would be great!

Best Answer

You don't modify the /etc/init.d/skeleton file. You copy it to a new file /etc/init.d/mamoudservice (replace mamoudservice with a more suitable name) and then you edit that new file appropriately.

Then you add a symlink from /etc/rc2.d/S99mamoudservice to /etc/init.d/mamoudservice etc.

Use e.g.

   /bin/bash -vx /etc/init.d/mamoudservice start

to understand how the bash shell is interpreting your script.

If your daemon program /usr/sbin/mamouddaemon is coded in C or in C++, I suggest using openlog and syslog inside, at least to get debugging messages (don't output to stderr or stdout in a daemon, it could go nowhere).

An alternative to having your /etc/init.d/mamoudservice script might be to put a @reboot entry in your crontab

Related Question