LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2012, 02:56 PM   #1
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Rep: Reputation: 0
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 ]
 
Old 04-04-2012, 07:08 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,360
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by mainstream View Post
(..) 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"; }
 
Old 04-05-2012, 08:05 AM   #3
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
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 View Post
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!

Last edited by mainstream; 04-05-2012 at 08:15 AM.
 
Old 04-05-2012, 11:58 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,360
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by mainstream View Post
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: $?"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking on which port what process is running ashwini.naidu Linux - Newbie 5 01-05-2009 07:41 AM
PROCESS CHECKING USING PIDs ratul_11 General 2 02-21-2008 03:08 PM
checking network traffic by particular process groxy Linux - Networking 1 01-07-2008 03:24 PM
Process Checking Command ratul_11 Linux - General 2 12-26-2007 07:52 AM
checking for a Process reygudu Programming 3 11-21-2005 05:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration