Execute a C program from Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute a C program from Shell
# 1  
Old 12-21-2010
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:
Code:
#!/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

Last edited by Franklin52; 12-21-2010 at 02:35 PM.. Reason: Please use code tags
# 2  
Old 12-21-2010
Code:
 
#!/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.

Last edited by anurag.singh; 12-21-2010 at 12:47 PM..
# 3  
Old 12-21-2010
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.
# 4  
Old 12-21-2010
Code:
 
ps -ef | grep process | grep -v grep

Will print details of process like
Code:
 
<UID>  <PID>  <PPID>  <C>  <STIME>  <TTY>   <TIME>  <COMMAND>

Code:
 
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
Code:
 
ps -ef | grep process | grep -v grep

# 5  
Old 12-21-2010
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.

Code:
$ 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}'
Code:
$ ps -ef | grep [h]ttpd | awk '{print $2}'
45
132
11028
22015
25697
25700
25701
25703
25704

# 6  
Old 12-21-2010
ok.. Then following should be good:
Code:
 
pid=$(ps -ef | grep program | grep -v grep | awk '{print $2}')

# 7  
Old 12-21-2010
The only way i managed to get an output is :
Code:
pid=$(ps -ef | grep program )
echo  $pid

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


Last edited by Franklin52; 12-21-2010 at 02:36 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Program Does not execute

Hi there, When I am trying to execute any shell script. The shell script only execute line 1 and I notice that the rest o the program was not executed. Please advise. # bash +x vmscript-4.sh Even when I enter this command there is not output. sudo su cd /tmp cp... (2 Replies)
Discussion started by: alvinoo
2 Replies

2. Ubuntu

I can't execute a C++ program, help!

My professor gave me a code with no errors. When I compile it's fine, it doesn't show any errors, but when I try to execute it shows this: line 3: syntax error near unexpected token '(' line 3: 'int main()' I searched through the Internet but I couldn't find any solution. Please!!!... (1 Reply)
Discussion started by: rosiiieee
1 Replies

3. Shell Programming and Scripting

Execute C program in Current shell

Hello, I have a c program executable which I need to run inside a shell script. But the c program runs in a subshell because of which all the actions done by the c program is not available to the current shell. Is there any way to execute a C program binary executable in the current shell? (4 Replies)
Discussion started by: sachinverma
4 Replies

4. Shell Programming and Scripting

how to execute a unix shell script from a java program

Hi All, well , i am facing this problem.. i have tried a few sample codes but there isn't any solution . could anyone please give a sample code as of how to do this... Please see the below details...and read the details carefully. I have written some code, logic is 1)from... (4 Replies)
Discussion started by: aish11
4 Replies

5. UNIX for Dummies Questions & Answers

how to execute program without using ./ or sh

Hi, I am a complete newbie for unix. I have just installed mysql on my MAC. I was wondering every time I wanted to use mysql I had to ./mysql or sh mysql everytime on /usr/local/bin/mysql/bin. How can I execute the mysql program without using ./ or sh. I chmod +x already. And what do I have to... (3 Replies)
Discussion started by: noppanit
3 Replies

6. Programming

C program to execute shell script

Hi, Can anyone give me a sample code to execute shell script from C program. Thanks (6 Replies)
Discussion started by: baigmd
6 Replies

7. Shell Programming and Scripting

C program to execute shell script

Hi, Can anyone pls give a sample to execute a shell script from C program Thanks (2 Replies)
Discussion started by: baigmd
2 Replies

8. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

9. Shell Programming and Scripting

how to execute this program?

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on /dev/hd4 32768 16016 52% 2271 14% / /dev/hd2 4587520 1889420 59% 37791 4% /usr /dev/hd9var 65536 12032 82% 518 4% /var /dev/hd3 819200 637832 ... (1 Reply)
Discussion started by: sathyaac
1 Replies

10. UNIX for Dummies Questions & Answers

Urgent!! How to write a shell program to execute command to access internet?

hi, I am new ot unix. So, can i write a shell(c shell or korn shell) program to access internet? I mean if I run the program, it can access specified url and then copy the html to a file? Can anyone help me? And how can make the program runs every 1 hr? new comer (2 Replies)
Discussion started by: firebirdonfire
2 Replies
Login or Register to Ask a Question