Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 01-21-2003, 07:12 PM   #1
Registered: May 2002
Location: Shanghai, PRC
Distribution: RedHat 6.2 | 7.2 | 8 | 9|3
Posts: 81

Rep: Reputation: 15
Angry How to determine whether a PROCESS is running, by a SHELL program?

Hi all,

For instance,
[root@svr1 root]# ps -ef |grep squid
nobody 6354 1 0 Jan20 ? 00:00:00 /cache/squid/bin/squid
root 17300 17212 1 08:45 pts/1 00:00:00 grep squid
[root@svr1 root]#
I need to write an if-then BASH shell to determine if a PROCESS is running.
I take SQUID as an example.
That should be something like this:
if [squid is running]; then
{echo "good"};
echo "bad";
Please give me some guide. Thank you in advance.

Old 01-21-2003, 07:25 PM   #2
Senior Member
Registered: Jul 2001
Location: 406292E 290755N
Distribution: GNU/Linux Slackware 8.1, Redhat 8.0, LFS 4.0
Posts: 1,004

Rep: Reputation: 46
You need to look for the "Introduction to BASH Programming" on the Internet or in the links section on the forum.

If it's a process you've invoked from the command line, use the command


to see a list of process invoked from your tty. If you want to see it from a script, look at

man test

and the syntax

if [ -test condition ]; then
do something
do something else

You can compare strings using this method and return a boolean TRUE or FALSE.

Also look at parsing the output of ps.

That's as much guidance as you should need (you don't want me to write it for you surely?)

Old 01-21-2003, 07:40 PM   #3
Registered: Jul 2002
Distribution: Redhat, Gentoo, Solaris, HP-UX, etc...
Posts: 391

Rep: Reputation: 30
The answer is in your post, you just need to put it all together.

if ( `ps -ef | grep squid | grep -v grep | wc -l` -eq 1 )
echo "good"
echo "not so good"

You could also use the pgrep utility which returns the process id. As shown below.

if (pgrep squid > 0)
echo "squid is running"
echo "squid is dead"

for some reason the code tag doesn't seem to work right now.

Last edited by jdc2048; 01-21-2003 at 07:43 PM.
Old 01-21-2003, 07:54 PM   #4
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
...also on any SysV system the easy way would be to check the script that exists as /etc(/rc.d)/init.d/<servicename> with the "status" arg.

RH also got /sbin/pidof as an pgrep equiv. btw:
case $(/sbin/pidof squid 2>&1> /dev/null; echo $?) in
0) exit 0;; *) echo it aint runnin;; esac
Old 01-21-2003, 08:41 PM   #5
Registered: May 2002
Location: Shanghai, PRC
Distribution: RedHat 6.2 | 7.2 | 8 | 9|3
Posts: 81

Original Poster
Rep: Reputation: 15

Thank you all so much.
I really need to learn much more in this field...


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to read ps -e output and determine process double processes. dr_zayus69 Programming 1 09-21-2005 05:37 PM
running a program from shell script Suinatsa Programming 10 04-14-2005 11:25 AM
how to determine if a program is running? iclinux Programming 10 02-04-2005 03:12 AM
Keeping a process running after shell logout abboq Linux - Software 5 09-01-2004 05:58 PM
program to check to make sure a process is running? IceNineJon Linux - Software 7 08-06-2003 02:07 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration