Friday, December 31, 2010

Command Line Init Script Generator For Linux Or Unix

Hey There,

To start this week off, we've got another updated script (now that we have over 600 some odd posts, we actually have about 400 some odd loose ends to finish up, as well ;) This script is an update of an earlier post we did to showcase a basic init script generator for Linux or Unix, again hearkening back to 2007. How time flies when you're not paying attention to what you're doing ;)

This script includes several updates (which haven't been tested to perfection - so feel free to write in with complaints ;) such as the ability to use command line arguments to specify a wider variety of options, set some to default and not even enter any (except three) if you don't want to. Actually, the only three required arguments are the name of the program you want to start with the init script you generate, the fully qualified name of the same program and the name of the init script itself. All the other options are easily displayed by just running the command without any arguments, like so:

host # ./rcscript.sh

Options -n -p and -f are required!

Usage: ./rcscript.sh [-h for this help screen]

Required switches:
[-n init script name] [-p name of program to control]
[-f controlled program's fully qualified name]

Optional switches:
[-i init directory] [-3 rc3.d directory] [-0 rc0.d directory
[-s start options for your program]
[-S stop options for your program]
[-k additional programs to kill on stop - space separated]
[-b common binary directory - defaults to /usr/bin]

Be sure to "double quote" any switch arguments with spaces!


Hope you enjoy the updates and this script helps you out in some way or fashion :)

Cheers,

Creative Commons License
rcscript.sh by Mike Golvach is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Based on a work at linuxshellaccount.blogspot.com.
Permissions beyond the scope of this license may be available at http://linuxshellaccount.blogspot.com.

#!/bin/sh
#
# Generic Init Script Creator
# 2009 - Mike Golvach - eggi@comcast.net
#
#Creative Commons License
rcscript.sh by Mike Golvach is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Based on a work at linuxshellaccount.blogspot.com.
Permissions beyond the scope of this license may be available at http://linuxshellaccount.blogspot.com.
#

trap 'rm -f ${init_dir}/${script_name} ${rc3_dir}/${script_name} ${rc0_dir}/${script_name} ${script_name}' 1 2 3 9 15

# Tips for a few command line arguments - setting an argument to zero will not be considered equivalent to not defining it
# a. set init_dir to 0 if you don't want any scripts and links setup - set rc3_dir, rc2_dir and/or init_dir to "0" to not create that link/script.
# b. If any of the init_dir, rc2_dir and/or rc3_dir variables are not set, they will default to the examples below.
# c. options that include 0 in the example do not need to be set and are entirely optional
#

# init_dir= /etc/init.d or 0
# rc3_dir= /etc/rc3.d or 0
# rc0_dir= /etc/rc0.d or 0
# script_name= myStartScript.sh
# program_name= testProgram
# fully_qualified_program_name= /usr/local/sbin/testProgram
# start_options= "start" or 0
# stop_options= "stop" or 0
# sure_kill= "space delimited list of extra programs to kill" or 0
# common_bin_directory= "place where common binaries are on your system" or defaults to /usr/bin
#

function usage()
{
echo
echo "Usage: $0 [-h for this help screen]"
echo
echo "Required switches:"
echo "[-n init script name] [-p name of program to control]"
echo "[-f controlled program's fully qualified name]"
echo
echo "Optional switches:"
echo "[-i init directory] [-3 rc3.d directory] [-0 rc0.d directory"
echo "[-s start options for your program]"
echo "[-S stop options for your program]"
echo "[-k additional programs to kill on stop - space separated]"
echo "[-b common binary directory - defaults to /usr/bin]"
echo
echo "Be sure to \"double quote\" any switch arguments with spaces!"
echo
exit 1
}

while getopts 0:3:b:f:hi:k:n:p:s:S: option
do
case $option in
'i')
if [[ "$OPT_ARG" == "0" ]]
then
init_dir=0
else
init_dir="$OPTARG"
fi
;;
'3')
if [[ "$OPT_ARG" == "0" ]]
then
rc3_dir=0
else
rc3_dir="$OPTARG"
fi
;;
'0')
if [[ "$OPT_ARG" == "0" ]]
then
rc0_dir=0
else
rc0_dir="$OPTARG"
fi
;;
'n')
script_name="$OPTARG"
;;
'p')
program_name="$OPTARG"
;;
'f')
fully_qualified_program_name="$OPTARG"
;;
's')
if [[ "$OPT_ARG" == "0" ]]
then
start_options=""
else
start_options="$OPTARG"
fi
;;
'S')
if [[ "$OPT_ARG" == "0" ]]
then
stop_options=""
else
stop_options="$OPTARG"
fi
;;
'k')
if [[ "$OPT_ARG" == "0" ]]
then
additional_kills=""
else
additional_kills="$OPTARG"
fi
;;
'b')
if [[ "$OPT_ARG" == "0" || -z "$OPT_ARG" ]]
then
common_bin_directory="/usr/bin"
else
common_bin_directory="$OPTARG"
fi
;;
'h')
usage
;;
*)
usage
;;
esac
done

