Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 04-24-2007, 05:51 AM   #1
LQ Newbie
Registered: Nov 2006
Posts: 20

Rep: Reputation: 0
Unhappy attach a process to init

Hello everybody,

When i open a terminal and run a program like say:
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?
Old 04-24-2007, 07:01 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
attach gdb to a running process linwenyuan Linux - Desktop 1 10-15-2006 01:50 AM
init Parent Process ID sobia Linux - General 1 06-09-2006 10:28 PM
init process gets stucked prital Linux - Software 0 02-21-2005 05:52 AM
How can i attach gdb to a daemon process praveengujar Linux - General 0 08-11-2004 06:58 AM
Slackware init process.... hjles Slackware 2 01-30-2003 02:44 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration