The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Determine Number of Processes Running wayne1411 Shell Programming and Scripting 4 12-17-2007 12:06 PM
Running a program on boot! D-Lexy UNIX for Dummies Questions & Answers 4 12-04-2002 02:14 PM
Running a program perleo UNIX for Dummies Questions & Answers 3 08-23-2002 07:34 AM
Running a program automatically jvadn0 Shell Programming and Scripting 3 03-12-2002 01:38 AM
Running a compiled Program Krebsbac High Level Programming 2 09-07-2001 01:39 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-30-2006
Registered User
 

Join Date: May 2005
Posts: 48
In C Program, determine if job is running

Is it possible to run the following Unix command in a C program and obtain results as to whether the job is running or not?

ps -ef | grep 'job_name' | grep -v grep

I'm fairly new at C and need to know what code I'd need in the C program.

Thanks, in advance, for your assistance.
Reply With Quote
Forum Sponsor
  #2  
Old 06-30-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
Code:
#include <stdlib.h>
#include <stdio.h>

void run_cmd(char *cmd)
{
	FILE *shellcommand=popen(cmd,"r");
	char tmp[256]={0x0};
	if(shellcommand==NULL)
	{
		perror("command failure");
		exit(EXIT_FAILURE);
	}
	while(fgets(tmp,sizeof(tmp),shellcommand) )
	{
		printf("%s", tmp);
	}
	pclose(shellcommand);
}


int main()
{
	run_cmd("ps -ef | grep 'job_name' | grep -v grep");
    return 0;
}
popen and pclose let you read or write from/to a shell comand run as a child process.
Reply With Quote
  #3  
Old 06-30-2006
Registered User
 

Join Date: May 2005
Posts: 48
After running the run_cmd how do I...?

Thanks for your reply Jim but I have another question.

What C commands would I use to know if the run_cmd obtained data, or not? i.e. - If it was running, how would I know? If it wasn't, how would I know that?
Reply With Quote
  #4  
Old 06-30-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
Change run_cmd to return 1 on success and 0 on failure

Code:
int  run_cmd(char *dest, char *cmd)
{
             int retval=0; 
	FILE *shellcommand=popen(cmd,"r");
	char tmp[256]={0x0};
    
             *dest = 0x0;
	if(shellcommand==NULL)
	{
		perror("command failure");
		exit(EXIT_FAILURE);
	}
	while(fgets(tmp,sizeof(tmp),shellcommand) )
	{
                          retval=1 /* means we found at least one */  
                          strcat(dest, tmp);
	}
	pclose(shellcommand);
             return retval;
}

int main()
{
    char result[1024]={0x0};
    if (run_cmd(result, "ps -ef | grep 'job_name' | grep -v grep")==1)
    {
          printf("%s", result);
    }
    return 0;
}
Reply With Quote
  #5  
Old 06-30-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
pure C

If you want 'pure' C approach look into ps.c source code. The simpliest approach is to read /proc directory and its subdirectories /proc/nnnn (via readdir())
Or use kvm_getprocs() (note that this function is available under BSD (may be under other Unix) but not under Linux ) that in general wraps routines mentioned above
Reply With Quote
  #6  
Old 06-30-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
A lot of folks here - their flavors of unix - do not support the /proc directory.
Which, if /proc were there, would be a great help.
Reply With Quote
  #7  
Old 06-30-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
I dont think that executing the shall command "ps -ef | grep 'job_name' | grep -v grep" is the best idia to solve this task. There are must be appropriate solution in C. (I've used Linux in general and prefer to use /proc)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0