LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-02-2015, 07:01 PM   #1
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Rep: Reputation: Disabled
exit the terminal


Hi,
when I type exit in the ubuntu terminal (tty1), although the logon prompt is back, the application that is spawned (a.out&) is still running .
using ps -ef |grep a.out , I get ? in place of tty.
I want to know how to exit tty and that will close a.out (by sending some signal to it). (like when I close a terminal in unity UI by clicking on the cross)
 
Old 12-02-2015, 07:12 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Then you don't want bash for your shell. I believe csh does that.

By putting it in the background you are TELLING bash to ignore it.

If it exits while the bash session is still running it will perform the clean up (getting the process status, and reporting it). If the bash session exits, then it will be the init process that cleans it up.
 
Old 12-02-2015, 07:21 PM   #3
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
You can use Bash for that. Just set the huponexit flag (http://www.gnu.org/software/bash/man...-Shopt-Builtin).
 
2 members found this post helpful.
Old 12-02-2015, 07:22 PM   #4
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Yes, ubuntu 14.04.3 uses bash, but how do i solve this
in the UI , clicking on the 'X' closes the terminal and the back ground process (the way I wan it), is there some command to do this by some command , ('exit' is not working out)
 
Old 12-02-2015, 07:24 PM   #5
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
thanks...I ll use that
 
Old 12-02-2015, 07:32 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by berndbausch View Post
You can use Bash for that. Just set the huponexit flag (http://www.gnu.org/software/bash/man...-Shopt-Builtin).
It doesn't always work - as the process that is started can ignore the HUP signal. xterm is one that ignores HUP signals.
 
Old 12-02-2015, 07:43 PM   #7
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by jpollard View Post
It doesn't always work - as the process that is started can ignore the HUP signal. xterm is one that ignores HUP signals.
If the process is named a.out, most likely the user has control over that.

For more reliability, I suppose you could also trigger kill -9 through a trap.
 
Old 12-02-2015, 08:05 PM   #8
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
setting it worked fine using shopt -s huponexit; shopt | grep huponexit
but this has no effect on terminal created in UI (by pressing ctr-alt-T)

I am testing it against a.out (simple hello world type with no signal handling)

Last edited by asif_bahrainwala; 12-02-2015 at 08:07 PM.
 
Old 12-02-2015, 08:20 PM   #9
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by asif_bahrainwala View Post
setting it worked fine using shopt -s huponexit; shopt | grep huponexit
but this has no effect on terminal created in UI (by pressing ctr-alt-T)

I am testing it against a.out (simple hello world type with no signal handling)
What does ctl-alt-T do? I think it opens a new tab in your terminal window, correct? If so, a new shell is run, which is unlikely to have the huponexit option.

Add shopt -s huponexit to your .bashrc and you should be fine.
 
Old 12-02-2015, 08:46 PM   #10
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
What does ctl-alt-T do? I think it opens a new tab in your terminal window, correct? If so, a new shell is run, which is unlikely to have the huponexit option.

Add shopt -s huponexit to your .bashrc and you should be fine.
I create the new terminal , enter shopt -s huponexit ,but this has no effect, the application does not get the signal

Adding it in .bashrc did not help

Last edited by asif_bahrainwala; 12-02-2015 at 08:49 PM.
 
Old 12-02-2015, 09:52 PM   #11
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Then perhaps the application ignores the signal or handles it otherwise than exiting.

For a test, kill -HUP PIDofyourapplication. Does it exit? If yes, something is wrong with your huponexit.

For a second test, run another command in the background, for example sleep 11111 & or tail -f /etc/passwd &, then log out and check if the other command is still running. It shouldn't.
 
Old 12-03-2015, 05:27 AM   #12
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
hi
sending kill -HUP 10309 results in: [1]+ Hangup ./a.out

My application is :

#include<stdio.h>
int main()
{
while(1){
printf("in \n");
fflush(stdout);}
};

There is no signal handling here....I wonder if this is a bug in ubuntu 14.04.3
 
Old 12-03-2015, 05:53 AM   #13
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by asif_bahrainwala View Post
hi
sending kill -HUP 10309 results in: [1]+ Hangup ./a.out

My application is :

#include<stdio.h>
int main()
{
while(1){
printf("in \n");
fflush(stdout);}
};

There is no signal handling here....I wonder if this is a bug in ubuntu 14.04.3
No not a bug. It's probably for this reason - from http://www.gnu.org/software/bash/man....html#Signals:
Quote:
If the huponexit shell option has been set with shopt (see The Shopt Builtin), Bash sends a SIGHUP to all jobs when an interactive login shell exits.
The highlight is mine. It needs to be a login shell, but the shells running in GUI windows aren't login shells. Sorry for leading you down the wrong path.

Apart from huponexit, I don't see an easy way to kill all background processes. You can do it with the trap command - trap somecommand 0 will execute somecommand when the shell exits. You would have to write a small script that kills all processes running on the same terminal.
 
Old 12-03-2015, 06:14 AM   #14
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
Then perhaps the application ignores the signal or handles it otherwise than exiting.

For a test, kill -HUP PIDofyourapplication. Does it exit? If yes, something is wrong with your huponexit.

For a second test, run another command in the background, for example sleep 11111 & or tail -f /etc/passwd &, then log out and check if the other command is still running. It shouldn't.
I tried the sleep command and Yes, it continued to work after typing exit (as showed in ps -ef |grep sleep though :

****:~$ shopt | grep huponexit
huponexit on
 
Old 12-03-2015, 06:21 AM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
For an experiment, you can try:

exec bash --login (this should terminate things and the terminal window too)

or just "bash --login" (this will allow you to test again easier).

This restarts bash as a login. Then test things again.

DO NOT PUT THIS IN .bashrc, .profile, .login or anything else - you will suddenly NOT be able to login.

Also note - sending such signals is usally a fail, as background processes tend to have output redirected to a file (or /dev/null) and input redirected from a file (or /dev/null) which effectively detatches from a terminal. The shell ALSO can't track processes that these background processes may spawn.

It then depends on the process group leader - which in the GUI is normally the window manager (or whatever process is started by the GUI login), and not anything later (are all in the process group started by the GUI). Note: this is done so that GUI programs started by some methods get a termination signal on logout. Without this, at least SOME GUI applications will constantly try to reconnect to the display (they show up as 100 CPU bound). It depends on the programming for the application handling the error condition where the X server is aborted.

What you are asking for is each shell spawned by the GUI terminal to be a new group leader, then when that group leader exits it has to send a signal to all members of that process group
https://www.win.tue.nl/~aeb/linux/lk/lk-10.html

You should be able to look at the properties of the menu entry that starts the GUI terminal - and change the options on it to start bash as a login bash (bash -l or bash --login). Then I think what you are trying to do with shopt will work. NOTE: This change will also alter how the shell environment is handled and how termination is handled (see the section INVOCATION of the bash manpage).

Last edited by jpollard; 12-03-2015 at 06:57 AM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 09:32 AM
Mandrake login won't exit terminal oxwilder Mandriva 2 10-12-2005 06:55 PM
how to exit a terminal changcheh Linux - General 3 08-30-2005 08:55 AM
I don't want the terminal to exit! dbcoder Linux - Software 15 07-29-2005 10:39 PM
Loose display on X exit or terminal switch hematose Linux - General 2 08-15-2004 05:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration