Saturday, February 11, 2012

Shell Script for auto-start and shutdown for a service in linux

Some services are having different starting points in linux. for example webmin: To start webmin we need to execute the command /etc/webmin/start where as all service can be started as /etc/init.d/service-name start. this is comman practice now. so we will write a shell script which will allow us to make it auto-start and shutdown for a service with comman practice & will start when we boot up the computer. lets take an example of webmin.

To make webmin automatically start when we boot up the computer,Create a file in "/etc/init.d/" directory as

sudo vi /etc/init.d/webmin

And put the following code in that file:
# Webmin auto-start
# description: Auto-starts webmin
# processname: webmin
# pidfile: /var/run/
case $1 in
sh /etc/webmin start
sh /etc/webmin stop
sh /etc/webmin start
sh /etc/webmin stop
exit 0

now we need to change the file permission of this file so that it become executable.

sudo chmod 755 /etc/init.d/webmin

The now the final step is to linking this script to the startup folders with a symbolic link as.

sudo ln -s /etc/init.d/webmin /etc/rc1.d/K99webmin
sudo ln -s /etc/init.d/webmin /etc/rc2.d/S99webmin

Now we are ready with our new set of commands to start, stop & restart the service as

sudo /etc/init.d/webmin start
sudo /etc/init.d/webmin stop
sudo /etc/init.d/webmin restart