LinuxQuestions.org
Have you heard the LinuxQuestions.org Podcast?
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This 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
 
Thread Tools
Old 11-03-2009, 07:00 AM   #1
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12
BASH script won't die when user logs out.


[Log in to get rid of this advertisement]
I've got a BASH script which runs when a user logs in. When a user logs out the script keeps on running. Which is Bad. I'm wondering if anyone can explain how to make the script die when a user logs out.

I've considered making the script check if the user is still logged in by looking at the output of 'who' but had to scrap that when I discovered that a user logged in via an NX session doesn't show up in the output of 'who'. (Whole other issue.) Also that would just be a workaround to the problem rather than a solution.

The script gets run when GNOME starts like this:

Code:
foo:~ # cat /usr/share/gnome/autostart/why_wont_you_die.desktop 
[Desktop Entry]
Type=Application
Name=Test script
Comment=Test script
Exec=/usr/local/sbin/why_wont_you_die
I've tried redirecting stdout/stderr to /dev/null and closing stdin by appending >&- to the end of the Exec line but none of that helped.

The script shows up in the output of pstree like this:

Code:
     ├─gdm───gdm─┬─X
     │           └─gnome-session─┬─bluetooth-apple
     │                           ├─compiz-manager───compiz───gtk-window-deco
     │                           ├─gnome-do───3*[{gnome-do}]
     │                           ├─gnome-keyring-d
     │                           ├─gnome-panel
     │                           ├─gpg-agent
     │                           ├─nautilus
     │                           ├─pidgin
     │                           ├─python
     │                           ├─python─┬─3*[bash]
     │                           │        ├─gnome-pty-helpe
     │                           │        └─{python}
     │                           ├─why_wont_you_di───sleep

The script itself looks like this:
Code:
#!/bin/bash

while true; do
sleep 600
date > /dev/null
done
I wonder if the problem is related to the use of the while true loop though the script exits fine if I use 'kill PID' from the command line.

N.B. this is not actually the script I'm having trouble with as that's rather long, it's a nice short script which does essentially the same thing (sleep for a while and then do something, all wrapped in a while true loop) that demonstrates the problem I'm having.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-03-2009, 08:51 AM   #2
kbp
Member
 
Registered: Aug 2009
Posts: 489
Thanked: 44
Try putting it under '~/.config/autostart' ... should do the job

