LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux 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
 
LinkBack Search this Thread
Old 11-03-2009, 07:00 AM   #1
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Rep: Reputation: 143Reputation: 143
BASH script won't die when user logs out.


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.
 
Old 11-03-2009, 08:51 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 2,773

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Try putting it under '~/.config/autostart' ... should do the job

cheers
 
Old 11-03-2009, 09:31 AM   #3
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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.
 
Old 11-03-2009, 10:15 AM   #4
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 5,995

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Which user is the script running as?
ps -Al | grep why_wont_you_die will return the user's UID as the third field.
 
Old 11-03-2009, 11:00 AM   #5
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
The script runs as the same user that ran the GNOME session which invoked it.
 
Old 11-03-2009, 04:38 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 2,773

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Maybe you need to trap signals, this may help:
http://www.linuxquestions.org/questi...m-bash-257157/

cheers
 
Old 11-04-2009, 05:34 AM   #7
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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.
 
Old 11-04-2009, 05:41 AM   #8
evo2
Senior Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 2,500

Rep: Reputation: 321Reputation: 321Reputation: 321Reputation: 321
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.
 
Old 11-04-2009, 05:55 AM   #9
evo2
Senior Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 2,500

Rep: Reputation: 321Reputation: 321Reputation: 321Reputation: 321
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.
 
Old 11-04-2009, 06:46 AM   #10
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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.
 
Old 11-04-2009, 07:07 AM   #11
evo2
Senior Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 2,500

Rep: Reputation: 321Reputation: 321Reputation: 321Reputation: 321
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.
 
Old 11-04-2009, 07:23 AM   #12
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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.
 
Old 11-05-2009, 11:36 AM   #13
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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
 
Old 11-05-2009, 07:55 PM   #14
kbp
Senior Member
 
Registered: Aug 2009
Posts: 2,773

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Won't they actually be directories not files ? ... maybe '-d' instead of '-f'

cheers
 
Old 11-06-2009, 03:37 AM   #15
arizonagroovejet
Member
 
Registered: Jun 2005
Location: England
Distribution: SLED 11, openSUSE 11.4
Posts: 948

Original Poster
Rep: Reputation: 143Reputation: 143
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
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 05:06 PM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration