Execute a C program from Shell

Hi
I want to create a shell script tha executes a C program and then retrieves information about it.

I managed to run the program with:

#!/bin/bash

gcc -o program program.c
./program

Now i want to get the id of the process (pid)
Any help would be appreciated,
Thank you

 
#!/bin/bash
gcc -o program program.c
nohup ./program &
sleep 120 #sleep for 2 mins
pid=$(ps -ef | grep program | grep -v grep | cut -d" " -f3,3)

Here we are trying to get PID of a child process in parent process. So I believe, it can be done only if child process runs in background. If process runs in foreground, ps command will execute only after child process is completed and then PID can't be obtained.
sleep is used to avoid cases when child process make take a while to get started.
~Others, Please correct if anything wrong here.

Thanks for the reply

pid=$(ps -ef | grep program | grep -v grep | cut -d" " -f2,2)
I'm not sure i understand this line.
I tried echo $pid
but it doesn't print anything.

 
ps -ef | grep process | grep -v grep

Will print details of process like

 
<UID>  <PID>  <PPID>  <C>  <STIME>  <TTY>   <TIME>  <COMMAND>
 
ps -ef | grep process | grep -v grep | cut -d" " -f2,2

will give 2nd field.
This will work for processes running in foreground.
But it looks like for background processes, there is some space at start of process detail line, so we need to get 3rd field using cut command.(Corrected in earlier post)

If you are getting nothing as output, Please post the output of

 
ps -ef | grep process | grep -v grep

The problem here is that many fields from ps are (uaually) right-justified, and so the second field from cut using -d" " will likely to be a blank.

$ ps -ef | grep [h]ttpd                
    0    45     1   0   0:15.66 ??         0:20.92 /usr/sbin/httpd -D FOREGROUND
   70   132    45   0   0:00.03 ??         0:00.05 /usr/sbin/httpd -D FOREGROUND
   70 11028    45   0   0:00.05 ??         0:00.07 /usr/sbin/httpd -D FOREGROUND
   70 22015    45   0   0:00.02 ??         0:00.03 /usr/sbin/httpd -D FOREGROUND
   70 25697    45   0   0:00.01 ??         0:00.01 /usr/sbin/httpd -D FOREGROUND
   70 25700    45   0   0:00.01 ??         0:00.01 /usr/sbin/httpd -D FOREGROUND
   70 25701    45   0   0:00.01 ??         0:00.01 /usr/sbin/httpd -D FOREGROUND
   70 25703    45   0   0:00.01 ??         0:00.02 /usr/sbin/httpd -D FOREGROUND
   70 25704    45   0   0:00.01 ??         0:00.01 /usr/sbin/httpd -D FOREGROUND
123 4     5     6.....
(fields)

$ ps -ef | grep [h]ttpd | cut -d" " -f2

(lots of blank lines)

Better to use something which doesn't force the use of a specific whitespace character, like awk '{print $2}'

$ ps -ef | grep [h]ttpd | awk '{print $2}'
45
132
11028
22015
25697
25700
25701
25703
25704

ok.. Then following should be good:

 
pid=$(ps -ef | grep program | grep -v grep | awk '{print $2}')

The only way i managed to get an output is :

pid=$(ps -ef | grep program )
echo  $pid

out: nteath 3241 3239 0 19:16 pts/0 00:00:00 grep program

This makes little sense, actually.

Unless you run "program" in the background, it will be finished before you run the ps command.

./program &
PID=$!

I tried the following:
pid=$(ps -ef | grep program | awk '{print $2}')
echo $pid

and i got a number '3348'
I assume this is my pid :slight_smile:

Could you please explain what "grep -v grep" does?

You got the PID of the grep command.

grep -v grep would remove grep from the ps output.

But it's not necessary.

ps -ef | grep [d]ummy

will also remove grep from the ps output.

The program may execute much quicker than 2 minutes. Also, I see he's using the bash shell.

Try the following:

./program &
pid=$!

This will run the program in the background and then return it's process ID.

Hey scottn,
I am new in shell scripting and i 'm a bit confused.

I execute a C program with:

gcc -o dummy dummy.c
./dummy

and then i put it to sleep with: sleep 120

While it is "sleeping" i need to get the id of the process.
Whats the right command to do that?

./program &      # run program in the background
wait             # wait for it to finish
PID=$!           # get it's process ID (if you have a use for it once the program has finished!)

Or if you only want to sleep for 120 seconds:

./program &      # run program in the background
sleep 120        # wait for 120 seconds
PID=$!           # get it's process ID

[/code]

The "wait" will halt script execution until the program completes. Just placing it in the background via the "&" will allow the script to continue. The bash "$!" environemt variable will return the process ID of the last command put in the background. No sleep. No wait. Simply execute in background and capture PID via the $! then proceed with what you need to do with the script.

./program &
wait
PID=$!
This is working but when i use
echo $PID
I get two numbers

I already mentioned this $! in post #8.

I have no idea why the O/P wants his script to sleep for 2 minutes and I'm not going to second guess it.

---------- Post updated at 06:49 PM ---------- Previous update was at 06:48 PM ----------

That hardly seems likely. $! stores the process ID of the last background job run.

Hi Scott!

I realize you first mentioned that in #8. I had been typing up #11 while you submitted that. The sleep was introduced by someone else in #2 where they were attempting to use a ps and grep pipe to get the PID. I don't believe that O/P really wants a sleep in there.

You're right. Sorry, I missed that.

So maybe the OP doesn't want to sleep. I do :smiley:

O/P: Read posts 8 and 11, and ignore everything else! (no disrespect to anurag.singh)

Perhaps knowing what you want to do with the PID once you have it might be more useful to know.

1 Like

Ok after all i got my pid with: PID=$!
Thank you :slight_smile:

---------- Post updated at 01:51 PM ---------- Previous update was at 12:55 PM ----------

I have another question ( not sure if i had to make a new thread)

Now that i have the PID i want to retrieve information about my process, using the command 'top'.

So far i have
top -b -d 1.0 -p $PID
which returns information while my process is running. When the process stops , top is still running. How can i stop it and proceed with my shell?