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 Here's the test: Code:
1 #!/bin/bash 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 :). |
If you wanted to keep the structure of the script the same, just change from = to -eq. For example:
Code:
if [ $TEST -eq 1 ] Code:
if [ $(ps ax | grep motion | grep -v grep) ] Lastly, I'd suggest a look at the Advanced Bash-Scripting Guide - it's well worth a read. :) |
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... |
Code:
#!/bin/bash |
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 The following also works on my box: Code:
#!/bin/bash |
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 |
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.
|
bash start process
Another solution:
ps ax | grep motion | grep -v grep || telinit run motion start |
Quote:
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 |
Quote:
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. |
Quote:
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. |
Quote:
Code:
if foo; then Quote:
Code:
TEST=`ps ax | grep $1 | grep -v ps | wc -l` Code:
TEST=$(ps ax | grep $1 | grep -v ps | wc -l) 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 |
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. ;) |
All times are GMT -5. The time now is 05:08 AM. |