command line question

Paul Mooring paul at opscode.com
Wed Jan 30 10:07:54 MST 2013


Using & specifically backgrounds the process so the shell is free to do other things, but the process is still a child of the current shell.  You can prove this to yourself by running:

    # print the shell's PID
    echo $$
    # background a process
    sleep 25 &
    # look at the sleep process's parent PID
    ps -ef |grep sleep

The 'nohup' command tells the process to ignore SIGHUP, I don't believe this backgrounds the process so your shell is still locked while waiting on the process to finish.  However when the parent shell is closed the HUP signal is sent to end it's child processes, so `nohup some_cmd` prevents the child from being killed along with the parent.  For this reason you will often see nohup used with backgrounded processes:

    nohup sleep 25 &

The final result is a process that doesn't lock up your shell and doesn't end when it's parent (your shell) is terminated.

--
Paul Mooring
Systems Engineer and Customer Advocate

www.opscode.com

From: Patrick Callahan <patrick.pxc.c at gmail.com<mailto:patrick.pxc.c at gmail.com>>
Reply-To: Main PLUG discussion list <plug-discuss at lists.phxlinux.org<mailto:plug-discuss at lists.phxlinux.org>>
Date: Wednesday, January 30, 2013 4:26 AM
To: Main PLUG discussion list <plug-discuss at lists.phxlinux.org<mailto:plug-discuss at lists.phxlinux.org>>
Subject: Re: command line question

Behavior is different if you close the terminal emulator by calling 'exit' from within the session inside it than if you click the x in the corner or close the tab in Konsole. Without using something like nohup, even things launched in the background are usually killed by clicking closing a terminal emulator from the outside. They stay open if you just log out of the session (at which point most terminal emulators just close themselves anyway).

Using nohup fixes this weirdness. It doesn't occur with processes that truly 'detach' themselves from the terminal, like those running inside screen and tmux sessions. I think this has something to do with the fact that closing a terminal emulator from the GUI is more like killing the session than asking it to close, either backgrounded processes still associated with that (p)tty can't cope with that, or they count as 'child' processes and so they are implicated to and also receive whichever signal is used to kill the terminal session.

Someone who really knows things about Unix shells can probably better explain the real mechanism of action here, but anyway, the behavior is different depending upon how the terminal emulator is closed.


On Tue, Jan 29, 2013 at 5:13 PM, Paul Mooring <paul at opscode.com<mailto:paul at opscode.com>> wrote:
I did not know '&>' was a thing, thanks Kevin and Hans!
--
Paul Mooring
Systems Engineer and Customer Advocate

www.opscode.com<http://www.opscode.com>






On 1/29/13 4:18 PM, "der.hans" <PLUGd at LuftHans.com<mailto:PLUGd at LuftHans.com>> wrote:

>Am 29. Jan, 2013 schwätzte Kevin Fries so:
>
>moin moin Kevin,
>
>> I am surprised nobody gave this answer, at least as far as I saw:
>>
>>   $ /path/to/command &> /dev/null
>
>A couple of us did, except we spelled it correctly :)
>
>'&>/dev/null' is a bashism for '>/dev/null 2>&1'. There's nothing wrong
>with it for bash scripting, but I find the latter to be easier to read,
>far more common and more portable, so that's what I use for teaching and
>giving answers on mailing lists.
>
>In a bash only environment, either is fine, but when working with other
>Bourne shell derivatives your syntax might not work. It might work in
>zsh, but I'm pretty certain it isn't allowed in POSIX shell or ksh and it
>definitely isn't in the original Bourne shell.
>
>A really cool feature of a good toolset is there is more than one way to
>do it :).
>
>ciao,
>
>der.hans
>
>> Kevin
>> On Jan 29, 2013 3:27 PM, "Matt Graham" <danceswithcrows at usa.net<mailto:danceswithcrows at usa.net>> wrote:
>>
>>> From: joe at actionline.com<mailto:joe at actionline.com>
>>>> I still can't get either to work.
>>>> James: gwenview filename.jpg > /dev/null 2>&1 &
>>>> Hans:  gwenview filename.jpg 2>/dev/null &
>>>>
>>>> Both examples work to cause gwenview to open filename.jpg without
>>>> any error messages. However, when I close the terminal window,
>>>> gwenview also closes.  What am I doing wrong?
>>>
>>> To stop a process from dying when its parent tty dies, you probably
>>>want
>>> nohup.  "nohup gwenview filename.jpg > /dev/null 2>&1 &" .  The
>>> xterm/konsole
>>> will still be there, though you can then manually close it.
>>>
>>> If this approach *did* automagically close the xterm, it'd be
>>>functionally
>>> equivalent to just starting gwenview from the mini-commandline....
>>>
>>> --
>>> Matt G / Dances With Crows
>>> The Crow202 Blog:  http://crow202.org/wordpress/
>>> There is no Darkness in Eternity/But only Light too dim for us to see
>>>
>>> ---------------------------------------------------
>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org<mailto:PLUG-discuss at lists.phxlinux.org>
>>> To subscribe, unsubscribe, or to change your mail settings:
>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>
>>
>
>--
>#  http://www.LuftHans.com/        http://www.LuftHans.com/Classes/
>#  When I work, I work hard. When I play, I play hard.
>#  When I sit, I sleep. - Embe Kugler


---------------------------------------------------
PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org<mailto:PLUG-discuss at lists.phxlinux.org>
To subscribe, unsubscribe, or to change your mail settings:
http://lists.phxlinux.org/mailman/listinfo/plug-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.phxlinux.org/pipermail/plug-discuss/attachments/20130130/50b9c1fe/attachment.html>


More information about the PLUG-discuss mailing list