![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Determine Number of Processes Running | wayne1411 | Shell Programming and Scripting | 4 | 12-17-2007 11:06 AM |
| Running a program on boot! | D-Lexy | UNIX for Dummies Questions & Answers | 4 | 12-04-2002 01: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 12:38 AM |
| Running a compiled Program | Krebsbac | High Level Programming | 2 | 09-07-2001 01:39 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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;
}
|
|
|||
|
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? |
|
|||
|
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;
}
|
|
||||
|
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)
|
||||
| Google The UNIX and Linux Forums |
| Tags |
| linux |
| Thread Tools | |
| Display Modes | |
|
|