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 &");

12
  • Hello @Tau, Welcome to S.O. Do you want to have a parent process that does not ends when the child process is finished?? Jul 29, 2019 at 13:19
  • 1
    @AlejandroMontilla no OP wants child process to continue when parent dies.
    – Slava
    Jul 29, 2019 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. stackoverflow.com/questions/30200126/… Jul 29, 2019 at 13:23
  • You can run shild process through nohup utility or implement what nohup does en.wikipedia.org/wiki/Nohup
    – Slava
    Jul 29, 2019 at 13:24
  • @LightnessRacesinOrbit it may terminate when termination of parent leading to close of control terminal (or vice versa).
    – Slava
    Jul 29, 2019 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.