How do you run a script upon autologout in bash?

Infohawk

Lifer
Jan 12, 2002
17,844
1
0
Hello,

I ssh to a shell account. I use a bash shell. I know you can set an autologout using

export TMOUT = Seconds_To_Wait_for_command_before_exiting_shell

but when I do that bash doens't run my logout script (.bash_logout). The .bash_logout script only runs when I run logout. Anyone know of a way I can run a script and logout after a certain amount of time in bash?

Thanks
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
You could add something like

sleep 30
exit


to the end of your .bashrc. Not sure exactly what you're after, though.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
In .bash_profile or .bashrc, add:

timeout=30 #change this
PROMPT_COMMAND="killall sleep; (sleep $timeout && exit) &"

Only prob is that you can't really use sleep(1) very reliably for other things, because it'll get killed whenever you enter a command. :) Can't think of a better way around that..
 

Infohawk

Lifer
Jan 12, 2002
17,844
1
0
cleverhandle,

Wow. That screwed me. :frown:

The "exit" makes my login automatically logout! In otherwords, ssh and telnet to that account becomes unusable. Used ftp to repair it.

I'm trying to have my ssh session autologout after a certain amount of time, at which time the script .bash_logout is run.

The problem with the autologout variable is that it doesn't run the .bash_logout script. It just exits. My issue is that I need that script to run at logout.

BingBongWongFooey,

Thanks. I tried that. Can't say I really understand what it does it looks good. :D

When I put that in I get, "no matching processes belonging to you were found" upon login. Am I supposed to replace PROMPT_COMMAND with something? Were there any other variables I missed?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Infohawk
cleverhandle,

Wow. That screwed me. :frown:

The "exit" makes my login automatically logout! In otherwords, ssh and telnet to that account becomes unusable. Used ftp to repair it.

I'm trying to have my ssh session autologout after a certain amount of time, at which time the script .bash_logout is run.

The problem with the autologout variable is that it doesn't run the .bash_logout script. It just exits. My issue is that I need that script to run at logout.

BingBongWongFooey,

Thanks. I tried that. Can't say I really understand what it does it looks good. :D

When I put that in I get, "no matching processes belonging to you were found" upon login. Am I supposed to replace PROMPT_COMMAND with something? Were there any other variables I missed?

Oh, oops. Didn't think of that. :p

Basically, $PROMPT_COMMAND is run every time your prompt appears, just before it appears. So every time you run a command (or more accurately, just after one finishes), it sets up a timer for $timeout seconds that will exit once the timer is done. But before it runs that timer, it runs killall sleep to kill the other timer(s) running. && means "if the first command ran successfully, execute the next one and return its exit value." & means "run the command before this in the background." Putting the foo && bar inside of parentheses makes them look like one command to the &.

Anyways.. the problem with your error message is that there isn't always an instance of sleep running (like when you first log in), so killall complains. This will shut it up (changes in bold):

timeout=30 #change this
PROMPT_COMMAND="killall sleep >/dev/null 2>&1; (sleep $timeout && exit) &"

This is still kinda dirty, because you can only have one shell open like this (and have it actually work), and no other instances of sleep can be running, otherwise they'll get killed. Hrmm..

Ok, here's something you can do.

ln -s /bin/sleep $HOME/.mysleep # do this one interactively, don't put it in your shell init files

Then use this PROMPT_COMMAND:

PROMPT_COMMAND="killall .mysleep >/dev/null 2>&1; ($HOME/.mysleep $timeout && exit) &"

This will make the sleep command run, but appear in ps and whatnot as .mysleep instead of sleep, so you can safely use sleep for whatever.

...however, you can still only use one shell at a time. I think I could get around that too, by regenerating the link on every login and using the shell's pid to determine the filename.. do you ever have more than one login going at once?
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
(google is my friend)


I the BASH shell has a autologout feature built into it.

If you set the variable TMOUT you can set it up to log you out after however many seconds or so.

something like
export TMOUT=30

Is that what your looking for?

 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: drag
(google is my friend)


I the BASH shell has a autologout feature built into it.

If you set the variable TMOUT you can set it up to log you out after however many seconds or so.

something like
export TMOUT=30

Is that what your looking for?

Reading the original post helps. ;)
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: BingBongWongFooey
Originally posted by: drag
(google is my friend)


I the BASH shell has a autologout feature built into it.

If you set the variable TMOUT you can set it up to log you out after however many seconds or so.

something like
export TMOUT=30

Is that what your looking for?

Reading the original post helps. ;)

Well that would just be a waste of my time, hrmph!
:p
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Well if that wasn't good enough.... how about this.


put something like:

/usr/bin/bash --rcfile .bashrc2
logout

at the end of your .bashrc script.
then in the .bashrc2, make a copy of the original .bashrc file and at the end of that put:

export TMOUT=30

instead. A nice, simple and very ugly hack. :)
 

Infohawk

Lifer
Jan 12, 2002
17,844
1
0
Originally posted by: BingBongWongFooey
Originally posted by: Infohawk

Ok, here's something you can do.

ln -s /bin/sleep $HOME/.mysleep # do this one interactively, don't put it in your shell init files

Then use this PROMPT_COMMAND:

PROMPT_COMMAND="killall .mysleep >/dev/null 2>&1; ($HOME/.mysleep $timeout && exit) &"

do you ever have more than one login going at once?

THank you for the help.

I added / edited in the above lines into my .profile.

Here's what happens. After the timeout period ends, nothing happens. When I type a command at the console (after the period ends), I get:

[2]+ Done ( ~/.mysleep 1 && exit )

But nothing's logged out. Might this be a problem because I'm logged in more than once? I try not to login more than once (don't really need to since it's just a shell account), but sometimes I get disconnected due to flaky wireless and I need to relogin.

Could there be another way around this?

All i really want to do is not have squid proxy on all the time. I like squid to start up when I log in, and stop when I log out. This generally works if I remember to logout, but I'd like it to do it automatically. Is there anyway to load a program so that it expires after a while? Perhaps I could have cron shutdown squid every couple hours? (How would i do that?)

I'm on a bash shell in freebsd in case that helps. Thanks for the help!
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
Sorry if I got you in trouble before - I couldn't really tell what you wanted. I was a bit tired, and probably should have mentioned the potential dangers.
rolleye.gif


Is there some particular reason you want to turn off squid? If you're not logged in and pulling down web pages, it's really not going to take up resources or anything?

Anyway, you could rig a combo of rc scripts and cron jobs that are less precise but more predictable than the above. Set a TMOUT to autoterminate the shell after an inactivity period. Then create a script that says something like

#!/bin/sh

if [ -z "`who | grep yourusername`" ]; then
killall squid
fi

Then run that via cron every 10 minutes or whatever. The details of setting up a cron job vary a bit between OS's, and I don't know FreeBSD at all. But checking out the cron and crontab man pages should get you set up in short order.