LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   attach a process to init (https://www.linuxquestions.org/questions/linux-general-1/attach-a-process-to-init-548560/)

sagar.1986 04-24-2007 05:51 AM

attach a process to init
 
Hello everybody,

When i open a terminal and run a program like say:
$programname
The program runs in foreground. When I want to put it to background I can invoke it as:
$programname &

In the above case ,program will still be the child of bash. If i close the terminal then the process terminates. Is it possible to attach this process to init so that even if I close the terminal the program continues to run?

matthewg42 04-24-2007 07:01 AM

The problem is caused because the program you run has the terminal set to the terminal of the parent shell. When that terminal is closed, SIGHUP (the hang-up signal) is sent to all processes which are attached to the terminal, and presumably your program chooses the default action for this signal, which is to terminate.

There are several solutions. I list them here is order of easy of implementation/use, easiest first:
  1. Prefix the command with "nohup". This traps SIGHUP, protecting your program from it. Output of the program will be send to the file "nohup.out". See the nohup manual page for further details.
  2. Run your program in GNU Screen. This takes a little more learning, but it's well worth it. For example, you can detach a process from a terminal, and re-attach it to another. The screen manual page is a little turgid, but worth a read. You might also find this tips page useful.
  3. Alter your program to run as a proper daemon process as opposed to a regular process which is backgrounded using shell job control. There's more to running as a daemon then just ignoring SIGHUP. See here for details.


All times are GMT -5. The time now is 10:02 PM.