How to run two commands from a exec call in a c program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to run two commands from a exec call in a c program
# 1  
Old 10-30-2009
How to run two commands from a exec call in a c program

Hi,

I have to run two commands one after another from a c program. How can i do this with exec system calls. i tried giving them as argument toexecv but it is not working.please help


thanks
# 2  
Old 10-30-2009
From the man page of exec(3):
Quote:
The exec() family of functions replaces the current process image with a new process image.
Means that as soon as the execv() is executed, the current process (your program) is completely replaced by the program you're starting. Any instructions left in the original program won't be executed.

If you want to just run a child process, use system(3)
# 3  
Old 10-30-2009
I cannot use system as my program is setuid prgram. Basically i am trying to run two commands which require root privilegs from a set uid program . how can i run one command and then second command using exec system calls.
# 4  
Old 10-30-2009
I'd think about why it's necessary to run that program as setuid root, and if that couldn't be accomplished by other means (eg. sudo) or by using a different approach using as few privileges as possible.

IF that isn't possible, you could simulate the system() call by using fork() and wait() (warning: the following is pseudo-code)
Code:
function my_system(command)
    pid = fork()
    if pid == 0 // child
        exec(command)
    else
        wait for pid to exit

my_system(command1);
my_system(command2);

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Run 2 exec commands

I have to create two instances of jBoss 5.1.0 GA. In order to do that I have to execute the following in start-jboss.sh: find . -exec /opt/novell/idm/jboss/bin/run.sh -Djboss.service.binding.set=ports-01 -c IDMProv -b 0.0.0.0 \; -exec /opt/novell/idm/jboss/bin/run.sh... (4 Replies)
Discussion started by: Joydeep Ghosh
4 Replies

3. Shell Programming and Scripting

[Solved] Run multiple commands in invoked program

Hi, I have coded a program in Haskell using the compiler Hugs and the program requires multiple commands (with parameters) to be entered into it, it then outputs the result of its execution. I need to test a lot of different options (i.e. the parameters) so it would be obvious to automate the... (0 Replies)
Discussion started by: tz742
0 Replies

4. Programming

[C] exec system call

Hi again ;) Now I want to make a program that will execute the programs with exec, asking the user if he wants the program to run in background or foreground. scanf("%c",&caracter); if (caracter=='y'){ printf("Has decidido ejecutarlo en background\n"); if((pid=fork())==0) {// fork para... (3 Replies)
Discussion started by: lamachejo
3 Replies

5. UNIX for Dummies Questions & Answers

How to run multiple piped commands in a find -exec statement?

I can't get this to work. Running a single command works fine: find . -name "*.dat" -exec wc -l '{}' \; gives me the file name and number of lines in each .dat file in the directory. But what if I want to pipe commands, e.g. to grep something and get the number of lines with that pattern... (3 Replies)
Discussion started by: DJR
3 Replies

6. Programming

How forbid use fork() in exec() program.

Hello World! I am writing code in C++ which have to launch another application X using exec(). I would like to set some limits on it using setrlimit etc... My problem is that i don't know how to forbid using fork() and strlimit by application X. How can i do it? (3 Replies)
Discussion started by: kzi
3 Replies

7. UNIX for Advanced & Expert Users

exec to call specific function in C prog

I would like to call a particular function in a C program using execl(). Is this possible using execl or anyother function ? Thanks (2 Replies)
Discussion started by: vpraveen84
2 Replies

8. Programming

exec() system call

hi there, i was reading about the exec() function. and if i m not wrong, exec() kills your present process and starts a new process in its place. the process id remains the same. then it says if exec is successful the text data and stack are overlayed by new file! - i dont get this part "only... (2 Replies)
Discussion started by: a25khan
2 Replies

9. UNIX for Dummies Questions & Answers

need to restrict program exec and running

I'm on Freebsd 4.5 stable, havin question of that kind: I need to restrict programs running, like BitchX for example, which can be dowlnoaded by logged on user, and i cant set permissions to all users to prevent that program from executing. And ipfw doesnt help me because of i need to allow that... (1 Reply)
Discussion started by: hachik
1 Replies

10. Shell Programming and Scripting

Exec. commands in python

How would i do if i'd want to execute a command in a python script or programme ? (1 Reply)
Discussion started by: J.P
1 Replies
Login or Register to Ask a Question