0

I need to start a program from within a c++ program on linux, similar to what ShellExecute does on Windows. This program has to keep running even after the first program ends.

I found suggestions to use fork() and exec(), however these do not work for me as they create a child process that terminates when the parent process ends. I need to keep the new process running even after the parent has ended. Using system("") i have similar issues.

pid_t pid;
pid = fork();

char *arg[] = {"/home/pi/Robot/RobotUpdate", 0};

if (pid == 0)
    execv("/home/pi/Robot/RobotUpdate", arg);

/home/pi/Robot/RobotUpdate is a c++ program that has to keep running after the first program has ended.

EDIT: solved with nohup command: system("nohup /home/pi/Robot/RobotUpdate &");

Tau
  • 1
  • 1
  • Hello @Tau, Welcome to S.O. Do you want to have a parent process that does not ends when the child process is finished?? – Alejandro Montilla Jul 29 '19 at 13:19
  • 1
    @AlejandroMontilla no OP wants child process to continue when parent dies. – Slava Jul 29 '19 at 13:21
  • _"however these do not work for me as they create a child process that terminates when the parent process ends"_ I'm not convinced by this. https://stackoverflow.com/questions/30200126/create-independent-process-in-linux?rq=1#comment48554310_30200126 – Lightness Races in Orbit Jul 29 '19 at 13:23
  • You can run shild process through `nohup` utility or implement what nohup does https://en.wikipedia.org/wiki/Nohup – Slava Jul 29 '19 at 13:24
  • @LightnessRacesinOrbit it may terminate when termination of parent leading to close of control terminal (or vice versa). – Slava Jul 29 '19 at 13:24
  • Yeah we're going to need more context/information from the OP – Lightness Races in Orbit Jul 29 '19 at 13:30
  • How you start child process using ```system()``` system call? You should start child process in background, and if the child process doesn't end by it's normal flow of execution then child process will not end on the end of parent process. – Vikas Awadhiya Jul 29 '19 at 13:38
  • `fork` and `execve` *are* the way to go – there is yet another system call necessary to achieve the desired behaviour, see [here](https://stackoverflow.com/a/9038037/1312382) or [here](https://codingfreak.blogspot.com/2012/03/daemon-izing-process-in-linux.html). – Aconcagua Jul 29 '19 at 13:55
  • Have a look at [daemon function](http://man7.org/linux/man-pages/man3/daemon.3.html) as well... – Aconcagua Jul 29 '19 at 14:05
  • if you're ending the "first program" using Ctrl+C to terminate it, then you're actually ending the "process group" which would include the process you `exec`d – Sam Mason Jul 29 '19 at 14:21
  • I think you have a misunderstanding of how fork/exec works. Your problems probably lies elsewhere. Also see [Are child processes created with fork() automatically killed when the parent is killed?](https://stackoverflow.com/q/395877/608639) – jww Jul 29 '19 at 14:59
  • Thank you for all the replies. I went with what Slava suggested - "nohup" command and it works. – Tau Jul 29 '19 at 16:25

0 Answers0