How to know process is running or not?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to know process is running or not?
# 1  
Old 11-26-2009
How to know process is running or not?

please suggest me a way to write c program that takes pattern of the process and returns true or false


my case is i need to keep track of dhcpcd

dhcpcd -d eth1

so i tried popen("ps -ef | grep dhcpcd | grep eth1 | wc -l", "r");

but pipe is storing 1 or 2 when not running
and 2 or 3 when running.

please suggest a good way to do this

is there any api in linux for the same instead of doing file operations.

Last edited by Gopi Krishna P; 11-26-2009 at 02:08 AM..
# 2  
Old 11-26-2009
When the process is not running, it will return the value 2
1. popen invokes the shell to execute the given command ( that sh command also will have the given pattern)
2. grep command with the given patter

and when the process is running it will return the value >2

So one of the way to find this is,

you have to convert the data reading from the pipe to integer through atoi()
and subtract 2. Then if the result is 0 - the process is not running.
if the result is > 0 the process is running.
# 3  
Old 11-26-2009
Thanks for reply skmdu

some times when dhcpcd is not running it is returning 1 mostly and 2 rarely
and when dhcpcd is running it is returning 2 mostly and 3 some times
problem is both cases are having 2 as output some time.

not understanding the reason why

generally it would be like

if ps -ef | grep dhcpcd
it would give
1) if dhcpcd is running that entry and our command entry (grep dhcpcd)
so the number of lines should be 2
2) if not running just (grep dhcpcd) entry , so the number of lines should be 1

any reason for returning 3 when running and returning 2 when not running ?
# 4  
Old 11-26-2009
You just remove wc -l from the command and see the result of grep..

Then you will get some understanding when it is returning 2 for both the case?
# 5  
Old 11-26-2009
MySQL

Code:
ps -eo pid,ppid,cmd | grep <process_name> | grep -v grep

Replace the process_name and u can get the result as having pid in the first column, ppid as the second column and command name as the third column.

The first line probably has the ppid 1. Perhaps, if u see a second line, its ppid will be the first line's pid which means it is started from that process.

Smilie
# 6  
Old 11-26-2009
what grep -v grep indicates ?
# 7  
Old 11-26-2009
Works at least under Solaris and Gnu/Linux:
Code:
pgrep -f "dhcpd.*eth1"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies

2. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (6 Replies)
Discussion started by: naveeng
6 Replies

3. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (1 Reply)
Discussion started by: naveeng
1 Replies

4. BSD

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (0 Replies)
Discussion started by: naveeng
0 Replies

5. UNIX for Dummies Questions & Answers

Need help in running a process

Hello All, We are running a process in unix box. This is how we will do 1.our process name is Runprojectprocess <ID> configuration 2.We will be running upto 500 id at once. So we are populating a text file as Runprojectprocess 1 configuration to Runprojectprocess 500 configuration 3.... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

6. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

7. UNIX for Dummies Questions & Answers

How to tell if a process is running?

Hi all, please excuse the newby question. I run OSX servers with Filemaker and Terascript (formerly Witango, before that Tango). Terascript talks to Filemaker via JDBC so I have installed the Filemaker Xdbc extensions (JDBC, ODBC). It is a listener process that waits for JDBC commands and... (28 Replies)
Discussion started by: AusS2000
28 Replies

8. UNIX for Dummies Questions & Answers

Running different process from current process?

I have been having some trouble trying to get some code working, so I was wondering...what system calls are required to execute a different program from an already running process? (1 Reply)
Discussion started by: Midwest Product
1 Replies

9. 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

10. UNIX for Advanced & Expert Users

How to create a dummy process of a process already running?

Hi Everybody, I want to create a shell script named as say "jip" and it is runned. And i want that when i do ps + grep for the process than this jip should be shown as process. Infact there might be process with name jip which is already running. (3 Replies)
Discussion started by: shambhu
3 Replies
Login or Register to Ask a Question