-2

Given the following C program:

int main() {
   do_something();

   if (console_command_m_got) {
      do_something_else_1();
   }
   if (console_command_q_got) {
      exit();
   }


   return 0;
}

When I start a C program in the terminal, it runs there. I am looking for a way to start the program from the terminal, but detach it from that terminal. It should not close, when I close the console and run in the background like when I put it in the autostart.

Is there any possibility, to change the control flow of that running program by entering commands in the console? A little example of how I could imagine the workflow:

$ ./run_program         // Program starts and runs in background
$ ./run_program -m      // Modify the behaviour of the running program (if-statement above)
$ ./run_program -q      // Stop the running program

Or maybe, I have a complete wrong approach.

luke
  • 71
  • 9
  • 2
    Do you mean "How do I [double fork](https://stackoverflow.com/questions/10932592/why-fork-twice)?" You do that and write a "PID file" used to control it or have some kind of IPC socket available. – tadman Mar 30 '21 at 21:16
  • How often are you expecting to need to tell it to do something different? Every millisecond? Once every few seconds? How many different possible instructions are there likely to be? 2-3? 800? Just trying to get a *"feel"* for the bounds. – Mark Setchell Mar 30 '21 at 21:38
  • 1
    Does [this](https://stackoverflow.com/questions/26636661/force-cli-to-use-existing-process) help at all? – tink Mar 30 '21 at 22:05
  • @Mark Setchell I should only be required to change the behaviour maybe every few minutes in worst case. Possible instructions could be around 5-10.@tadman that sounds nice, I*ll have a look at this! – luke Mar 30 '21 at 22:05
  • 1
    A unix socket is pretty nice for forwarding commands to an already-running instance. Also see `nohup` and/or installing your own SIGHUP handler inside your code. – Ben Voigt Mar 30 '21 at 22:08
  • The very simplest, IMHO, and also rather inelegant is to create a file called say `EXIT` or `FASTER` in the Terminal with `touch EXIT` and your program checks for that file's existence every time through its main loop and deletes it when seen as acknowledgment. tink's answer provides more sophisticated ideas. YMMV. – Mark Setchell Mar 30 '21 at 22:17
  • 1
    @tadman That worked perfectly for me. Thank you! If you add it as an answer, I will mark the question as solved. – luke Mar 31 '21 at 15:55

1 Answers1

0

Have you checked out screen? It can be scripted as in https://unix.stackexchange.com/a/162150 to run in detached mode and run the called script (which could run your program) in the background. You can then find it again with screen -ls and re-attach to the session with screen -r name-of-screen.

jwvh
  • 50,871
  • 7
  • 38
  • 64
dsteiert
  • 24
  • 2
  • `screen` is always useful to know, but this doesn't address the "make the running process do something different when invoked with a parameter", so it's technically not an answer to the question ... – tink Mar 30 '21 at 23:29