Testtube Posted January 5, 2010 Report Posted January 5, 2010 (edited) #!/bin/bash ############################################################################### # This script checks the pid of a program and if its greater than 0 its # # running If not it executes the program # # # # # # # ############################################################################### # Author information ######################################################## ############################################################################### # # # Testtube 12/30/2009 # # For General use # # # # # ############################################################################### # Change log # ############################################################################### ## # ## 1/5/2010 Initial release of code. # ## # ## # ## # ## # ## # ############################################################################### # Variable Section ########################################################## PIDOF=`pidof konsole` #name of program PROGRAM=/home/ofprogram #set where the program lives ############################################################################### if [ ${PIDOF} -gt "0" ]; then echo Program is running else echo Program is not running Program is being started #$PROGRAM fi Edited January 5, 2010 by Testtube Quote
»Maverick Posted January 5, 2010 Report Posted January 5, 2010 haha you probably spent more time on that header thing then the code, right? Quote
Testtube Posted January 5, 2010 Report Posted January 5, 2010 (edited) If you wanted to get a bit more hard core with it.this writes to a logall log file and also sends it to you via email. #!/bin/bash ############################################################################### # This script checks the pid of a program and if its greater than 0 its # # running If not it executes the program # # # # # # # ############################################################################### # Author information ######################################################## ############################################################################### # # # Ryan C 12/30/2009 # # For General use # # # # # ############################################################################### # Change log # ############################################################################### ## # ## 1/5/2010 Initial release of code. # ## # ## # ## # ## # ## # ############################################################################### # Variable Section ########################################################## PIDOF=`pidof pico` #name of program to monitor PROGRAM='pico' #set where the program lives LOG_FILE=logall.`date +%m%d%y` ############################################################################### { if [ ${PIDOF} -gt "0" ]; then echo "$PROGRAM is running." >> ${LOG_FILE} else echo "$PROGRAM is not running $PROGRAM is being started." >> ${LOG_FILE} $PROGRAM fi } WRITE_LOG_HEADER () { echo "# Begin WRITE_LOG_HEADER function" # echo "###############################################################################" >> ${LOG_FILE} # echo "# TRANSFER LOG #" >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "Transfer log for `date`" >> ${LOG_FILE} echo >> ${LOG_FILE} # echo >> ${LOG_FILE} echo "# End WRITE_LOG_HEADER function" } # # WRITE_LOG_FOOTER () { echo "# Begin WRITE_LOG_FOOTER function" echo >> ${LOG_FILE} echo >> ${LOG_FILE} echo >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "# TRANSFER LOG COMPLETED #" >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "Transfer log for `date`" >> ${LOG_FILE} echo >> ${LOG_FILE} echo >> ${LOG_FILE} echo "# End WRITE_LOG_FOOTER function" } # # SEND_LOG () { echo "# Begin SEND_LOG function" cat ${LOG_FILE} | mail -s "Transfer Log: ${LOG_FILE}" billy@aol.com echo "# End SEND_LOG function" } WRITE_LOG_HEADER WRITE_LOG_FOOTER SEND_LOG Edited January 5, 2010 by Testtube Quote
»Maverick Posted January 5, 2010 Report Posted January 5, 2010 wow chill out, I was only joking hehe, seriously, nifty stuff Quote
Testtube Posted January 5, 2010 Report Posted January 5, 2010 actually some of that code was a first time for me. Other i pasted from another script from my toolbox. Quote
L.C. Posted January 5, 2010 Author Report Posted January 5, 2010 Great stuff! Thanks guys. These fabulous scripts I shall entreasure on my storage HDD. Quote
L.C. Posted January 6, 2010 Author Report Posted January 6, 2010 (edited) Kind of hard to chose which one to use, since they all basically do the same thing. I might be incorrect in my examination of all these scripts, but it does not appear -- by code -- that any of them have flawless support for multiple instances of the same program of different execution locations. If multiple users run the same server program, how would the script tell apart all the duplicate entries (each with a unique PID) from the specific execution it is supposed to monitor? Doc flabby's script does it alright (since it is leeched to the specific location+program it is running), except I cannot get it to run. Monit looks like an excellent program to use -- based on its details, it looks like it meets and exceeds. Though it requires total installation (I am looking for a script). If you run multiple subgame2.exe's on Linux (via Wine), how would the script tell it apart from other instances of subgame2.exe, regardless of which user they might be running on? This is the question. Edited January 6, 2010 by L.C. Quote
Dr Brain Posted January 6, 2010 Report Posted January 6, 2010 That's because cron isn't the way to go. You want something more along the lines of run-asss. Quote
»doc flabby Posted January 6, 2010 Report Posted January 6, 2010 bluetds2@krslynx:~/5768_dirserver$ ./auto-bash: ./auto: /bin/bash^M: bad interpreter: No such file or directoryThat error indicates that it cant find "bash" shell try changing the first line to instead: #!/bin/sh Quote
L.C. Posted January 23, 2010 Author Report Posted January 23, 2010 (edited) bluetds2@krslynx:~/5768_dirserver$ ./auto-bash: ./auto: /bin/bash^M: bad interpreter: No such file or directoryThat error indicates that it cant find "bash" shell try changing the first line to instead: #!/bin/sh Same error. I did cd /bin/, then dir. Both sh and bash are there. EDIT: I tried bash and sh commands:This is doc_flabby's script:bluetds2@krslynx:~$ sh 4990_dirserver.sh: No such file or directorycd: /home/bluetds2/4990_dirserver'990_dirserver.sh: line 3: syntax error near unexpected token `'990_dirserver.sh: line 3: `for (( ; ; )) bluetds2@krslynx:~$ bash 4990_dirserver.sh: No such file or directorycd: /home/bluetds2/4990_dirserver'990_dirserver.sh: line 3: syntax error near unexpected token `'990_dirserver.sh: line 3: `for (( ; ; )) This is TestTube's script:bluetds2@krslynx:~$ bash 4990_dirserver.tt: command not foundline 30:'990_dirserver.tt: line 38: syntax error near unexpected token `'990_dirserver.tt: line 38: `fi bluetds2@krslynx:~$ sh 4990_dirserver.tt: command not foundline 30:'990_dirserver.tt: line 38: syntax error near unexpected token `'990_dirserver.tt: line 38: `fibluetds2@krslynx:~$ As far as crontab goes, I figured out that I had to do crontab -u bluetds2 crontab (where the last 'crontab' is a file at ~/) to get Cron started. Lynx script is working atm. I made this reply to this old thread because I found ds1.krslynx.com offline, and also found some open time to boot it back up and try some other scripts here. Edited January 23, 2010 by L.C. Quote
»doc flabby Posted January 23, 2010 Report Posted January 23, 2010 bluetds2@krslynx:~/5768_dirserver$ ./auto-bash: ./auto: /bin/bash^M: bad interpreter: No such file or directoryThat error indicates that it cant find "bash" shell try changing the first line to instead: #!/bin/sh Same error. I did cd /bin/, then dir. Both sh and bash are there. I've found our what's causing the error. You need to save the file in UNIX format to remove the the Windows Line endings. More info:http://www.linuxforums.org/forum/redhat-fedora-linux-help/65469-bad-interpreter-no-such-file-directory.html the command: dos2unix should do this for you Quote
L.C. Posted January 23, 2010 Author Report Posted January 23, 2010 (edited) bluetds2@krslynx:~/5768_dirserver$ ./auto-bash: ./auto: /bin/bash^M: bad interpreter: No such file or directoryThat error indicates that it cant find "bash" shell try changing the first line to instead: #!/bin/sh Same error. I did cd /bin/, then dir. Both sh and bash are there. I've found our what's causing the error. You need to save the file in UNIX format to remove the the Windows Line endings. More info:http://www.linuxforums.org/forum/redhat-fedora-linux-help/65469-bad-interpreter-no-such-file-directory.html the command: dos2unix should do this for youOh heh. I use Notepad++, and it has a function to convert to Windows, Mac, and UNIX. Let me try your script here. EDIT: doc_flabby's script works. Now trying TestTube's. bluetds2@krslynx:~$ bash 4990_dirserver.tt4990_dirserver.tt: line 33: [: -gt: unary operator expected -- Snrrrub's Directory Server 1.0 started --Press CTRL+C to exit.Shutting down the directory server.# Begin WRITE_LOG_HEADER function# End WRITE_LOG_HEADER function# Begin WRITE_LOG_FOOTER function# End WRITE_LOG_FOOTER function# Begin SEND_LOG function# End SEND_LOG function (Did CTRL+C to shut it down.) Though due to that -gt error, the script might not actually function correctly. I am not sure if it was supposed to allow me to completely shutdown when I did CTRL+C, or restart the program or something. Or perhaps this script is mean to be done as Cron -- it would first check to see if said process is running, and if so, terminate self (otherwise it would launch dirserver). EDIT2: Recap of scripts (all configured too) L Y N X script:#/usr/bin/python #Python Script for L.C. #Saturday Jan 2nd 2010, 23:24 #by Christopher Burke krslynx@gmail.com #2010/Jan/02 - Made edits, made it easier to understand """To Run This Script: open utilities->terminal and execute: $ python /home/bluetds2/4990_dirserver.py You will need to check that processName variable matches the process that you're _running_, and you will need to make sure that the correct path to the process that you're trying to _run_ is located within the path field on line 23""" import os import sys import subprocess processName = "dirserver" # the process to check def runProcess(): # this function will run the below application # If you want to change what application it opens, change the below path subprocess.Popen(['/home/bluetds2/4990_dirserver/dirserver' ]) # If you want a script that checks multiple directory servers # (I've got no idea why you'd need this) then I guess that can be arranged. def checkProcess(): """ This function will effectively run ps -A | grep <appname> in the shell - therefore it will be checking if processName is running so for multiple instances, you'll need to edit the processName variable to the closest path possible.""" for line in os.popen("ps -A | grep " +processName): fields = line.split() # This is splitting the output of the above pid = fields[0] # and this is grabbing the leftmost output (pid) print "pid for", processName, 'is', pid # this is non-essential check = os.path.exists("/proc/" +pid) # this part of the script is # checking if a /proc/<pid> folder has been created (ie. if the proc- # ess exists) if check == True: # if the check is true... print processName, "was running, goodbye!" # it was running exit() # so exit else: # otherwise, run the runProcess function, found at line: 12 runProcess() """MAIN""" print "Checking if dirserver is running..." # This line is non-essential checkProcess() # check process runProcess() # run process, check process exits the script first anyway... doc_flabby script:#!/bin/sh cd ~/4990_dirserver for (( ; ; )) do ./dirserver > logall.txt done Testtube script:#!/bin/bash ############################################################################### # This script checks the pid of a program and if its greater than 0 its # # running If not it executes the program # # # # # # # ############################################################################### # Author information ######################################################## ############################################################################### # # # Ryan C 12/30/2009 # # For General use # # # # # ############################################################################### # Change log # ############################################################################### ## # ## 1/5/2010 Initial release of code. # ## # ## # ## # ## # ## # ############################################################################### # Variable Section ########################################################## PIDOF=`pidof dirserver` #name of program to monitor PROGRAM='./4990_dirserver/dirserver' #set where the program lives LOG_FILE=logall.`date +%m%d%y` ############################################################################### { if [ ${PIDOF} -gt "0" ]; then echo "$PROGRAM is running." >> ${LOG_FILE} else echo "$PROGRAM is not running $PROGRAM is being started." >> ${LOG_FILE} $PROGRAM fi } WRITE_LOG_HEADER () { echo "# Begin WRITE_LOG_HEADER function" # echo "###############################################################################" >> ${LOG_FILE} # echo "# TRANSFER LOG #" >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "Transfer log for `date`" >> ${LOG_FILE} echo >> ${LOG_FILE} # echo >> ${LOG_FILE} echo "# End WRITE_LOG_HEADER function" } # # WRITE_LOG_FOOTER () { echo "# Begin WRITE_LOG_FOOTER function" echo >> ${LOG_FILE} echo >> ${LOG_FILE} echo >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "# TRANSFER LOG COMPLETED #" >> ${LOG_FILE} # echo "###############################################################################" >> ${LOG_FILE} # echo "Transfer log for `date`" >> ${LOG_FILE} echo >> ${LOG_FILE} echo >> ${LOG_FILE} echo "# End WRITE_LOG_FOOTER function" } # # SEND_LOG () { echo "# Begin SEND_LOG function" cat ${LOG_FILE} | mail -s "Transfer Log: ${LOG_FILE}" admin@hlrse.net echo "# End SEND_LOG function" } WRITE_LOG_HEADER WRITE_LOG_FOOTER SEND_LOG Crontab file (must be reloaded via crontab -u every time it is updated):# m h dom mon dow command * * * * * python /home/bluetds2/4990_dirserver.py For some reason L Y N X script is not working correctly any more. It was working previously before I commented out the line in my file 'crontab' and reloaded it with crontab -u bluetds2 crontab. Manually executing 4990_dirserver.py works fine, but when I have Cron do it it is unable to start it by saying... python: can't open file '/home/bluetds2/4990_dirserver.py ': [Errno 2] No such file or directory Edited January 23, 2010 by L.C. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.