aspose file tools*
The moose likes Linux / UNIX and the fly likes Signal to a process started in Login session during logout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Signal to a process started in Login session during logout" Watch "Signal to a process started in Login session during logout" New topic
Author

Signal to a process started in Login session during logout

Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Hi All,

I start a process in background mode in user login session by placing it in user's profile. [I place a entry in .bash_profile to run a program in background].

Now if I choose logout from the left bottom menu of the GUI, what signal would be sent this my process?

I shall appreciate any help/comments/suggestion onto this.

Regards,
~Bhopal
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16142
    
  21

Should be SIGHUP (as in Hang up).

There's a "nohup" command for situations where you want to keep running after logout, BTW.
[ August 12, 2005: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
I thought of the same and wrote a sample C++ program that catch the SIGHUP and in its handler it just flushes out all the information [Signal No, Signal code, the send process id etc] on a file and then exit by calling exit(0).
Now I compile this C++ and placed the Executable in my bash profile so that it can be started at login in background mode.

Now I logoout [Using my Desktop manager's logout button] and expected to see the SIGHUP info written on that file.

But unfortunately I did not find any file and hence assuming either it is some other signal or a process started in background mode in login shell do not recieve SIGHUP.

Any thoughts/suggestions?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Just guessing: SIGTERM, like in TERMinate?
[ August 13, 2005: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Mahesh Rana
Ranch Hand

Joined: Sep 05, 2001
Posts: 139
Bhopal,
You can try this:
add following line to your .bash_logout file,

killall -15 name_of_your_program

or

kill -15 pid_of_you_program


It will send SIGTERM to the process.
[ August 13, 2005: Message edited by: Deep Narsay ]

SCJP2
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I would expect at least a SIGHUP first.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16142
    
  21

I'd check for SIGTERM. Form a command line, I'd expect SIGHUP, but this is a GUI desktop shutting down its apps, so it's reasonable to expect that the GUI itself will trigger some sort of shutdown signals before the actual logout process begins.

Just guessing, since I've never had to deal with that sort of thing in the Unix world, but, like I said, reasonable.
Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Thanks everybody for you response.

Let me rephrase my objective.

I'm writting an application [GUI in GTK] which can be started in following three manners:
1. At the user login [by placing a line myApplication& in user profile]. Placing the application at appropriate location so that it can be started at start up for KDE and GNOME is another possible solution bu this requires a no of hacks and work around and hence wanted to avoid this.
2. Manual invokation by the user by type on any shell [./myApplication <arguments>]
3. Or by a browser as a helper application in respose to a particular MIME type.

My objective is to ensure a graceful exit [wanted to perform some cleanup activities before my appliaction shutdown]of my application in following three scenario.

1. User logout.[I'm not sure which signal is sent when user logout using the Desktop manager's logout button]
2. User close the shell from which he has start my application.[I can catch sighup and can do the cleanup]
3. User closes the browser window which invoke my application as a helper application.[No idea at all how to handle this]

The solution I'm looking for should be generic enough to support various distribution of Linux [Redhat, SuSe and Mandrake atleast] and with all the possible shells the user can opt for.

I believe .bash_logout solution is applicable only if linux user using BASH as his shell. Please correct me if I'm worng.

I would appreciate any pointer/suggestion/solution to achieve the desired scenario.

Regards,
~Bhopal
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

isn't WindowListner nClosing () (or similar - don't have my browser at hand the way to go?

[ August 17, 2005: Message edited by: Stefan Wagner ]
[ August 17, 2005: Message edited by: Stefan Wagner ]
Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Any linux guru with his expert comments/suggestions?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Sorry - I have overseen the 'gtk'-note.
Gtk is c/ c++ - isn't it?

I used this way on a CLI-Application, but I think it will work in a gui-environment too:
In the main method I registered a signal-handler:

I guess it will work, but I'm not sure.

[ August 21, 2005: Message edited by: Stefan Wagner ]
[ August 21, 2005: Message edited by: Stefan Wagner ]
Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Thanks Stevens,

The million dollar question here is what all Signals[SIGHUP/SIGTERM ...] I need to handle in my code.
As described in my problem statement if I start my application in background mode while the linux user logs in, the application is not sent SIGHUP at the time of logout.
I found the link http://www.faqs.org/faqs/unix-faq/programmer/faq/ pretty useful and hence thought of sharing it.
Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Hi All,

I've found the solution for 2 of desired scenarios in my original request.

Now I wanted to know how the Display manager [I'm interested in Gnome and Kde only] notifies the application started at Start Up [by configuring the Sessions Preference for Gnome and by placing your executable in .kde/Autostart folder for KDE] to shutdown when user logs out of the session?

I expected Signals SIGHUP or SIGTERM but not did not recieve any of them.

I would appreciate any pointers/suggestions.

Regards,
~Bhopal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Signal to a process started in Login session during logout