cheers
linuxfedora kbp is offline     Reply With Quote
Old 11-03-2009, 09:31 AM   #3
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
Unfortunately putting it in ~/.config or ~/anything is no good. I have thousands of users who could log in to the machines. Modifying all their home directories would be madness. (Also for technical reasons I can't actually do that myself.) I need to start the script from a system wide directory. I tried using /etc/xdg/autostart but that makes no difference.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-03-2009, 10:15 AM   #4
tredegar
Senior Member
 
Registered: May 2003
Location: London, UK
Distribution: Kubuntu6.06.1LTS (still excellent!). Kubuntu 8.04.1
Posts: 4,384
Thanked: 144
Which user is the script running as?
ps -Al | grep why_wont_you_die will return the user's UID as the third field.
linuxubuntu tredegar is offline     Reply With Quote
Old 11-03-2009, 11:00 AM   #5
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
The script runs as the same user that ran the GNOME session which invoked it.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-03-2009, 04:38 PM   #6
kbp
Member
 
Registered: Aug 2009
Posts: 489
Thanked: 44
Maybe you need to trap signals, this may help:
http://www.linuxquestions.org/questi...m-bash-257157/

cheers
linuxfedora kbp is offline     Reply With Quote
Old 11-04-2009, 05:34 AM   #7
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
I've tried trapping various combinations of TERM INT and EXIT like so:

Code:
#!/bin/bash

trap "{ touch /tmp/blah; exit 0; }"  TERM INT EXIT

while true; do
sleep 600
date > /dev/null
done
It doesn't make any difference.

I know the trap syntax is good because if I include INT, run the script from a command prompt then press ctrl-c the time stamp on /tmp/blah gets changed.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-04-2009, 05:41 AM   #8
evo2
Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 219
Thanked: 28
Hi,

I've come up against a similar problem before. What I think might be happening is that the sleep subprocces is not being killed. As suggested by another poster I tried to implement a trap to kill it, but couldn't get it to work. What might work for you is to replace the loop with a recursive call of the script. In this case your example would become:
Code:
#!/bin/bash
sleep 600
date > /dev/null
exec $0
Evo2.
linuxdebian evo2 is offline     Reply With Quote
Old 11-04-2009, 05:55 AM   #9
evo2
Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 219
Thanked: 28
Hm,
stupid me. My recursive script won't work. Please try the following trap. I should kill the whole process group (ie including the sleep) when the parent script is killed. The key here is the minus sign in front of the pid. The -- is needed so that kill does not interpret it a switch.

Code:
#!/bin/bash

trap "/bin/kill -- -$$" EXIT

while true; do
sleep 600
date > /dev/null
done
Evo2.
linuxdebian evo2 is offline     Reply With Quote
Old 11-04-2009, 06:46 AM   #10
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
I tried 'trap "/bin/kill -- -$$" EXIT'. No effect. I also tried adding TERM INT and HUP to the list of things to catch just for the hell of it.

Then I tried a slightly different method of doing the same thing:

Code:
#!/bin/bash
trap "touch /tmp/blah ; pkill -P $$; exit 0" EXIT TERM INT HUP

while true;do
sleep 600
date > /dev/null
done
That doesn't work either. Also /tmp/blah doesn't get touched. It does get touched if I manual kill the process after log out using 'kill PID'. This leads me to wonder if maybe the problem is that when the GNOME session ends the script just isn't being sent any of EXIT TERM INT HUP. Though why that should be or what it could be being sent instead I have no idea.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-04-2009, 07:07 AM   #11
evo2
Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 219
Thanked: 28
Hmm,

how about using a slightly different approach. Perhaps you could used gdm to start and stop your script.

http://library.gnome.org/admin/gdm/s...l.en#scripting

Evo2.
linuxdebian evo2 is offline     Reply With Quote
Old 11-04-2009, 07:23 AM   #12
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
Right now I am using the GDM PostSesssion script to clean up the problem script after a user logs out. However if a user logs in with NX then GDM isn't used, so the script keeps running when user logs out. I can do something with NX to clean the script up as I've done for GDM, but that's just adding in another work around. Rather than keep adding workarounds, I'd really like to solve the problem of why the script doesn't get killed when the GNOME session ends. All the other children of the gnome-session process get killed, yet my script doesn't.
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-05-2009, 11:36 AM   #13
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
I think I've come up with an acceptable solution. It's a workaround, which is irritating, but it's a workaround contained in the script itself rather than one that relies upon another script, such as a GDM session script.

Code:
#!/bin/bash

parentpid=$PPID

while true;do

sleep 600

date > /dev/null

# check if original parent process is still running. 
# if not, exit.
if [ ! -d /proc/$parentpid ];then
   exit 0;
fi


done

The script still does not get killed when the user logs out as, IMO, it really should be, however it does exit of it's own accord within 600 seconds (or whatever value is given to sleep) of the user logging out.

Last edited by arizonagroovejet; 11-06-2009 at 03:33 AM.. Reason: -d not -f - thanks to kdp
linuxsuse arizonagroovejet is offline     Reply With Quote
Old 11-05-2009, 07:55 PM   #14
kbp
Member
 
Registered: Aug 2009
Posts: 489
Thanked: 44
Won't they actually be directories not files ? ... maybe '-d' instead of '-f'

cheers
linuxfedora kbp is offline     Reply With Quote
Old 11-06-2009, 03:37 AM   #15
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE 11.1
Posts: 432
Thanked: 12

Original Poster
Doh. Yeah of course they're directories, not files. I did actually know that despite the evidence to the contrary Thanks for pointing it out. With -f the script would die after 600 seconds even if the original parent process was still running. In testing I was logging in then out again within the 600 seconds and hence my error didn't show up. I've edited the script in my previous post to avoid someone else possibly copy/pasting a non-working version.

I'm sure the script will work properly with the -d but I'll test it later on when I have time just to make sure
linuxsuse arizonagroovejet is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Need help with script to get user ftp logs pdstu25 Linux - Newbie 3 05-26-2009 08:25 PM
Have Proftpd run a script after user logs out derridking Linux - Server 0 04-03-2009 02:44 AM
bash script to use sed for filter mutiples patterns from apache access logs matyu Programming 5 02-06-2008 11:28 PM
What script runs when a user logs in? GuyWhoKilledBear Slackware 6 11-27-2006 10:58 PM
Automaticly run script when user logs in? bigdog0007 Linux - Security 3 06-23-2005 04:14 PM


All times are GMT -5. The time now is 01:16 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration