Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux daemon start up

i wrote one service on linux(Redhat Server Edition 5.1) . which is started by shell scritpt, In case when i start my application i manually start my service , now i want to start my service at boot time,by means i put my service on init.d folder by my daemon not invoke at boot time,any have idea how to start a daemon at boot time on linux?

this my sample but is not working

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

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


# Do preliminary checks here, if any
#### START of preliminary checks #########


##### END of preliminary checks #######


# 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
like image 780
Mr.Cool Avatar asked Aug 01 '12 11:08

Mr.Cool


People also ask

How do you start a daemon?

Starting daemons. Daemons can be started by JCL and also by the shell. Some daemons such as inetd can also be started by the shell. Interactive login shells, shell scripts run as background jobs from a login shell, and batch jobs using BPXBATCH to run the shell all can start daemons.

What is Linux daemon?

A daemon is a service process that runs in the background and supervises the system or provides functionality to other processes. Traditionally, daemons are implemented following a scheme originating in SysV Unix.


2 Answers

Put 2 comments into your script:

# chkconfig: - 90 10
# description: description of your service

As root, run :

chkconfig --add my_service
like image 53
Guillaume USE Avatar answered Sep 23 '22 22:09

Guillaume USE


a basic unix daemon does the following:

fork
close all filedescriptors (stdout,stderr, etc)
chdir /
signal handeling (sighup, sigterm etc)
while
do stuff
sleep(xx)
done

(example in C: daemon.c)

Red Hat example on how to install startup scripts:

to start a deamon at system startup in redhat you need a init script. it should be placed in /etc/init.d

example of init script :

Code:

# chkconfig: 3 99 1
# description: my daemon

case "$1" in
'start')
/usr/local/bin/mydaemon
;;

'stop')
pkill mydaemon
;;

'restart')
pkill -HUP mydaemon
;;

esac

the first line will tell chkconfig to start the daemon in run level 3 with priority 99 and kill it as priority 1 when server shutdowns.

to install the startup script use the following: chkconfig --add ./scriptabove Now it will start when the server boots.

to start it right away use: service start

If you want more details information visit a link

Hope this helps somewhat!

like image 28
Sandy8086 Avatar answered Sep 21 '22 22:09

Sandy8086