top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Creating a Service for a Daemon in Linux?

+3 votes
292 views

A Linux service is an application (or set of applications) that runs in the background waiting to be used, or carrying out essential tasks. Execute "ls /etc/init.d". This will show you the names of various services on your machine.

Now coming to the point for creation of a service in Linux, as explained above you can create a script in the /etc/init.d/ directory and content will look like following (Replace myservice with your daemon)

#!/bin/sh
#
# myservice     This shell script takes care of starting and stopping
#               the <myservice>
#

# Source function library
. /etc/rc.d/init.d/functions

# Handle manual control parameters like start, stop, status, restart, etc.    
case "$1" in
  start)
    # Start daemons.

    echo -n $"Starting <myservice> daemon: "
    echo
    daemon <myservice>
    echo
    ;;

  stop)
    # Stop daemons.
    echo -n $"Shutting down <myservice>: "
    killproc <myservice>
    echo

    # Do clean-up works here like removing pid files from /var/run, etc.
    ;;
  status)
    status <myservice>

    ;;
  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0

Now say if you can type service myservice start to start the myservice daemon.

How to start a Service at the Bootup
Simple answer is put it into your /etc/rc.d/rc.local this is the one which is started as last in the linux bootup. The script must always end with exit 0

How to Stop a Service at the shutdown
Put your script in /etc/rc6.d and make it executable (sudo chmod +x myscript)
The name of your script must begin with K99 to run at the right time.
Note: The scripts in this directory are executed in alphabetical order

posted Feb 18, 2014 by Meenal Mishra

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...