Re: command line question

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
+ (text/plain)
Delete this message
Reply to this message
Author: Paul Mooring
Date:  
To: Main PLUG discussion list
Subject: Re: command line question
So we could actually have a pretty detailed discussion on what 'termination' means. In the world of UNIX like OS processes receive and handle various signals as they see fit. Your terminal emulator isn't actually your shell process, if you open multiple tabs or shells and run `echo $$` you'll find that each shell instance has it's own PID and that processes launched from that shell have the shell's PID as a parent.

Different actions can cause signals to be sent, in the case of closing a window the window manager will kill the terminal emulator and it is up to the window manager and terminal emulator what signals get sent where. In the case of running `exit` you are directly telling the shell to exit and the shell will send a HUP to children and also the terminal manager will (hopefully) pick up that the shell process is gone and act accordingly.

Keep in mind that programs receive these signals and are allowed to over ride default behavior, I will generally send a process a SIGTERM to allow it to clean up after itself if I need to end a process. This is the sort of case where the parent (shell) can send SIGHUP to it's children so they know to gracefully die. The only exception is a SIGKILL cuts the process off at an OS level, no chance for clean up (this is why you shouldn't reach straight for `kill –9` on a hung process) which wouldn't allow the parent to send a SIGHUP to it's children.

As for the specifics of what common terminal emulators do when closed, I don't really know but if the processes stay alive I suspect the answer isn't send a SIGTERM.



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


Thanks for the lucid and concise explanation, Paul. I'd always kind of lazily wondered about why that was. Even though it seems a bit redundant, I would appreciate one last point of clarification:

Termination has a very specific meaning here, right? Most of the time, when processes exit (like when I type 'exit' in my shell), they _aren't_ 'terminated', they just dispose of themselves some other way. And that, then, is why the signaling of HUP to child processes doesn't come into play in situations when 'exit' is called in the shell, right?

On Jan 30, 2013 10:08 AM, "Paul Mooring" <<mailto:paul@opscode.com>> wrote:
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<http://www.opscode.com>

From: Patrick Callahan <<mailto:patrick.pxc.c@gmail.com>>
Reply-To: Main PLUG discussion list <<mailto:plug-discuss@lists.phxlinux.org>>
Date: Wednesday, January 30, 2013 4:26 AM
To: Main PLUG discussion list <<mailto:plug-discuss@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 <<mailto:paul@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" <<mailto:PLUGd@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" <<mailto:danceswithcrows@usa.net>> wrote:
>>
>>> From: <mailto:joe@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 - <mailto:PLUG-discuss@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 - <mailto:PLUG-discuss@lists.phxlinux.org>
To subscribe, unsubscribe, or to change your mail settings:
http://lists.phxlinux.org/mailman/listinfo/plug-discuss


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