Execute a C program from Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute a C program from Shell
# 8  
Old 12-21-2010
This makes little sense, actually.

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

Code:
./program &
PID=$!

# 9  
Old 12-21-2010
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 Smilie

Could you please explain what "grep -v grep" does?
# 10  
Old 12-21-2010
You got the PID of the grep command.

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

But it's not necessary.

Code:
ps -ef | grep [d]ummy

will also remove grep from the ps output.
# 11  
Old 12-21-2010
The program may execute much quicker than 2 minutes. Also, I see he's using the bash shell.

Try the following:

Code:
./program &
pid=$!

This will run the program in the background and then return it's process ID.
# 12  
Old 12-21-2010
Hey scottn,
I am new in shell scripting and i 'm a bit confused.

I execute a C program with:
Code:
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?

Last edited by Scott; 12-21-2010 at 01:39 PM.. Reason: Code tags
# 13  
Old 12-21-2010
Code:
./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:
Code:
Code:
./program &      # run program in the background
sleep 120        # wait for 120 seconds
PID=$!           # get it's process ID

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