Script not running as expected
My script is not working as expected.
I have a need to restart a program every hour. If the program is running when the script runs, kill it then restart it. If the program is NOT running when script runs, then just start it. Here is the script's behavior currently: The script kills the program if the program is running, but it won't restart it, until the next time the script runs. I.e., the only time it restarts the program is if it wasn't running. Why is it doing this? Here is the script... #!/bin/sh APP='ffmpeg2theora' APPID=`pidof ffmpeg2theora` if ps ax | grep -v grep | grep $APP > /dev/null then kill $APPID fi wait /usr/bin/ffmpeg2theora /dev/video0 -o /dev/stdout ... |
What did you do to debug the issue ? E.g. did you check until what line inclusive your script is executed ?
For example, did you try to put -xv after #/bin/sh ? |
Quote:
it makes no sense that the line /usr/bin/ffmpeg2theora /dev/video0 -o /dev/stdout ... only runs if the program wasn't previously running. Isn't a script supposed to run line-by-line? And that anything outside a conditional statement runs automatically? Anyway, I debugged the script the following way: $ watch cat `pidof ffmpeg2theora` When the script runs from cron, the pid changes. E.g. from 14449 to 24045 But then 24045 disappears 2 seconds later. Quite a bizarre behavior. The following seems to work for now, but I still don't know where the problem lies. Code:
#!/bin/sh /usr/bin/ffmpeg2theora /dev/video0 -o /dev/stdout ... twice, I'm getting the expected behavior. |
What is the wait for?
It seems to me that the following much simpler script would work: Code:
#!/bin/sh |
Quote:
I could use "sleep X" in place of "wait", but there is no guarantee that the line would finish executing in X seconds. Your suggestion, killall /usr/bin/ffmpeg2theora /usr/bin/ffmpeg2theora /dev/video0 -o /dev/stdout ... suffers the same problem as I'm having. It kills the running process, but does not restart, until the next time the script runs. |
Quote:
|
Quote:
|
The killall has a --wait option, perhaps that is what you are looking for. The following appears to work correctly over here:
Code:
#!/bin/sh I don't have ffmpeg2theora here, is it by any chance a shell script? That could also complicate things. |
Quote:
This is why the kill/killall command has to finish executing, before the script is allowed to proceed. |
Quote:
If you read my post carefully, I said I debugged using $ watch echo `pidof ffmpeg2theora` Did you just read the one line, and quickly ran to reply? Don't do that. You should read the entire post, and my reasoning. |
Quote:
http://www.linux.com/archive/feed/43774 |
Quote:
Quote:
|
Quote:
As for using --wait with killall, I'm reluctant to use killall, because a case may arise where I have more than two ffmpeg2theora processes running. So, I've rewritten my code as follows with grep and kill. Grep lets me find particular ffmpeg2theora processes. APP='ffmpeg2theora' APPID=`pidof ffmpeg2theora` CMD=/usr/bin/ffmpeg2theora /dev/video0 -o /dev/stdout... if ps ax | grep -v grep | grep $APP > /dev/null then kill $APPID & # backgrounding makes it a sub-process, and gives it a pid. LPID=$! #$! contains the PID of the last called process. wait $LPID $CMD else $CMD fi This seems to do what I want. Please critique for obvious flaws. |
Quote:
Code:
kill $APPID & # backgrounding makes it a sub-process, and gives it a pid. Quote:
By the way, I suggest pgrep instead of the ps | grep foo | grep -v grep thing in general. |
Quote:
FWIW, 'man killall' might be useful if one wants to kill processes by name. |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 07:42 AM. |