Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hooking into the windows restart or shutdown commands

 
Shawn Diotte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wonder if anybody knows how to take shut down a java program when the restart or the shutdown command is given in windows. What i mean is i want to be able to hook into this command and have my application shutdown automatically before windows shuts down.
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it isn't already? From the JavaDocs for java.lang.RunTime.addShutdownHook (jdk 1.4)
The Java virtual machine shuts down in response to two kinds of events:
* The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
* The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the Shutdown Hooks API.
Of course if the system CRASHES down it might not work.
From an article:

But what if someone presses CTRL+C and shutdown the VM, or sends a kill signal in Unix/Linux?
Will your memory-buffered logfile not be written to disk, with the loss of important information as a consequence?
The only way to make some code execute right before the VM shuts down is using shutdown hooks. And that's exactly what this article will teach you.
 
Shawn Diotte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about this type of hooking but what i am looking for is when Windows goes to shutdown or restart.....is there a way for us to see that Windows is doing this and can we do stuff based on these events?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic