In C Program, determine if job is running


 
Thread Tools Search this Thread
Top Forums Programming In C Program, determine if job is running
# 1  
Old 06-30-2006
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.
# 2  
Old 06-30-2006
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.
# 3  
Old 06-30-2006
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?
# 4  
Old 06-30-2006
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;
}

# 5  
Old 06-30-2006
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 Smilie ) that in general wraps routines mentioned above
# 6  
Old 06-30-2006
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.
# 7  
Old 06-30-2006
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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl : Inline program to determine file size

Hi, I have 5 files as below $ ll sam* -rw-rw-rw- 1 sam ugroup 0 Mar 21 06:06 sam3 -rw-rw-rw- 1 sam ugroup 0 Apr 3 22:41 sam2 -rw-rw-rw- 1 sam ugroup 17335 Apr 10 06:07 sam1 -rw-rw-rw- 1 sam ugroup 5 Apr 10 07:53 sam5 -rw-rw-rw- 1 sam ugroup 661 Apr 10 08:16 sam4 I want to list out... (4 Replies)
Discussion started by: sam05121988
4 Replies

2. Solaris

Cron job is not running

Hi, I have set up the crontab as follows. root@IDC4VASAPP07 # crontab -l 0-59 * * * * /var/tmp/r.sh 0-59 * * * * date >> /var/tmp/log root@IDC4VASAPP07 # r.sh is as follows. root@IDC4VASAPP07 # cat r.sh #!/bin/bash dt1=$(perl -e 'use POSIX;print strftime... (10 Replies)
Discussion started by: SunilB2011
10 Replies

3. Shell Programming and Scripting

cron job and running program

Dear experts, I have this cronjob * */2 * * * $path/supervisor.sh The supervisor script is checking another script, which initializes the final scripts in multiple directories. Note that the supervisor script runs without the cronjob. In each directory, I have the following script: ... (2 Replies)
Discussion started by: TheTransporter
2 Replies

4. Shell Programming and Scripting

determine the active processes on the system which are running since long time

Hi , Please help me shell script to determine the active processes on the system which are running since long time (2 Replies)
Discussion started by: itian2010
2 Replies

5. UNIX for Advanced & Expert Users

How to determine the number of NFS threads RUNNING on the system

Hi, Anyone can tell me how to get the number of NFS threads RUNNING on the system for Solaris 10? Someone told me for Solaris 9, the method is "echo "*svc$<svcpool" | adb -k. But, I've tried to google the method for Solaris 10 and did not find the corresponding method, please help... (1 Reply)
Discussion started by: wang.caiqi
1 Replies

6. UNIX for Dummies Questions & Answers

Cron job not running

Hi All, I am editing crontab using -e option to add a new job Below is the line 30 * * * * scriptpath This job is not executing every thirty minutes. I have checked, cron daemon is running. What did I miss? Can some one help? I am using cron shell..ksh (7 Replies)
Discussion started by: yabhi_22
7 Replies

7. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies

8. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies

9. Shell Programming and Scripting

Hw to Know the status of running JoB

Hi all, I am running a job .. and i want to know the status tht it is runnig or not .. and how can i find the jobId of my job .. I have to get it to kill my running job Pls let me know da Unix commands to do it .. i m wrking on Hp UNIX (1 Reply)
Discussion started by: ravi.sadani19
1 Replies

10. Shell Programming and Scripting

Backup with shell program and cron job.

Hi, The object of my program is to take automatic backup on daily basis to different folders. I have created the respective folders. when I execute below given shell program manually it is working perfectly and taking the backup to respective folder. #!/bin/sh #script to take backup on... (1 Reply)
Discussion started by: jarkvarma
1 Replies
Login or Register to Ask a Question