I have create a script to start a server(my first question). Now I want it to run on the system boot and start the defined server. What should I do to get this done?

My findings tell me put this file in /etc/init.d location and it will execute when the system will boot. But I am not able to understand how the first argument on the startup will be start? Is this predefined somewhere to use start as $1? If I want to have a case startall that will start all the servers in the script, then what are the options I can manage.

My Script is like this:

#!/bin/bash

case "$1" in
start)
     start
    ;;
stop)
    stop
    ;;

restart)
    $0 stop
    $0 start
    ;;
*)
    echo "usage: $0 (start|stop|restart)"
;;
esac
link|improve this question

feedback

3 Answers

up vote 4 down vote accepted

This directory is only the script directory. You then need to add a file to the /etc/rc#.d directory where you put files, and the order they should be run. The number after the rc indicates what run level the machine is running at, according to this chart:

http://en.wikipedia.org/wiki/Runlevel

So if you have:

/etc/init.d/importantscript

Then you need:

/etc/rc.d/rc3.d/S20importantscript
/etc/rc.d/rc6.d/K20importantscript

which does not need to contain anything. The S means start, and the K means kill. This will execute '/etc/init.d/importantstript start' when your machine starts up. The 20 is just for ordering purposes... your script will run behind 'S19' and in front of 'S21'. You can create these files simply by doing:

sudo touch /etc/rc.d/rc3.d/S20importantscript/

Here's a nice summary as well: http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts

link|improve this answer
feedback

The $1 is the command line argument that is passed to your script, it is one of start, stop or restart. In Opensuse, I don't remember having an option to pass other arguments into the script when useing the runlevel editor thingy so I think that these are probably the only ones you should use.

I don't use Centos myself, but it seems that the program to control what is started at which runlevel is ntsysv.

link|improve this answer
feedback

You don't have to --- and shouldn't --- create files in /etc/rc.d/rcN.d/; what you should do instead is put a comment in your init script reading

# chkconfig NNN A B

where NNN is the set of run-levels in which you want the script active (e.g., 345 if it's active in runlevels 3, 4, and 5), and A and B are the start and stop priorities. Then chkconfig --add foo (assuming your script is named foo) will create the files in /etc/rc.d/rcN.d/ with the appropriate names.

You can then use service foo bar to send the bar message to your script (e.g., start, stop, whatever -- that's where your $1 comes from).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.