LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Start a daemon/program at boot (https://www.linuxquestions.org/questions/linux-newbie-8/start-a-daemon-program-at-boot-27170/)

timmeke 01-24-2006 01:58 AM

I don't have an example handy here, but most of the /etc/init.d scripts basically do the following.
You can use them via the "service" program. Each service is represented by one script in /etc/init.d, with the same name as the service.
/sbin/service script_name start|stop|restart
Some also include other options, like "status", "condrestart" (conditional restart), etc.
But, let's start with just the basic 3: "start" to start the daemon, "stop" to stop it and "restart" to stop
the daemon (if it's running) and then restart it. Restart is sometimes used after updating the daemons configuration files. Some daemons also support sending them signals (like SIGHUP) to make them reread their configuration files, but that depends really on the implementation of the daemon program itself.

The script in /etc/init.d would contain the following:
Code:

#!/bin/bash
#=> the shell you want to use for interpreting the script's code.
Code:

# chkconfig levels number1 number2
chkconfig configuration. When entering a runlevel, chkconfig will check the "levels" (the default runlevels) and if you are entering any of those levels, then the script will be run to start your daemon. number1 and number2 determine the order in which the scripts are launched (number1) or stopped (number2).
Usually number2 = 100 - number1. You could also take a look at the values for other scripts to get a clue.
Or, use the "services configuration" option in your graphical menu.

Next, the script will include the pre-defined functions in /etc/init.d/functions.
Code:

. /etc/init.d/functions
Then, it defines some functions for starting/stopping/restarting and whatever else you want to do.
Typically, start() does something like
prog="Name_of_your_program"
echo -n $"Starting $prog: "
Possibly do some checking, for instance to see if you are starting it as "root" or not.
start your daemon, ie via the "daemon" function.

stop() similarly does:
echo -n $"Stopping $prog: "
kill your program, ie via "kill" or the "killproc" function.

restart() only does:
stop
start

After the definition of these functions, the script simply needs to be told when it should run each of the functions. In other words, it needs to interprete the command line argument.
ie /sbin/service your_script start => launches your script, passing "start" as first and only argument.
So, it does:
case "$1" in
start)
#argument is now "start"
start #calls start() function
;;
stop)
stop
;;
restart)
restart
;;
*)
#Argument isn't recognized by the script => exit with error message
echo $"Usage: $0 {start|stop|restart}" #you can add other options as well
exit 1; #exit with error
esac

A good example would be the script for "snmpd". It basically does what I describe here, plus some testing for the root user ($UID == 0) and also adds options for "condrestart" (restart only if there is no lock), "reload" (sending a SIGHUP signal to daemon for making it reread it's configuration files - your daemon must support this) and "status" (using the pre-defined status function).

Some Java daemons, run with jsvc for instance, also support the use of a PID-file as I've described before.
I start & stop my Tomcat webserver using jsvc.

Note also that, since the script may not be run under your user and almost certainly is called without loading your environment first, you'll need to set up any environment variables at the top of the script.

Please also look at the "similar threads" section below for more examples.


All times are GMT -5. The time now is 02:53 AM.