LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash strange subshell command result (https://www.linuxquestions.org/questions/programming-9/bash-strange-subshell-command-result-771424/)

fenor 11-25-2009 04:18 AM

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

druuna 11-25-2009 04:30 AM

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.

timmeke 11-25-2009 04:34 AM

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`
the grep process you start can appear in the output of the "ps" command, then gets through the grep and into wc (thus providing a false match).
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?

fenor 11-25-2009 04:39 AM

Many thanks for the quick answare!

fenor

jschiwal 11-25-2009 04:45 AM

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>

urban.yoga.journeys 11-25-2009 04:45 AM

you can also check the exit status of grep.

if the file exists

Code:

mo@mo-laptop:~/scripts$ ls | grep 4big.sh
4big.sh
mo@mo-laptop:~/scripts$ echo $?
0

if the file doesn't exist

Code:

mo@mo-laptop:~/scripts$ ls | grep filedoesnt
mo@mo-laptop:~/scripts$ echo $?
1

so you should be able to do

Code:

ps | grep filename
if [ $? == 0 ]; then
    echo "file found"
else
    echo "file not found"
fi


chrism01 11-25-2009 07:51 PM

For a numeric comparison, it's -eq , not '=='
http://tldp.org/LDP/abs/html/comparison-ops.html

rkski 11-25-2009 10:32 PM

Was it as simple as forgetting the space:

Code:

programtostart &

fenor 11-26-2009 01:33 AM

Quote:

Originally Posted by timmeke (Post 3769013)
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`
the grep process you start can appear in the output of the "ps" command, then gets through the grep and into wc (thus providing a false match).
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?


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

ta0kira 11-26-2009 01:44 AM

You don't actually need an extra call to wc -l if you use grep; you can use grep -c.
Kevin Barry

timmeke 11-26-2009 03:00 AM

Quote:

Originally Posted by fenor
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.

Makes sense. OpenOffice is a user application, not a background service (like httpd).
Only the latter typically use files in /var/run.

How do you use openoffice specifically? Can you post the exact command(s)?

fenor 11-26-2009 03:56 AM

Quote:

Originally Posted by timmeke (Post 3770108)
Makes sense. OpenOffice is a user application, not a background service (like httpd).
Only the latter typically use files in /var/run.

How do you use openoffice specifically? Can you post the exact command(s)?

It is started with:
Code:

soffice -headless -nofirststartwizard -accept="socket,host=localhost,port=8100;urp;"&
and the documentconverter.py script is used to convert the docs.
http://artofsolving.com/opensource/pyodconverter

fenor

timmeke 11-26-2009 07:35 AM

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
to see if port 8100 is open.

The ps approach might be better, though.


All times are GMT -5. The time now is 10:02 AM.