Search This Blog

Saturday, October 19, 2013

Ubuntu 12.04 - create a simple daemon (service) for your application

Creating a daemon in Linux is as simple as creating a BASH/SH script in /etc/init.d. BASH is NOT part of this short tutorial so I won't go into the details of the script itself.

To create a daemon create a script in /etc/init.d. To simplify the creation you can copy the skeleton script in /etc/init.d/skeleton. Your script need to handle the following daemon commands:
start
stop
restart/reload
force-reload
status

cp /etc/init.d/skeleton /etc/init.d/daemonName
sudo chmod 775 /etc/init.d/daemonName

To enable the daemon to start at boot:
update-rc.d daemonName defaults 97 03

http://manpages.ubuntu.com/manpages/hardy/man8/update-rc.d.8.html


To disable the daemon from starting at boot:

update-rc.d -f daemonName remove

12 comments:

  1. Great tutorial...
    sudo chown 775 /etc/init.d/daemonName

    Should this be sudo chmod 775 not chown?

    ReplyDelete
    Replies
    1. Yes, you're right. I already updated the post. Thanks.

      Delete
  2. I'm still struggling to figure out what further is required to create the service. I followed your tutorial and enabled the daemon. However when I do the service "servicename" start, nothing happens. What am I missing?

    ReplyDelete
    Replies
    1. The command "update-rc.d daemonName defaults" should enable the service. Then "sudo service daemonName start" should start the service.
      Maybe you're mispelling the service name?

      Also the service should implement "start" "stop" "restart" command paraters - example bash script is in /etc/init.d/skeleton

      Delete
  3. It's worth noting that with upstart in newer versions of Ubuntu (including 12.04) you just need to create the appropriate config file in /etc/init/ (note NOT /etc/init.d). This will result in the service that you can start using service xxx start and the config file is used to start the service automatically on boot so no need for update-rc.d.

    ReplyDelete
    Replies
    1. +1: I think upstart is a neater/cleaner way of creating services. See a tutorial on it here: https://help.ubuntu.com/community/UbuntuBootupHowto

      Delete
  4. -1. you are mixing up services and daemons.og descThe blog describes creation of a service. Deamon is something different.

    ReplyDelete
    Replies
    1. What?? A daemon is the equivalent of a Windows Service. Different names but same intent. A daemon IS a Unix/Linux service.

      Delete
  5. thanks a lot for the tutorial!

    ReplyDelete
  6. Many thanks for the manual. Short and correct

    ReplyDelete
  7. how can I do something like this but if I want to run a .py file?

    ReplyDelete
  8. Hi so where should i place the custom script.

    ReplyDelete

If you like this post, please leave a comment :)