Results 1 to 9 of 9
This is a sample script I have:
#!/bin/bash
pidFile=/tmp/.NDSP.pid
if !(test -f $pidFile)
then
#find the process id from the process name
ps -ef | grep NDSP | grep -v ...
- 07-17-2007 #1Just Joined!
- Join Date
- Jul 2007
- Posts
- 5
Bash script help
This is a sample script I have:
#!/bin/bash
pidFile=/tmp/.NDSP.pid
if !(test -f $pidFile)
then
#find the process id from the process name
ps -ef | grep NDSP | grep -v grep
if (test $? -eq 0)
then
echo "Process already running"
else
echo "Process not running. O.k. to Start server"
fi
fi
When I run the ps -ef | grep NDSP | grep -v grep; echo $? from the cmd line, it returns a 0 if the process is running and 1 otherwise.
However, when i execute the script as: bash -x ./StartNDSP, it shows the following even when the process is not running:
+ ps -ef
+ grep NDSP
+ grep -v grep
+ grep -v bash
+ ps -ef
+ grep NDSP
+ grep -v grep
vrrapadm 22012 4690 0 21:21 pts/1 00:00:00 bash -x ./StartNDSP
+ test 0 -eq 0
+ echo 'Process already running'
Process already running
+ exit 1
## I can get around this by adding: a grep -v bash to the ps -ef | grep NDSP | grep -v grep | grep -v bash. But is that the right way or how do I get around the shell spawned by the script
- 07-17-2007 #2You'll want to use square whatchacallid, these [ ] instead of ( )Code:
pidFile=/tmp/.NDSP.pid if !(test -f $pidFile) then #find the process id from the process name ps -ef | grep NDSP | grep -v grep if (test $? -eq 0) then echo "Process already running" else echo "Process not running. O.k. to Start server" fi fi
Perhaps rewriting the rest a bit might help
Still, the script doesn't contain instructions on what to do when $pidFile exists. I'll leave that up to youCode:pidFile=/tmp/.NDSP.pid if [ ! -f $pidFile ] ; then # Look for the existence of the NDSP-process PROCESS=`ps -ef | grep NDSP | grep -v grep` if [ $PROCESS ] ; then echo "Process already running" else echo "Process not running. O.k. to Start server" fi fi
PS: I haven't tested, so it's academic as far as I'm concerned.Can't tell an OS by it's GUI
- 07-17-2007 #3Just Joined!
- Join Date
- Jul 2007
- Posts
- 5
Thanks for your response. I had intentionally left out the "if file exists" part. That works. This was only for the file does not exist part.
The OS is Linux (Red Hat 3.4.4-2).
I tried your suggestion and this is what I got when I executed the script as
bash -x ./StartNDSP (i.e when the pid file did not exist and the process was running)
bash -x ./StartNDSP
++ ps -ef
++ grep NDPS
++ grep -v grep
+ PROCESS='vrrapadm 22613 4690 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP
vrrapadm 22614 22613 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP'
+ '[' vrrapadm 22613 4690 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP vrrapadm 22614 22613 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP ']'
./StartNDSP: line 17: [: too many arguments
+ echo 'Process not running. O.k. to Start server'
Process not running. O.k. to Start server
##As you see, the debug says that the process is not running and it's o.k .to start, though the process is running.
I think it may be the bash shell's that are the culprit.
- 07-17-2007 #4
Indeed.
Do you have the 'pidof' utility available? This determines the PID of a given program. Check its man page for the full docs, but this will simplify your issue incredibly.DISTRO=Arch
Registered Linux User #388732
- 07-17-2007 #5
This cures the script. Told ya I didn't test before posting

Code:PROCESS=`ps -ef | grep -v grep | grep -c NDPS`
Can't tell an OS by it's GUI
- 07-19-2007 #6Just Joined!
- Join Date
- Jul 2007
- Posts
- 5
Thanks but the probelm encountered after trying "PROCESS=`ps -ef | grep -v grep | grep -c NDPS`" is that when the file is not there and the process is not up, the script still reports that the "Process is already running". It still reports the PROCESS count as a positive number.
bash -x ./StartNDSP
+ pidFile=/tmp/.NDSP.pid
+ '[' '!' -f /tmp/.NDSP.pid ']'
++ ps -ef
++ grep -v grep
++ grep -c NDSP
+ PROCESS=2
+ '[' 0 -eq 0 ']'
+ echo 'Process already running'
Process already running
+ exit 1
- 07-19-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
Possibly you have a typo in your script, can you post the script between code brackets?
Regards
- 07-20-2007 #8Yeah, well, it still finds itself running. How to fix that depends on how you call the entire piece of script. It may or may not do this when run as a whole.
Originally Posted by STJ
Anyway, this works:
#!/bin/bash
Code:# Generic File/Process finder # Version 0.01.1 Beta # Date 20-07-2007 # pidFile=~/tst/$1 #Adjust path as appropriate if [ ! -f $pidFile ] ; then # Look for the existence of the $1-process echo Physical file not found on drive.... checking processes PROCESS=`ps -ef | grep -v grep | grep -v $0 | grep -c $1` if [ $PROCESS = 0 ] ; then echo "Process not running. O.k. to Start server" else echo "Process already running" fi else echo Found fiCan't tell an OS by it's GUI
- 07-25-2007 #9Just Joined!
- Join Date
- Jul 2007
- Posts
- 5
Yes, the grep -v $0 addition works. Thx, Freston


Reply With Quote