LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to capture logout signal (https://www.linuxquestions.org/questions/programming-9/how-to-capture-logout-signal-337275/)

thinkgeek 06-26-2005 06:58 AM

How to capture logout signal
 
Hello!

I am writing a daemon in python. In my program, I want to capture a signal for a particular event and perform some specified action. Below are more details.

1. The action to capture, is to know when the user who is currently logged in, logs out.
2. On his logout I want to update some entries into the Postgres database which will already be in connection with my program.

Bye!

thinkgeek

microsoft/linux 06-26-2005 09:32 AM

I'm sure this not the type of solution your looking for, but why not have your program run 'finger' every so often, then once there is nothing returned, put the data in your database. Just a thought

shakezilla 06-26-2005 11:16 PM

Don't know about having the python daemon listening for logout.
Don't know if you can even listen for that.

What you can do is use the file "~/.bash_logout". If you look at
the bash man page, you should find it. This is from my man page:

Code:

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
Maybe that messes up your idea with the python daemon, but you
could still use IPC from python to fix the problem. Or if you can, just
take the pgsql code out of your daemon and put it in a separate
python script which is started from .bash_logout

thinkgeek 06-27-2005 09:41 AM

but i want to control the entire process centrally using a daemon.

is there no signal which the user sends before logout?
how the system knows that the user is about to logout?

prior to the logout of every user connected to NIS server i want to
perform some update activities.

thinkgeek

frandalla 06-27-2005 11:16 AM

I think I found what we need =)
(http://www.cactus.org/~dak/shellscript.html)
Let's google guys! :D

Process Handling

*
Signals

Name Number Control Character
o EXIT 0 Used to trap exiting script
o HUP 1 Logout
o INT 2 Control-C
o QUIT 3 Control-\
o KILL 9 can not be ignored or trapped
o TERM 15 Default kill
o TSTP 24 Control-Z

*
Traps

o trap "" signal-list Ignore signal
o trap "cmds" signal-list Execute commands if signal is caught
o trap signal-list Reset signal to original condition
o trap : signal-list (undocumented) ignore signal, pass to child
Signal are normally not passed to subprocesses
o Examples
trap 'rm tmpfile; exit' 0 1 2 #remove tmpfile on exit, logout, interrupt

trap "echo 'You hit Control-C'" INT
while true ; do
sleep 60
done
o Example parent child process
#!/bin/bash #parent
echo parent running
trap 'echo parent exiting; exit' 0
trap :2
child
sleep 1000

#!/bin/bash #child
echo child started. pid is $$
trap 'echo child got signal 2; exit' INT
sleep 1000


All times are GMT -5. The time now is 06:44 AM.