LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-24-2006, 01:58 AM   #16
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to start my GUI program by a daemon? oldh Linux - Software 3 08-05-2005 02:04 PM
How to have 'at' daemon start automatically on boot TrulyTessa Linux - Newbie 4 12-13-2004 11:12 AM
Start program in daemon mode from cgi-bin Xa! Programming 1 11-30-2004 11:00 PM
Getting a daemon to start at boot cmfarley19 Linux - General 5 12-14-2002 02:54 PM
How do I start ntp daemon at boot? jkcunningham Linux - Software 5 07-19-2002 08:49 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration