Thursday, June 25, 2009

Can Code And/Or Pictures Be Posted Via Mail2Blogger - Test Number 2

Thursday, June 25, 2009

Revisiting The Command Line Init Script Generator For Linux Or Unix


So far, this experiment in mobile blogging has been a disappointment.  Not much works when you try to work with HTML and the font seems to get screwed up.  I've continued to try and make this a seemless "post via email," but much work is always left at the other end.  I'm leaving this post alone - as sent, with all it's imperfections.  I think, basically, sending a decent post via email using Mail2Blogger will require at least 2 things:

1.  Explicitly encoding text blocks so they're parsed as html and not as plaintext.  This same trap also makes character entity conversion not work since they are, themselves, converted to even more bizarre character entities.
2.  Scripting out actually rewriting the entire Blogger post area in such a fashion, including the uuencoded attachment of any binary files, like video or pictures.

Below are a few smatterings of things I hope make it into this post, including some shell script and a picture.

For reasons that are probably obvious, I'm not really going out there and tooting my own horn about these posts ;)

Enjoy, hopefully :)

b

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} Labels: backup, bpdbjobs, linux, netbackup, perl, report, reporter, reports, script, symantec, unix, veritas