LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Loop for process checking (https://www.linuxquestions.org/questions/linux-newbie-8/loop-for-process-checking-938122/)

mainstream 04-04-2012 01:56 PM

Loop for process checking
 
Hello,

I'm just playing around with bash to understand it better. I've tried many different lines of code.

I'm trying to create a loop to check for process using ps aux. For this example i took sabnzbdplus and sickbeard.

I want to detect if one is running, and if not, start the program.

Code:

#!/bin/bash
PROGRAMS='sabnzbd SickBeard'
PROG1='sabnzbdplus -d'
PROG2='python ~/SickBeard/SickBeard.py'

for PROCESS in $PROGRAMS
do ps aux | grep -c $PROCESS #> /dev/null #check output (1 or 2)
if [ $? -eq 2 ]; then
  echo "Process is running ($PROCESS)"
else
  #if [ $? -eq 1 ]; then
  echo "Process is not running ($PROCESS)"
  if [ "$PROCESS" == "sabnzbd*" ]; then
  `start $PROG1`
    else if [ "$PROCESS" == "SickBeard*" ]; then
    `start $PROG2`
    fi
  fi
fi
#fi
done

The output:
Code:

mainstream@dopamine-pc:~/Desktop$ ./process.sh
2
Process is not running (sabnzbd)
1
Process is not running (SickBeard)
mainstream@dopamine-pc:~/Desktop$

I commented "> /dev/null" to check the output.
Somehow it's not working. Figure it's got something to do with [ $? -eq 2 ]

unSpawn 04-04-2012 06:08 PM

Quote:

Originally Posted by mainstream (Post 4644768)
(..) Somehow it's not working.

If a shell script doesn't work for you then make it a habit to run it with echo statements for variable assignments or run it as '/bin/bash -vx /path/to/script anyargsyouneed 2>&1 | tee /temp/path/script.tee'. This shows you the scripts contents as it runs, populated variables and errors as they happen. That's easier for you to read back and assess and easier for us to read.


Code:

#!/bin/bash --
# Unset line below when you're done troubleshooting:
set -vx
APPLICATIONS="sabnzbd SickBeard.py"
for APPLICATION in $APPLICATIONS; do
 # Unset line below when you're done troubleshooting:
 echo "Testing ${APPLICATION}: "
 # Generally speaking don't use 'ls' or 'ps' but 'find' and 'pgrep' instead.
 # Check the manual pages for details.
 # Grep process table for argv[0] and use exit status of process:
 pgrep $APPLICATION >/dev/null 2>&1; APPLICATIONSTATUS=$?
 # If the exit status is 0 then assert the application is running:
 if [ $APPLICATIONSTATUS -ne 0 ]; then
  case $APPLICATION in
  sabnzbd)
            /path/to/sabnzbdplus -d
            ;;
  SickBeard*)
            /usr/bin/python /home/mainstream/SickBeard/SickBeard.py
            ;;
  *)      # Always good to have a catchall entry:
            echo "I dont know \"${APPLICATION}\" yet."
            break
            ;;
  esac
 fi
done
# Always exit scripts the right way:
exit 0


Should you need it hnd here's some
Code:

function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }


mainstream 04-05-2012 07:05 AM

Quote:

Originally Posted by unSpawn (Post 4644920)
If a shell script doesn't work for you then make it a habit to run it with echo statements for variable assignments or run it as '/bin/bash -vx /path/to/script anyargsyouneed 2>&1 | tee /temp/path/script.tee'. This shows you the scripts contents as it runs, populated variables and errors as they happen. That's easier for you to read back and assess and easier for us to read.

Ah, ok. I didn't know that was possible! Thanks allot. I was trying, trying and trying...
I think this will help me allot in future coding.

Code:

#!/bin/bash --
# Unset line below when you're done troubleshooting:
set -vx
APPLICATIONS="sabnzbd SickBeard.py"
for APPLICATION in $APPLICATIONS; do
 # Unset line below when you're done troubleshooting:
 echo "Testing ${APPLICATION}: "
 # Generally speaking don't use 'ls' or 'ps' but 'find' and 'pgrep' instead.
 # Check the manual pages for details.
 # Grep process table for argv[0] and use exit status of process:
 pgrep $APPLICATION >/dev/null 2>&1; APPLICATIONSTATUS=$?
 # If the exit status is 0 then assert the application is running:
 if [ $APPLICATIONSTATUS -ne 0 ]; then
  case $APPLICATION in
  sabnzbd)
            /path/to/sabnzbdplus -d
            ;;
  SickBeard*)
            /usr/bin/python /home/mainstream/SickBeard/SickBeard.py
            ;;
  *)      # Always good to have a catchall entry:
            echo "I dont know \"${APPLICATION}\" yet."
            break
            ;;
  esac
 fi
done
# Always exit scripts the right way:
exit 0

So, when the app is running, the output in APPLICATIONSTATUS=0
When the app is not running the output is APPLICATIONSTATUS=1


Quote:

Originally Posted by unSpawn (Post 4644920)
Should you need it hnd here's some
Code:

function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }


Thanks i'll check it out!

unSpawn 04-05-2012 10:58 AM

Quote:

Originally Posted by mainstream (Post 4645362)
So, when the app is running, the output in APPLICATIONSTATUS=0
When the app is not running the output is APPLICATIONSTATUS=1

The exit status ($?), yes. Try this:
Code:

false; echo "false: $?"; true; echo "true: $?"


All times are GMT -5. The time now is 07:03 AM.