Continuing after using execl() to start a script

Hey,

So I have a shell script that I am calling with execl(). The script starts up 5 instances that are required by my c++ code and it has exit 0 at the end. How do I allow my program to continue on once it has called execl() and completed.


1
2
3
4
  if (pID == 0) {

        execl("/etc/init.d/test", "test", "start", NULL);
    }


Thank you!
execl() never returns unless the system call fails. (ie, the program is not started).
You need to use fork() to create a child process and then run the execl() in the child.

If you want your program to continue executing while /etc/init.d/test is still running,
then there is nothing more to do -- just have the parent keep going.

If you want your program to pause while /etc/init.d/test is running and itself
continue only when test finishes, then you need to use waitpid() in the parent.
can you not use popen(3) ?

http://www.manpagez.com/man/3/popen/

or system?


you should describe what you are trying to do
rather than how you think it should be done.
Last edited on
Thank you very much for the replies!

I have now realized a new problem. These instances that I am starting, are printing their status as they startup. Since I am putting these processes in the background, right now everything returns, and the code continues on. The problem is that my c++ relies on these instances being fully started, which is indicated by a certain sentence in their output. How would I go about reading the output from them and waiting until these key words are read, instead of just having the code sleep for a bit.

This is my current function...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int execute(const char *command)
{
    pid_t cpid;

    cpid = fork();

    switch (cpid) {
        case -1: perror("fork");
                 break; 

        case 0: execl("/etc/init.d/test", "test", command, NULL); /* this is the child */
                _exit (EXIT_FAILURE);

        default: waitpid(cpid, NULL, 0); 

    }



Thank you!
Last edited on
You can redirect the output of the child process to your program, then have your program read and parse the data.

Topic archived. No new replies allowed.