12

Possible Duplicate:
In Linux, how to prevent a background process from being stopped after closing SSH client

I have a C program which I access and interact with over terminal (usually from SSH on a linux box). I have been trying to find the solution to the problem where after I close the terminal/logout, the process ends with it (the program basically asks for some options then goes about its business with no further interaction required so I would like to have it continue to run even after I logout of SSH).

There are ways in linux to avoid this such as 'screen', but I want to do it programatically with C without relying on installed packages such as screen- even if this means reinventing the wheel.

So far I understand fork() to be the standard trivial way to daemonize a process, so could anyone help me to finish the code that allows the above described process to happen?

Within parent:

 main()
{

//Do interactive stuff

signal(SIGCHLD, SIG_IGN); //stops the parent waiting for the child process to end

if(fork())
 exit(0);
// and now the program continues in the child process

I can now logout of SSH which closes the original shell...and the child continues its work!

Within child:

//Continue with processing data/whatever the program does (no input/output to terminal required)
exit(0);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1166981
  • 1,738
  • 8
  • 26
  • 44
  • 3
    Read all answers. http://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client – San Jacinto Jan 27 '12 at 18:04
  • Um, just use `nohup` when you run it? No forking required. – Brian Roach Jan 27 '12 at 18:04
  • @ Brian, nohup will not let me interact with the program. Its a command line interaction. – user1166981 Jan 27 '12 at 18:06
  • @San, I want to achieve the same aim without relying on nohup being installed. – user1166981 Jan 27 '12 at 18:06
  • See the suggested thread: answer #2 proposes to use the GNU Screen utility, that allows you to reconnect to a given "virtual terminal" across shell sessions. – ChrisJ Jan 27 '12 at 18:08
  • @User That's somewhat similar to saying you want to program on Linux in C and not use any POSIX facilities. – San Jacinto Jan 27 '12 at 18:09
  • @User I just tested... My Busybox device beside me has nohup... – San Jacinto Jan 27 '12 at 18:10
  • So your question is that you don't know how to use `fork()`? What specific problem are you having? – Brian Roach Jan 27 '12 at 18:10
  • @ san, point taken, but doesn't change my mind about doing this programatically. – user1166981 Jan 27 '12 at 18:13
  • @ Brian, the exitting of SSH/shell closes the parent and child still. – user1166981 Jan 27 '12 at 18:13
  • @User Fair enough. I won't argue the point anymore but I am curious over the use case where you can't rely on such a standard utility. Could you elaborate? – San Jacinto Jan 27 '12 at 18:15
  • @ San, thank you, and of course. In all honesty I would just like to carry out the task with usage: ./program. Its sheer stuborness on my part I have to admit to rely on outside programs, even if these are standard in the very vast majority of linux installations. – user1166981 Jan 27 '12 at 18:20
  • Pretty annoying that this is closed as a duplicate, when the duplicate is clearly about shell usage, and this is about C programming. – Timmmm Feb 12 '19 at 14:29

1 Answers1

14

to detach the process from the parent:

use setsid() on the children process, it will run the program in new session

 sid = setsid();

To Keep a program running even when the terminal is closed:

SIGHUP is a signal sent to a process when its controlling terminal is closed.

try to ignore it using

signal (SIGHUP, SIG_IGN);
Timmmm
  • 88,195
  • 71
  • 364
  • 509
Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • Yes others have suggested launching program with nohup- but can you suggest how to do this programatically within the context of the code I have posted? – user1166981 Jan 27 '12 at 18:17
  • @User you can install a signal hander for this signal that ignores it. just be sure to register the handler with the new process, not the parent. http://www.gnu.org/software/libc/manual/html_node/Signal-Handling.html – San Jacinto Jan 27 '12 at 18:26
  • you don't need signal (SIGHUP, SIG_IGN); because the parent exits and therefore the child will not have a controlling process – Konstantin Glukhov Nov 10 '19 at 10:01