Tuesday, November 22, 2011

Write Startup scripts in Linux Environment?

Explanation is based on SLES (SUSE Enterprise Linux). It could be related to other distributions of Linux as well.
There are various situations where you write your own script and want it to run at system startup. In Linux environment you have to place your script in /etc/init.d and use 'insserv' command or 'chkconfig' config to insert those script in various runlevels.

[By default, insserv insert the script in runlevel 3 and 5. You can check that by running #chkconfig --list ]

Let's say I want to run 'asterisk' program automatically at startup and I want to make sure that it starts only when all the services of OS are up.

  • 'which' command returns me the location of the program or application#which asterisk
/usr/bin/asterisk

  • Create a script file in /etc/init.d directory
#vi /etc/init.d/asteriskStartupScript
#!/bin/bash
#Copyright (c) 2011 DShah
# All rights reserved
#
#Author: DShah, 2011
# /etc/init.d/asterisk
#PLEASE READ /etc/init.d/skeleton to understand various parameters in startup scripts
#
### BEGIN INIT INFO
# Provides: asterisk
# Required-Start: $ALL
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts Asterisk
### END INIT INFO

#Start service
/usr/sbin/asterisk

Save and close the script file.

  • 'chmod' command can be use to add executable permission
#chmod 744 /etc/init.d/aseriskStartupScript

  • 'insserv' command can be used to insert the script in desired runlevel as specified in script file
#insserv asteriskStartupScript

  • 'chkconfig' command can be used to check if the script will run in next startup
#chkconfig --list asteriskStartupScript


Reference:
http://www.novell.com/coolsolutions/feature/15380.html

No comments: