![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Difference grep, egrep and fgrep | ravind27 | UNIX Desktop for Dummies Questions & Answers | 2 | 06-14-2009 07:37 AM |
| monitoring running processes | nhatch | Shell Programming and Scripting | 2 | 12-21-2007 09:03 AM |
| Can we grep a list of all running PIDs in a file !!?? | marconi | Shell Programming and Scripting | 1 | 12-20-2007 04:48 PM |
| how to find all processes that are running | speedieB | UNIX for Dummies Questions & Answers | 9 | 07-12-2006 05:04 AM |
| grep/awk/egrep? | whugo | UNIX for Dummies Questions & Answers | 3 | 01-20-2006 12:51 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
need to grep or egrep the running processes in C file
Hi,
I want to find if specific process are running at any given moment in C? please tell me how do I do this. Currently I am trying to save these process ids in a file and check it. But this is not a foolproof method. thanks in advance. Jimmy |
|
||||
|
Try man getutent
This function reads any file in "utmp" format -- the /etc/utmp file has the recent entries. The utmp struct has information about pids currently running. You can periodically open, read /etc/utmp, then close it to find what processes are currently running. |
|
||||
|
try man getutxent - this should be on your system. It automatically opens a file:
/etc/utmp /etc/utmpx or /var/adm/wtmp /etc/utmp may not exist depending on how you set up the system. If you disabled any accounting, then it may not exist. Also try using popen and a shell script - I just made up a function foo() - you get to change it and the command it uses: Code:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
pid_t foo(char *pid_to_find)
{
char cmd[296]={0x0}; /* 40 + 256 */
char tmp[256]={0x0};
FILE *in=NULL;
int retval=0;
pid_t the_pid_I_found=0;
snprintf(cmd,sizeof(cmd)-40,
"ps -ef | grep %s | awk '{print $2}'",pid_to_find);
in=popen(cmd, "r");
if(in==NULL)
{
perror("Shell execution error");
exit(EXIT_FAILURE);
}
while(fgets(tmp,4096,in)!=NULL)
{
the_pid_I_found=atol(tmp);
break;
}
retval=pclose(in);
return the_pid_I_found;
}
|
![]() |
| Bookmarks |
| Tags |
| grep or |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|