LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices



Reply
 
Search this Thread
Old 11-12-2006, 02:16 PM   #1
MaffooClock
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Rep: Reputation: 0
Bash script to check for dead process


My bash scripting skills are horrible (if non-existant). I've been trying for a couple of hours now to write a simple script which will check for a process, and if it's not running, start it. I can't seem to pull this off.

Code:
  1 #!/bin/bash
  2
  3 TEST=`ps ax | grep motion | grep -v grep | wc -l`
  4
  5 if [ $TEST = 0 ]
  6 then
  7     telinit run motion start
  8 fi
The ps command works: it returns 0 if the process is not running, and 1 if it is. But for some reason, the if statement seems to not be evaluating correctly, and I have no idea what to do.

Here's the test:
Code:
  1 #!/bin/bash
  2
  3 TEST=`ps ax | grep motion | grep -v grep | wc -l`
  4
  5 if [ $TEST = 1 ]
  6 then
  7     echo "Alive!"
  8 else
  9     echo "Dead :("
 10 fi
And even though the ps command correctly returns either 1 or 0, the if statement always evaluates false. Clearly, I don't know what I'm doing.

So to clarify: I run from-source Linux PC which is actually a DVR for security cameras. I use Motion as the v4l recording application. The process is 'motion'. All I want to do is check for the motion process, and if it isn't running, execute 'telinit run motion start'. Once I get this working, it'll be a cron job.

Thank you in advance for your time .
 
Old 11-12-2006, 02:59 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
If you wanted to keep the structure of the script the same, just change from = to -eq. For example:
Code:
if [ $TEST -eq 1 ]
You could just put the original commands in the test. I haven't tested my syntax, but the following should work:
Code:
if [ $(ps ax | grep motion | grep -v grep) ]
Also, once you get to the cron job stage... Cron uses a cut down execution environment, so you'll probably need to modify your script to use the full path to commands. For example, /usr/bin/ps instead of ps, etc.

Lastly, I'd suggest a look at the Advanced Bash-Scripting Guide - it's well worth a read.

Last edited by gilead; 11-12-2006 at 03:02 PM.
 
Old 11-12-2006, 04:02 PM   #3
MaffooClock
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Original Poster
Rep: Reputation: 0
I appreciate your response and am grateful for the time you took to help me, however, nothing you suggested was useable.

Your first suggestion is pointless, as = and -eq are identical. But I tried it anyway .

Your second suggestion is (as you may have already guessed) syntactically incorrect. If motion is running, $(ps ax | grep motion | grep -v grep) will return several items which causes bash to belch out a error about "too many arguments". But I got yoru point and tried my original ps command string in place of the variable, but the behavior was identical (no surprise).

As far as the cron job: I'm extremely familiar with cron, but I do appreciate the advice. But for the record: until I can get this stupid script to work, there won't be a cron job.

But, hey, the way I'm goign abotu it may not necessarily be the only way I want to do this. If ANYONE has any ideas on routinely checking for a process and restarting it, please, share . Google has not turned anything up (yet), but if I find a solution, I'll certainly close off my own thread with the solution I come to...

Last edited by MaffooClock; 11-12-2006 at 04:08 PM.
 
Old 11-12-2006, 05:58 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
#!/bin/bash
if pgrep PROCESS &>/dev/null; then
    echo alive
else
    echo dead
fi
 
Old 11-12-2006, 06:01 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
The following works on my bash shell (version 3.1.17(2)-release (i486-slackware-linux-gnu)). It prints out Dead as is, and doesn't print anything if I change the httpdz to httpd since I have Apache running:
Code:
#!/bin/bash

TEST=`ps ax | grep httpdz | grep -v grep | wc -l`

if [ $TEST -eq 0 ]
then
  echo "Dead :("
fi
Using "=" is not the same as using "-eq". According to man bash on my box, using "=" is the POSIX compliant version of using "==" for comparing strings, not numbers.

The following also works on my box:
Code:
#!/bin/bash

if [ ! "$(ps ax | grep httpdz | grep -v grep)" ]
then
  echo "Dead :("
fi
It returns Dead for httpdz and nothing for httpd while Apache is running.
 
Old 11-20-2006, 04:46 PM   #6
sensovision
Member
 
Registered: Mar 2004
Location: Ukraine
Distribution: Debian "Wheezy"
Posts: 94

Rep: Reputation: 15
hi folks! maybe I'm a bit late but maybe you would be interested in this code:
it's shorter and I think less resource consuming than grep:
Code:
#!/bin/bash

if [ ! "$(pidof httpdz)" ]
then
  echo "Dead :("
else
  echo "Alive :)"
fi
 
Old 02-10-2007, 01:04 AM   #7
vvard
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Rep: Reputation: 0
pretty nice solution sensovision, works really fine. And for those using "ps type" solution there is a lil issue with that, when u pipe the output and grep for a pattern, sometimes u can get the ps command itself on the output, and sometimes dont, so that is not a good approach.
 
Old 05-16-2007, 12:10 PM   #8
Oupa
LQ Newbie
 
Registered: Apr 2007
Location: Welkom, South Africa
Distribution: RHL + Mandriva
Posts: 15

Rep: Reputation: 0
bash start process

Another solution:

ps ax | grep motion | grep -v grep || telinit run motion start
 
Old 05-16-2007, 02:25 PM   #9
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Quote:
Here's the test:
Code:
  1 #!/bin/bash
  2
  3 TEST=`ps ax | grep motion | grep -v grep | wc -l`
  4
  5 if [ $TEST = 1 ]
  6 then
  7     echo "Alive!"
  8 else
  9     echo "Dead :("
 10 fi
