4

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!

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

9

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • how can i modify it excuse me as i told you i just take my first steps on Linux World –  Jan 22 '12 at 09:01
  • 1
    try emacs (or vi, no flame war!) –  Jan 22 '12 at 09:18
  • i don't ask about what text editor i use but what section/Variable in this file to change –  Jan 22 '12 at 09:24
  • Oh, can't help you there. There are so many ways to solve a problem in linux, it's great. BTW, welcome to linux! (CopyRight free!) –  Jan 22 '12 at 09:30
  • Ok. Your real question is how to debug your /etc/init.d/mamoudservice script. Try to run /bin/bash -vx /etc/init.d/mamondservice start and you'll understand what is happening. On a more general note, learn more about your operating system before adding services to it. We cannot help more because we have no idea of how your /usr/sbin/mamouddaemon operate, and which program options it is accepting. –  Jan 22 '12 at 09:49
  • I improved my answer. –  Jan 22 '12 at 09:58
  • my daemon application is just the template for any daemon app and it does nothing now –  Jan 22 '12 at 10:19
  • when i tried this command /bin/bash -vx /etc/init.d/mamondservice start it starts my application now how can i make it execute it when startup –  Jan 22 '12 at 10:49
  • https://unix.stackexchange.com/a/480897/5132 /etc/init.d/skeleton is no more. – JdeBP Nov 10 '18 at 04:53
2

Try chkconfig with the name of your service and then "on." This must be run as root.

Kevin
  • 40,767
  • i don't want to use GUI App or Terminal if i want so i can use Startup Application Application on Ubuntu –  Jan 22 '12 at 08:58
  • chkconfig modifies a file so that the indicated service is automatically started the next time you turn your computer on. Trust me, after setting up a CentOS server cluster, this is how you do it. –  Jan 22 '12 at 09:15
  • @Ma7moud El-Naggar: chkconfig creates various symlinks in /etc/rc\d.d/ for you. – jfs Jan 22 '12 at 10:36
  • i have already symlinks on rc#.d directories –  Jan 22 '12 at 10:54