PDA

View Full Version : How to convert scripts to a daemon



suneesh
21st April 2009, 09:25 AM
Hello There,

I have a doubt in Linux. I used to write shell scripts in order to carry out particular tasks, or to start some applications. I would like to know how I can convert these scripts to a service, so that it will be started automatically at startup, or I can start it using 'service abcd start' command.
Suneesh

sideways
21st April 2009, 03:21 PM
Here's a minimal service script you can start with



#!/bin/bash
#
# chkconfig: 345 80 20
# description: A minimal service script


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

lockfile=/var/lock/subsys/minimal
RETVAL=0

start() {
/bin/touch "$lockfile"
}

stop() {
/bin/rm -f "$lockfile"
}

restart() {
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
if [ -f $lockfile ]; then
echo $"minimal is enabled."
RETVAL=0
else
echo $"minimal is disabled."
RETVAL=3
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac

exit $RETVAL


call the script 'minimal', copy it to /etc/init.d/ and chmod 755

You can start and stop the service as usual and check status. To set the service to start in the specified runlevels use chkconfig, the chkconfig line in the minimal script specifies the default runlevels to start on, the start priority 80 and stop priority 20, normally start + stop = 100, it specifies the number used for the S** and K** start and kill scripts in /etc/rc.*/ dirs
(see RUNLEVEL FILES section in 'man chkconfig' ):


[user@localhost ~]$ sudo chmod 755 /etc/init.d/minimal
[user@localhost ~]$ sudo service minimal start
[user@localhost ~]$ sudo service minimal status
minimal is enabled.
[user@localhost ~]$ sudo service minimal stop
[user@localhost ~]$ sudo service minimal status
minimal is disabled.
[user@localhost ~]$ chkconfig --list | grep minimal
[user@localhost ~]$ chkconfig --add minimal
failed to make symlink /etc/rc0.d/K20minimal: Permission denied
failed to make symlink /etc/rc1.d/K20minimal: Permission denied
failed to make symlink /etc/rc2.d/K20minimal: Permission denied
failed to make symlink /etc/rc3.d/S80minimal: Permission denied
failed to make symlink /etc/rc4.d/S80minimal: Permission denied
failed to make symlink /etc/rc5.d/S80minimal: Permission denied
failed to make symlink /etc/rc6.d/K20minimal: Permission denied
[user@localhost ~]$ sudo chkconfig --add minimal
[user@localhost ~]$ chkconfig --list | grep minimal
minimal 0:off 1:off 2:off 3:on 4:on 5:on 6:off


note you need root permission to create the S** and K** symlinks.

Now just put your script startup code in the start) section and the shutdown code in the stop) section.

NomadRetsif
21st April 2009, 04:23 PM
Just for clarification, do you want scripts to run once on start-up or are you looking to run a script continuously in the background as a daemon to provide a service, i.e. httpd?

suneesh
4th May 2009, 03:16 PM
Yes , I need to do run its like "httpd".. Is it possible to do? pls reply.
Suneesh

NomadRetsif
8th May 2009, 06:47 PM
Sorry for the late response,

If you need your script to run in the background and not terminate on logout, you can execute the script with the nohup command.


nohup /path/to/myscript.sh "arguments" &

You can invoke it through the System V startup scripts as indicated by sideways or add it in /etc/rc.local. Either option will cause the execution persist across reboots.

I do assume your script is coded so it will never terminate otherwise your effort will be wasted.