LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2003, 11:16 AM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 31
exec program from within another


Hello

I need to run a program (or a script) from within my program, but the second one must stop the caller program from running.

You must imagine, at a specific event a script has to be run from my program, which then continues listening for other events. I dont need to wait for return values or so, I just want to issue a command and then forget about it.

How do I do that? execve will stop my caller program (or at least i understood the manpage so)

Thanks

raven
 
Old 11-04-2003, 11:32 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Use system(3) and/or fork(2).
 
Old 11-04-2003, 11:36 AM   #3
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
A few more details would be helpful but you can do it wiht a simple bash script:

!#bin/bash
cd <path to directory where first executable located>
<name of first exec>
cd <path to script>
./<name of script> &
cd <path to directory where execve located>
execve

Create a text file in your home directory or anywhere you want and give it a nice name like "doexec". Cut and paste the above text to doexec. make sure there is no white space before"!#bin/bash", i.e. these are the first characters in the text file w/o white space in front.
Then open a console su to root and navigate to the location of doexec and run:

# chmod a+x doexec

That makes the script executable. To run doexec, navigate to the directory where it's located and run:

$ ./doexec

Alternatively, you could edit the called script to run the exe program first and shut down at the appropriate time rather than create a new script.
 
Old 11-04-2003, 11:41 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Based on your description, consider system(). While fork() will work, your "calling code" continues to run, which you do not seem to want.

With system() your calling thread is suspened until the child completes.

If you want the calling thread to "become" the child process, execve() and its cousins -- execl, execlp, execle, execv, execvp. These do not return except on error.

system() is probasbly the easiest one code and deal with
 
Old 11-04-2003, 11:42 AM   #5
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 16
I think using signals may help u.Can u make a bit more clear ur quest ??.
 
Old 11-04-2003, 12:03 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
pa1 is right - system (under Linux) does not respond to signals -
ie., if the program you call happens to lock up you can't get rid of it.

If you read the man page for system I think it gives a workaround for this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c code: how to exec a external program in c feetyouwell Programming 3 04-04-2005 10:44 PM
java runtime() exec() program mcshen Programming 4 11-25-2004 11:29 AM
Logging program exec leckie Linux - Security 1 08-06-2004 08:45 AM
c++ exec fosh Programming 2 07-04-2004 10:44 AM
cannot exec as person Linux From Scratch 5 12-19-2003 04:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration