LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-25-2009, 05:18 AM   #1
fenor
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
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
 
Old 11-25-2009, 05:30 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 11-25-2009, 05:34 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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?
 
Old 11-25-2009, 05:39 AM   #4
fenor
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Many thanks for the quick answare!

fenor
 
Old 11-25-2009, 05:45 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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>
 
Old 11-25-2009, 05:45 AM   #6
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
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
 
Old 11-25-2009, 08:51 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
For a numeric comparison, it's -eq , not '=='
http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 11-25-2009, 11:32 PM   #8
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Was it as simple as forgetting the space:

Code:
programtostart &
 
Old 11-26-2009, 02:33 AM   #9
fenor
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by timmeke View Post
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
 
Old 11-26-2009, 02:44 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You don't actually need an extra call to wc -l if you use grep; you can use grep -c.
Kevin Barry
 
Old 11-26-2009, 04:00 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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)?
 
Old 11-26-2009, 04:56 AM   #12
fenor
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by timmeke View Post
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
 
Old 11-26-2009, 08:35 AM   #13
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
  


Reply


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
Making bash script WAIT after starting subshell mintybadger Programming 9 08-04-2011 05:49 AM
bash script and exporting variables into a subshell spork Programming 3 10-15-2008 12:38 AM
How to execute commands within a subshell opened by another command? plin Programming 3 02-11-2008 09:19 AM
Strange ip from the result of tracert command mrpc_cambodia Linux - Networking 4 01-25-2005 07:49 AM
BASH Command Result redhatnoob Programming 2 08-19-2004 07:25 PM


All times are GMT -5. The time now is 12:01 PM.

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