if [[ -z $script_name || -z $program_name || -z $fully_qualified_program_name ]]
then
echo
echo "Options -n -p and -f are required!"
usage
fi

if [[ -z $common_bin_directory ]]
then
common_bin_directory="/usr/bin"
fi

if [[ -e $script_name ]]
then
rm $script_name
fi

cat << EOM >>$script_name

#!/bin/sh

#
# ${program_name} init script
# Usage: $script_name [start|stop|restart]
#

case \$1 in
'start')
echo
echo Starting ${program_name}....
echo
${fully_qualified_program_name} ${start_options} >/dev/null 2>&1 &
${program_name}_running=\`${common_bin_directory}/ps -ef|${common_bin_directory}/grep "${fully_qualified_program_name}"|${common_bin_directory}/grep -v grep|${common_bin_directory}/awk '{print \$2}'\`
if [ "\$${program_name}_running" == "" ]
then
echo "${program_name} start cannot be confirmed. Please check"
echo "system output and application logs for further detail"
else
echo "${program_name} started successfully"
fi
;;
'stop')
echo
echo Stopping ${program_name}....
echo
if [ "$stop_options" != " " ]
then
${fully_qualified_program_name} ${stop_options} >/dev/null 2>&1 &
fi
${program_name}_walking=\`${common_bin_directory}/ps -ef|${common_bin_directory}/grep "${fully_qualified_program_name}"|${common_bin_directory}/grep -v grep|${common_bin_directory}/awk '{print \$2}'\`
if [ "\$${program_name}_walking" == "" ]
then
echo "${program_name} does not appear to be running."
echo "Process not found. Not shut down."
else
counter=5
dead="alive"
echo "Shutting ${program_name} down."
echo "${fully_qualified_program_name} - pid ${program_name}_walking - stopping."
while [ \$counter -gt 0 ]
do
${program_name}_still_walking=\`${common_bin_directory}/ps -ef|${common_bin_directory}/grep "${fully_qualified_program_name}"|${common_bin_directory}/grep -v grep|${common_bin_directory}/awk '{print \$2}'\`
if [ "\$${program_name}_still_walking" != "" ]
then
echo "killing pid \$${program_name}_still_walking "
${common_bin_directory}/kill \$${program_name}_still_walking
counter=\`expr \$counter - 1\`
sleep 1
else
dead="dead"
echo "dead"
counter=0
fi
done
if [ \$dead = "alive" ]
then
echo "Could not kill process after 5 attempts."
echo "Process ${program_name}_walking still active."
${common_bin_directory}/ps -ef|${common_bin_directory}/grep "${fully_qualified_program_name}"|${common_bin_directory}/grep -v grep
fi
fi
EOM
if [[ $additional_kills && ! -z $additional_kills ]]
then
cat << EOM >>$script_name
for aks in `echo $additional_kills`
do
echo
echo Stopping \$aks....
echo
aks_walking=\`${common_bin_directory}/ps -ef|${common_bin_directory}/grep "\$aks"|${common_bin_directory}/grep -v grep|${common_bin_directory}/awk '{print \$2}'\`
if [ "\$aks_walking" == "" ]
then
echo "\$aks does not appear to be running."
echo "Process not found. Not shut down."
else
counter=5
dead="alive"
echo "Shutting \$aks down."
echo "\$aks - pid \$aks_walking - stopping."
while [ \$counter -gt 0 ]
do
aks_still_walking=\`${common_bin_directory}/ps -ef|${common_bin_directory}/grep "\$aks"|${common_bin_directory}/grep -v grep|${common_bin_directory}/awk '{print \$2}'\`
if [ "\$aks_still_walking" != "" ]
then
echo "killing pid \$aks_still_walking "
${common_bin_directory}/kill \$aks_still_walking
counter=\`expr \$counter - 1\`
sleep 1
else
dead="dead"
echo "dead"
counter=0
fi
done
if [ \$dead = "alive" ]
then
echo "Could not kill process after 5 attempts."
echo "Process \$aks_walking still active."
${common_bin_directory}/ps -ef|${common_bin_directory}/grep "\$aks"|${common_bin_directory}/grep -v grep
fi
fi
done
EOM
fi
cat << EOM >>$script_name
;;
'restart')
echo
echo ReStarting ${program_name}....
echo
\$0 stop
\$0 start
;;
*)
echo "Usage: \$0 [start|stop|restart]"
;;
esac
EOM

${common_bin_directory}/chmod 750 ${script_name};
${common_bin_directory}/cp ${script_name} ${init_dir}/${script_name}
${common_bin_directory}/ln -s ${init_dir}/${script_name} ${rc3_dir}/${script_name}
${common_bin_directory}/ln -s ${init_dir}/${script_name} ${rc0_dir}/${script_name}


, Mike


Banner for College Student-Oriented Sites (728 version 1)



Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.