bash strange subshell command result
Hi,
I had to write a script that checks if a special program is running and if not then it starts that. It is an easy thing I thought and wrote something like that: count=`ps aux|grep programtostart|wc -l` if [ $count -eq 1 ]; then programtostart& fi but it dous not work. Sometimes the value of count becomes 0. I wrote than that count=`ps aux|grep programtostart >debug.txt` and run it many times on 3 different linux machines and it gaved allways the same: the result of that is unpredictable. Dous somebody know about that behavior? And now, how can I check if a program is running or not? Thanks, fenor |
Hi,
When using grep it sometimes happens that the grep statement itself is also caught, but not always. Change count=`ps aux|grep programtostart|wc -l` to count=`ps aux | grep "[p]rogramtostart"| wc -l` or count=`ps aux | grep programtostart | grep -v grep |wc -l` Hope this helps. PS: count is now 0 (zero) when the process is not found. |
Hi fenor and welcome to the LQ forums!
1/ What program are we talking about? A background service ("daemon")? A regular user program? The reason I ask is because there are mechanisms in place (like PIDs in /var/run, /etc/inittab) that can help restart programs automatically when they unexpectedly stop. 2/ In the pipeline Code:
count=`ps aux|grep programtostart|wc -l` Your debug.txt file should show you this. Bottom line, you'll need to filter more than just grep'ing the program's name. Edit: it seems druuna provides the solution to this one. 3/ Another important aspect could be logging: does the program write any output to a file? And how is it started up? Does the shell that starts it up wait for the program to finish (to log exit code and then restart it), or is it started in background? |
Many thanks for the quick answare!
fenor |
If you use "ps -C <command>", you don't need to use grep. If you don't need a count of instances, you can use the return value instead.
ps -C <command> || <start_command> |
you can also check the exit status of grep.
if the file exists Code:
mo@mo-laptop:~/scripts$ ls | grep 4big.sh Code:
mo@mo-laptop:~/scripts$ ls | grep filedoesnt Code:
ps | grep filename |
For a numeric comparison, it's -eq , not '=='
http://tldp.org/LDP/abs/html/comparison-ops.html |
Was it as simple as forgetting the space:
Code:
programtostart & |
Quote:
We use openoffice in the background to convert documents to different formats and a cron job checks if it is running or not. I looked in /var/run but ther is no file for openoffice. fenor |
You don't actually need an extra call to wc -l if you use grep; you can use grep -c.
Kevin Barry |
Quote:
Only the latter typically use files in /var/run. How do you use openoffice specifically? Can you post the exact command(s)? |
Quote:
Code:
soffice -headless -nofirststartwizard -accept="socket,host=localhost,port=8100;urp;"& http://artofsolving.com/opensource/pyodconverter fenor |
After a quick look at the web page you linked to, it seems that you are running openoffice as a service that listens to port 8100.
But it is not started using the usual /etc/init.d approach. Anyway, as it is listening to a specific port, you could use Code:
nmap -p8100 localhost The ps approach might be better, though. |
All times are GMT -5. The time now is 10:02 AM. |