From: Cameron Simpson To: unix-basics@yahoogroups.com Cc: Bcc: Subject: Re: services on unix platform Reply-To: cs@zip.com.au In-Reply-To: <20020110040343.70662.qmail@web12704.mail.yahoo.com>; from sirishpannala@yahoo.com on Wed, Jan 09, 2002 at 08:03:43PM -0800 On 20:03 09 Jan 2002, sirish reddy wrote: | thanks for the response.i want to write a program which acts as a operating | system service on unix platforms but i dont know how to start and where to | start.i designed one for windows but i dont have idea on unix. Daemons (programs that run silently in the background doing things for people) are usually started from shell scripts. On systems with SysV-derived startups this is done by placing a shell script in the directory /etc/init.d with a suitable name which takes one argument: "start" or "stop". For the sake of an example we will imagine the script is called "sirish", and thus installed as /etc/init.d/sirish Its content would look something like this: #!/bin/sh case "$1" in start) echo "Starting sirish's process ..." run-the-program-to-supply-the-service some args here ... & echo $! >/var/run/sirish.pid ;; stop) echo "Stopping sirish's process ..." kill `cat /var/run/sirish.pid` ;; *) echo "Usage: $0 {start|stop}" >&2 exit 1 ;; esac This script invokes your program and lets it run in the background and stores its process id in the file /var/run/sirish.pid for later use. The permissions on the script should be 755: chmod 755 /etc/init.d/sirish Next you must decide under which run levels this process should be active. SysV flavours UNIX systems have a run level notion representing the functions offered by the system. 0 is "down" (off). 1 is single user (no services, just a root shell; this is a maintenance mode). 2 is up, multiuser, no network services (so up, but for example not serving its discs to other machines on the network etc), 3 is up, multiuser, network services active. 6 is usually a fake runlevel to request to get the machine to reboot. On RedHat linux systems there 5 is defined, being like 3 but with a graphical login screen instead of the textual login presented in run level 3. Let us suppose you want the service available at all time. This means run levels 2 and 3 (and also 5 on RedHat). To do this you would place symbolic links in the directories /etc/rc2.d /etc/rc3.d /etc/rc5.d called SNNsirish pointing at your main sirish script, for example by saying: ln -s /etc/init.d/sirish /etc/rc2.d/S90sirish The NN in the name above (and the 90 in the example command) affect the order in which services are started. Earlier services have low numbers and later services have nigh numbers. The ordering is basicly to arrange that if service B requires something supplied by service A then A must be started before B. Addon services like your are almost always started quite late in the list. Similarly the services must be shut down when switch to run levels 0 or 1, and matching "K" (kill) links should be made, for example: ln -s /etc/init.d/sirish /etc/rc0.d/K10sirish Note that here we have a _low_ number because stopping processes is naturally done in reverse order of starting them. All that happens at startup is that every script called SNNthing in the appropriate /etc/rcN.d run level directory is run with the "start" argument, and the converse KNNthing script is run with "stop" when shutting down. Make sense? -- Cameron Simpson, DoD#743 cs@zip.com.au http://www.zip.com.au/~cs/ Be smart, be safe, be paranoid. - Ryan Cousineau, courier@compdyn.com DoD#863, KotRB, KotKWaWCRH