Script to see if a process is running or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to see if a process is running or not
# 1  
Old 12-08-2010
Script to see if a process is running or not

Hi, I'm trying to make a little script to see if a process is running or not, the problem is...it always says it's running.

It's probably fault of the script itself, because ps -ef lists the arguments I send to the script

if ps -ef | grep -q $process
then
echo " process is running"

That's how I find if a process is running. I always get "pocess is running" no matter how stupid I make up the name of the process. It's always shown twice, one in /bin/bash and the other in grep.

Any tips?

edit: Also, I'd gladly accept a nice bash scripting tutorial that starts from scratch if anyone knows a good one.
# 2  
Old 12-08-2010
Code:
#!/bin/ksh

process='[f]oobar'

if ps -ef | grep -q "$process"
then
    echo " process is running"
fi

# 3  
Old 12-08-2010
In this case, since the actual process name is hidden in a variable, I'd go with adding a second pipe to your statement like so:

Code:
 
pe -ef | grep -q $process | grep -v grep

The -v option tells grep to ignore results, therefore you're telling it to ignore "grep" processes.

Or ... you could do this (ksh - don't know what shell you've got) :

Code:
 
if (( $(ps -ef | grep -c $process) > 1 ))

This accounts for the fact that you'll always get the "grep" process. But if you ever don't get it for whatever reason, you'll get a false miss. I don't like relying on this.

I've seen some folks when displaying actual process names and not putting the name in a variable do this:

Code:
 
ps -ef | grep [m]yprocess

The bracket expression somehow precludes the grep process from showing up in the reults. As I say "somehow" means I don't understand why it works, and until I do, I don't trust it. ;-)
This User Gave Thanks to rwuerth For This Post:
# 4  
Old 12-08-2010
Uhh...Just noticed, doesn't work with bash (my bad, I forgot mentioning that I was working with bash).

It just stores '[f]oobar' in that variable and doesn't find anything that is running.

Edit: Thanks rwuerth! I added one more pipe and got it done (actually I added 2 more pipes since I needed to remove also the name of the script executing or else it'll also show the argument given, which leads to failure since the argument would be recognised as a process running).

Last edited by lamachejo; 12-08-2010 at 06:29 PM..
# 5  
Old 12-08-2010
Quote:
Originally Posted by rwuerth
Code:
 
ps -ef | grep [m]yprocess

The bracket expression somehow precludes the grep process from showing up in the reults. As I say "somehow" means I don't understand why it works, and until I do, I don't trust it. ;-)

Bracket notation denotes a list of single characters to match.
For example "grep [cfh]at" would match:
Code:
cat
fat
hat

so "grep [b]ind" matches
Code:
root      201     1  0  09:28:09      -  0:05 /sbin/bind -d -x

but not
Code:
chubler  2928  2901  0  11:10:09  pts/4  0:00 grep [b]ind

.
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 12-09-2010
i think pgrep should match your needs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find out process name that are running for last 10 days.

Hi all, As i am new to shell script.Please help me to write a Shell script to find out process name that are running for last 10 days. Thank's in advance. (8 Replies)
Discussion started by: manas_1988
8 Replies

2. Shell Programming and Scripting

ksh script to check if certain process is running

I have to kill the process "test" for a maintenance I do but want the script to check when it comes back up. I can get what I want when I run this while loop: while true;do ps -ef | grep test | grep -v grep | sed -e 's/^*//';sleep 60;done but I want the script to do it for me and as soon as... (6 Replies)
Discussion started by: seekryts15
6 Replies

3. Shell Programming and Scripting

Script to kill a process running at a certain port

Hello, I have multiple scripts (vlc1, vlc2,...vlc5) and as I do not know how to run them as upstart processes, I entered my script links into rc.local file. Here is the sample one for process vlc1: $ nano /etc/rc.local added below line into rc.local /var/bin/./vlc1 & Port nr of vlc1... (7 Replies)
Discussion started by: baris35
7 Replies

4. Shell Programming and Scripting

Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (9 Replies)
Discussion started by: blacksteel1988
9 Replies

5. Shell Programming and Scripting

Simple Script to Check running Process

#!/bin/sh CHECK='ps aux | grep start.jar | grep -v grep | wc -l' if then /usr/local/jre-1.7.0/bin/java - jar start.jar & else fi Could anybody advise whats up with this code im trying to put this in as a cron job to check that solr search engine is running every 10secs and if... (10 Replies)
Discussion started by: will_123
10 Replies

6. Shell Programming and Scripting

Script to check running of process

Hi, Can anyone please tell me how to write a shell script to check whether a process if running or not.... if its still running then wait for sometime and if not then run the next query. Also, Under my one main script main.sh I have to run 2 scripts simutaneously which take some time to... (2 Replies)
Discussion started by: lovepujain
2 Replies

7. Shell Programming and Scripting

How to write an expect script to check if a process is running?

I'm new to expecting and i want to create a script to ssh to a device,check is a process is running and display that the process is running or not.This is what i have so far After executing this script i get an error. #!/usr/bin/expect set timeout -1 set ip "machine ip goes here" set... (5 Replies)
Discussion started by: icchi
5 Replies

8. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

9. Shell Programming and Scripting

script to check if process is running

How do I make a shell script to see if a certain process is running. The process shows up on ps aux as /usr/sbin/daemon eg: if /usr/sbin/daemon else #nothin basically i want to run the process if it isnt running/ has been stopped. Thanks. (2 Replies)
Discussion started by: daydreamer
2 Replies

10. Shell Programming and Scripting

script to check for a particular process and alert if its not running

Hai Friends, I have to develop a script that checks for a particular process say x.exe once every day and report if its not running say through a blat mail. I need this to run in a given list of pc from a single point. Any suggestion of how to go abt this Thnx for any help (1 Reply)
Discussion started by: goks
1 Replies
Login or Register to Ask a Question