Procedure To Run An Application As Daemon In Linux


DESCRIPTION:

  • I wrote a simple server program in C. It monitor my client application.
  • Client application need to send acknowledge to server continuously with out any help from user.
  • So i making my client application runs in background and it should start at boot time.
PROCEDURE:
  • My client application name is client_ack. Copy this file to bin directory
         sudo cp client_ack /bin/client_ack
  • Go to the /etc/init.d/ directory and create a file and 
         cd /etc/init.d
         sudo vi client_d
  • Write the following script into file

         #!/bin/sh
         ### BEGIN INIT INFO
         # Provides:          client_d
         # Required-Start:    $network $remote_fs $syslog
         # Required-Stop:     $network $remote_fs $syslog
         # Should-Start:      network-manager
         # Should-Stop:       network-manager
         # X-Start-Before:    $x-display-manager gdm kdm xdm wdm 
                              ldm sdm nodm
         # X-Interactive:     true
         # Default-Start:     2 3 4 5
         # Default-Stop:      0 1 6
         # Short-Description: server client application
         ### END INIT INFO

         USAGE="usage: $0 {start|stop}";    
         usage() {
             echo $USAGE >&2
         }     
    
         ss_start() {
             cd /bin
             rm -r nohup.out
             exec nohup ./client_ack &
             sleep 3
             echo ""
         }
     
         ss_stop() {
             killall -9 client_ack
         }

         case $1 in
             start) ss_start ;;
             stop) ss_stop ;;
             *) usage
             exit 1
             ;;
         esac
  • Give executable permission to client_d file
         chmod +x client_d
  • Create symbolic link for start up using following command
         sudo update-rc.d client_d defaults
  • Restart the PC
  • You can check the client_d process running by using following command
         ps aux | grep client_d
  • You can Stop and Start the client_d manually by using following commands
        TO START : sudo service client_d start
        TO STOP  : sudo service client_d stop
  • Now my server getting acknowledgement message from client without any help from user and its start at boot time.

2 comments:

  1. can you also put the C programs? This is just a bash script driving the C program every 3 seconds

    ReplyDelete
  2. Hi parnav. you wrongly understood the logic. This script not execute the C program every 3 second. 3 second is just the delay.
    What actually happening is & symbol make the application run in background and nohup get the stdout of the application i,e nohup will store all content displayed by the c program.
    Try to exercise this you will clearly get it...
    I will post the C program for this future days...

    ReplyDelete