473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shutdown hook

Does any one know if python has the ability to run a shutdown hook.

For example you set a method to run when the python process is shutting
down, like it recieved a kill signal?

Basically looking for an effect like the following java code.
Runtime.getRunt ime().addShutdo wnHook(new Thread(this));

Nov 22 '05 #1
10 10569
Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


When the Python runtime system wants to exit, it raises a SystemExit
exception.

Catch that exception at the top level of your code, and do whatever
you like. (It might be polite to actually exit at some point, of
course. sys.exit(exitco de) will do so -- raising another SystemExit
exception.)

--
\ "I took it easy today. I just pretty much layed around in my |
`\ underwear all day. ... Got kicked out of quite a few places, |
_o__) though." -- Bug-Eyed Earl, _Red Meat_ |
Ben Finney
Nov 22 '05 #2
Thanks do appreciate it

--

Lisp Programming - You don't know what your missing ...

=============== =============== ========
Help Send Laurie to Veterinary School
http://www.sendlaurietovetschool.com

Nov 22 '05 #3
Il 2005-11-15, Ben Finney <bi************ ****@benfinney. id.au> ha scritto:
Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


When the Python runtime system wants to exit, it raises a SystemExit
exception.

Catch that exception at the top level of your code, and do whatever
you like. (It might be polite to actually exit at some point, of
course. sys.exit(exitco de) will do so -- raising another SystemExit
exception.)


I think using atexit module -
http://docs.python.org/lib/module-atexit.html is cleaner

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Nov 22 '05 #4
Il 2005-11-15, Ben Finney <bi************ ****@benfinney. id.au> ha scritto:
Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


When the Python runtime system wants to exit, it raises a SystemExit
exception.

Catch that exception at the top level of your code, and do whatever
you like. (It might be polite to actually exit at some point, of
course. sys.exit(exitco de) will do so -- raising another SystemExit
exception.)


I think using atexit module -
http://docs.python.org/lib/module-atexit.html is cleaner

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Nov 22 '05 #5
Lawrence Oluyede wrote:
Il 2005-11-15, Ben Finney <bi************ ****@benfinney. id.au> ha
scritto:
Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


When the Python runtime system wants to exit, it raises a SystemExit
exception.

Catch that exception at the top level of your code, and do whatever
you like. (It might be polite to actually exit at some point, of
course. sys.exit(exitco de) will do so -- raising another SystemExit
exception.)


I think using atexit module -
http://docs.python.org/lib/module-atexit.html is cleaner

Correct, although this won't catch all cases where a program is exiting.
For example, on Windows, if you use Ctrl+C to terminate a program the
atexit function *is* called, but if you use Ctrl+Break the atexit function
is not called unless you install a suitable signal handler:

import signal
def sigbreak(signum , frame):
sys.exit("***br eak")

signal.signal(s ignal.SIGBREAK, sigbreak)

Even then there are still other ways to terminate a process which will not
be caught.

Nov 22 '05 #6
Lawrence Oluyede wrote:
Il 2005-11-15, Ben Finney <bi************ ****@benfinney. id.au> ha
scritto:
Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


When the Python runtime system wants to exit, it raises a SystemExit
exception.

Catch that exception at the top level of your code, and do whatever
you like. (It might be polite to actually exit at some point, of
course. sys.exit(exitco de) will do so -- raising another SystemExit
exception.)


I think using atexit module -
http://docs.python.org/lib/module-atexit.html is cleaner

Correct, although this won't catch all cases where a program is exiting.
For example, on Windows, if you use Ctrl+C to terminate a program the
atexit function *is* called, but if you use Ctrl+Break the atexit function
is not called unless you install a suitable signal handler:

import signal
def sigbreak(signum , frame):
sys.exit("***br eak")

signal.signal(s ignal.SIGBREAK, sigbreak)

Even then there are still other ways to terminate a process which will not
be caught.

Nov 22 '05 #7
great.
It's a good idea.

Nov 22 '05 #8
great.
It's a good idea.

Nov 22 '05 #9
On 15 Nov 2005 14:45:27 -0800, Steve <st*********@gm ail.com> wrote:
Does any one know if python has the ability to run a shutdown hook.


Look at the "atexit" module.

barbet (~)$ pydoc atexit.register
Help on function register in atexit:

atexit.register = register(func, *targs, **kargs)
register a function to be executed upon normal program termination

func - function to be called at exit
targs - optional arguments to pass to func
kargs - optional keyword arguments to pass to func
--
Steve Juranich
Tucson, AZ
USA
Nov 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2755
by: Jonathan Heath | last post by:
Hi all, I have created an ASP script that enters data into an Access Database. My problem is that I'd like this script to run when the computer is shutdown or the user logs off. I think this would be simple enough if the ASP didn't have any user input, but it does.
0
1892
by: Allan Bredahl | last post by:
Hi All I am trying to construct an application that is able to cancel a machine shutdown, reboot or logoff. And after performing some stuff to perform the original shutdown order : Shutdown/reboot/logoff. I have tried this : AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf
3
1189
by: Senthil | last post by:
Hi, I would like to know the code for reboot and shutdown windows xp professional using VB.Net. thanx Senthil
4
18097
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call...
2
11391
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess, OpenProcessToken, LookupPrivilegeValue, AdjustTokenPrivilegese, ExitWindowsEx. I am trying to avoid using any API calls if possible and to use...
4
9593
by: yaron | last post by:
Hi, I am developing an infrastructure dll in c#. my dll can be use by all kind of application containers win/web/console. how do i implement a shutdown hook which will be called when the user exit from the application (for all kind of .NET applications)? for example in c# console application i do this with win32 interop...
0
2483
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form containing a Edit(textbox) and button to test at first. When I change text in Edit or press button to setText value, I want to monitor the change event. ...
5
17395
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
0
1696
by: lawbaal | last post by:
Here I got 3 computers A,B and C, they are all with windows xp pro running. Above all: I've open the remote force shutdown permission for guest account in all of these three computer. But.... 1.I tried to remote shutdown B from A by using command: shutdown -s -m \\ip -t 0 -f The result is "Access is denied". Then I thought there maybe...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.