LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 04-24-2007, 06:51 AM   #1
sagar.1986
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:
$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?
 
Old 04-24-2007, 08:01 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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


Reply


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 02:50 AM
init Parent Process ID sobia Linux - General 1 06-09-2006 11:28 PM
init process gets stucked prital Linux - Software 0 02-21-2005 06:52 AM
How can i attach gdb to a daemon process praveengujar Linux - General 0 08-11-2004 07:58 AM
Slackware init process.... hjles Slackware 2 01-30-2003 03:44 PM


All times are GMT -5. The time now is 09:32 AM.

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