This script runs fine here, without any modifications.

I do believe proper syntax is this:

TEST=`ps ax | grep motion | grep -v grep | wc -l`;
if [ $TEST = 1 ];

but my bash doesn't seem to care.

edit:

I also constructed this version, which allows you to specify the command on the command line. Also notice that I changed the test; rather than checking for a line count that equals 1 it checks for a line count greater than 1.

Now, this version sometimes returns alive and sometimes returns dead if I specify a process that has only one instance running. I put the echo statement in to see what is happening, and sometimes one line is returned, sometimes two. I have kget running so I tried it with the command "tstscript kget".

I find the result to be peculiar. Anyone here have a comment on why it happens?
Code:
#!/bin/bash
TEST=`ps ax | grep ${1} | grep -v tstscript | wc -l`;
echo $TEST;
if [ $TEST -gt 1 ];
then
    echo "Alive!"
else
    echo "Dead :("
fi

Last edited by jiml8; 05-16-2007 at 02:43 PM.
 
Old 05-17-2007, 09:41 AM   #10
vvard
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by jiml8
Now, this version sometimes returns alive and sometimes returns dead if I specify a process that has only one instance running. I put the echo statement in to see what is happening, and sometimes one line is returned, sometimes two. I have kget running so I tried it with the command "tstscript kget".

I find the result to be peculiar. Anyone here have a comment on why it happens?
Code:
#!/bin/bash
TEST=`ps ax | grep ${1} | grep -v tstscript | wc -l`;
echo $TEST;
if [ $TEST -gt 1 ];
then
    echo "Alive!"
else
    echo "Dead :("
fi
See my post above.
Try this several times on your shell $ ps | grep process where process is only 1 in the process list, do that and see what results u get.
 
Old 05-17-2007, 12:03 PM   #11
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by vvard
See my post above.
Try this several times on your shell $ ps | grep process where process is only 1 in the process list, do that and see what results u get.
Yes, well...

The question is not whether this is happening. Clearly it is. The question is WHY it is happening.

Shouldn't happen. Program should work the same way every time.
 
Old 05-18-2007, 05:36 AM   #12
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by jiml8
This script runs fine here, without any modifications.

I do believe proper syntax is this:

TEST=`ps ax | grep motion | grep -v grep | wc -l`;
if [ $TEST = 1 ];

but my bash doesn't seem to care.
That's not the proper syntax - the semi-colon is an alternate for a newline. It's completely useless at the end of TEST and, while you will often see
Code:
if foo; then
  bar
fi
the semi-colon is there because bash is expecting either it or a newline before the 'then' - putting both a semi-colon and a newline is redundant.

Quote:
edit:

I also constructed this version, which allows you to specify the command on the command line. Also notice that I changed the test; rather than checking for a line count that equals 1 it checks for a line count greater than 1.

Now, this version sometimes returns alive and sometimes returns dead if I specify a process that has only one instance running. I put the echo statement in to see what is happening, and sometimes one line is returned, sometimes two. I have kget running so I tried it with the command "tstscript kget".

I find the result to be peculiar. Anyone here have a comment on why it happens?
Code:
#!/bin/bash
TEST=`ps ax | grep ${1} | grep -v tstscript | wc -l`;
echo $TEST;
if [ $TEST -gt 1 ];
then
    echo "Alive!"
else
    echo "Dead :("
fi
ps has an annoying habit of sometimes matching itself and sometimes not - I can't recall the precise reason - perhaps simply a sort of race condition - but it does. Hence the 'grep foo | grep -v ps' dance. Another way to avoid this is to use 'pgrep' which never matches itself.

Code:
TEST=`ps ax | grep $1 | grep -v ps | wc -l`
Also, it would more properly be
Code:
TEST=$(ps ax | grep $1 | grep -v ps | wc -l)
I type backticks in most interactive cases because it's quicker, but I type the $(...) notation in scripts - it's POSIX and nests better.

And, while we're on syntactical matters, the brace notation for variables (${1}) while always permissable is only required when there's ambiguity about where the variable leaves off -
Code:
:cat demo
#!/bin/bash
foo=bar
echo $foobar
echo ------
echo ${foo}bar

:./demo

------
barbar
the first references a nonexistent variable 'foobar' because foobar is a 'word'. The braces tell bash 'foo' is the variable and 'bar' is a literal string.

Last edited by slakmagik; 05-18-2007 at 05:39 AM. Reason: very tired
 
Old 05-18-2007, 06:14 AM   #13
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I'd have followed up sooner to correct an omission but half the net seems to have disappeared, including this site for a bit - weird DNS issues.

Anyway, obviously you need the braces for things like arrays and parameter expansion and so on. So my 'only required' was a misstatement, because I was speaking in too narrow a focus.

And I turned up this from the bashFAQ - doesn't shed much more light on it, but corroborates.
 
  


Reply

Tags
bash, check, process, restart, script


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
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 02:18 PM
Bash: check pid for active process (FreeBSD) Harpune Programming 2 05-16-2006 02:33 PM
Simple check for process, bash BruceC Programming 9 02-09-2006 04:57 AM
How to find out whether a process is dead or alive from within a shell script file walwali Linux - Software 2 12-24-2005 02:39 PM
script to check if process is dead or running rspurlock *BSD 6 04-13-2004 12:32 AM


All times are GMT -5. The time now is 08:48 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration