LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to test disown and nohup commands ? (https://www.linuxquestions.org/questions/programming-9/how-to-test-disown-and-nohup-commands-796692/)

gjagadish 03-20-2010 11:29 AM

How to test disown and nohup commands ?
 
Hi,

I ran a perl script in background with nohup and disown to make sure my script runs even after the parent process is terminated.

Method 1: nohup
> nohup perl run.pl &
[1] 2080

Method 2: disown
> perl run.pl &
[2] 2448
> disown

In either method, the script process doesn't seem to connect to init process, when I checked using pstree command. I thought, the disown or nohup command detaches the process from its parent and attaches it to grand grand parent init process. And it disables SIGHUP signal to my script process.

But the pstree command didn't show me what I expected. It still shows my script process attached to my current terminal process.

I just don't understand the concept behind these two commands (nohup and disown). Is there any way, I can see the list of processes that are run by nohup or disown command ?

Thanks in advance
Jagadish

LinuxTutBlog 03-20-2010 02:19 PM

Let's see, you're asking multiple questions. I'll try to answer them all.
  • "the script process doesn't seem to connect to init process, when I checked using pstree command"
    • Neither nohup nor disown will actually move the process in the pstree. Movement will happen when you close the shell.
  • "I just don't understand the concept behind these two commands (nohup and disown)."
    • nohup makes sure a process won't stall if the terminal loses connection somehow (without nohup, a process could stall because nothing reads its output anymore)
    • disown tells the shell to forget that the specified job is a child process of the shell, meaning that the shell won't kill it on exit
  • "Is there any way, I can see the list of processes that are run by nohup or disown command ?"
    • I haven't been able to find an easy way. You could use lsof to check if a process has an unconnected stdin and stdout/stderr directed to nohup.out, which would indicate that it's been started using nohup. For disown, you can compare the output of `ps -o pid= --ppid <pid>` with that of `jobs -l | awk '{ print $1; }'`; pid's occuring in the former and not in the latter are either disowned or not started by the shell itself.
I hope this clears things up a bit. If you still don't understand everything, don't hesitate to ask. I might even write a tutorial about it :).

gjagadish 03-21-2010 04:33 AM

Thank you very much for your explanation. It makes perfect sense :)


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