|
Replies:
13
-
Last Post:
Dec 22, 2006 6:54 AM
by: mthornton
|
|
|
|
|
|
|
Detect user logout or system shutdown
Posted:
Dec 20, 2006 9:06 PM
|
|
|
Hi,
Is it somehow possible to detect when a user logs out from its operating system or when the system is shutting down?
Reason for asking is that I want a scheduled task to be performed when one of these events occurs.
I have taken a look at Runtime.addShutdownHook but the API says the following:
"When the virtual machine is terminated due to user log off or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook."
And as the scheduled task may take anything from a couple of seconds to hours this doesn't seem like an appropriate solution.
Is there any other way to accomplish this in Java?
Thanks!
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 6:04 AM
in response to: diod
|
|
|
>Is there any other way to accomplish this in Java?
Doubtful, and actually I think it sounds problematic in any language to fire up something just as the system prepares to terminate...
In Linux/Unix you have the rc.d shutdown scripts which are executed by init when the runlevel changes or a shutdown initializes, so you might be able to put a little shell script in there which starts a new java process. However, Windows (even the different versions) have very different API and behaviour. I don't think you can rely on the OS letting your new process to live.
What is it that requires such a potentially long process to fire up at shutdown? Are you saving some state or progress? If so perhaps you could redesign your solution so that you save it incrementally.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 9:35 AM
in response to: diod
|
|
|
"the scheduled task may take anything from a couple of seconds to hours"
You've got me curious about what that could be. Optimization can sometimes produce amazing results, so perhaps there is hope of always making it quick.
I don't think you need to worry about the operating system. Most users will cut the power if a shutdown is still in progress after 10 minutes. You need to give the user some kind of status information if you don't want that to happen. There is also the possibility that the shutdown is due to a power failure.
You may also want to vote for this RFE:
"Enhancements for the Shutdown Hooks API" http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5036297
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 10:41 PM
in response to: coxcu
|
|
|
The reason for this is that I am developing a personal backup tool and I want to schedule backups when I log out of the system or when I want to shutdown the computer.
I want the backup to perform at log out since it is the only time I am not actively working on the computer. During startup or any other time it could slow down the computer while I am working with it and this is something I would like to avoid.
The only approach besides log outs that I can imagine would be useful would be to detect if the computer is idle and perform backups then.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 11:07 PM
in response to: diod
|
|
|
To specify a bit more, if there is a power failure or if I terminate the task that is ok. Otherwise the task should detect the log out and suspend it until the backup has finished.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 22, 2006 1:47 AM
in response to: diod
|
|
|
You could look at the apache common Daemon package. Start your program as a system service/daemon and use the shutdown handling facilities.
It doesn't sound like good practice, however, creating a long delay on system shutdown. What if you simply meant to reboot and wanted the system to go down and come back as fast as possible? Would you remember to stop your backup thingy?
Can't you run it as a cron job in the small hours or something?
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 22, 2006 4:40 AM
in response to: diod
|
|
|
I've used a backup system that worked this way and it was VERY annoying. Backups are best done at night, if necessary automatically starting the computer and shutting down again once complete.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 22, 2006 4:48 AM
in response to: mthornton
|
|
|
My computer is almost never turned on at nighttime, which rules out that option.
The task will of course present a GUI that will present the option to end the backup process.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 22, 2006 4:55 AM
in response to: diod
|
|
|
> My computer is almost never turned on at nighttime, > which > rules out that option.
The idea is that the computer is scheduled to turn on again (or resume from hibernation) at the time the backup is due to start. Once the backup is complete the computer shuts down or hibernates again.
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 22, 2006 6:54 AM
in response to: coxcu
|
|
|
> If the backup is ever going to complete in a few > seconds, you will need access to filesystem change > APIs.
Even with those it won't always complete in a few seconds. You need a system that still works well even when there have been many changes, or the change logging has failed (when the system is under stress, change notifications can get lost).
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 3:24 PM
in response to: diod
|
|
|
Are you 100% certain that the OS even makes this information available to a process being terminated? Do you know of the specific OS API calls that the JVM should be making? If so, feel free to mention them. I suspect there isn't a native API to do what you're asking for.
If there is such a thing, is there a platform that does not? It would be nice to collect this information before pushing the RFE further.
If you only need a Windows-specific solution, might I suggest you code this up in JNI to begin with and move on from there? You could possibly add a task to the Task Manager to be invoked on system shutdown, then have that task send a message to your other JVM. Just a thought.
Gili
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 3:53 PM
in response to: cowwoc
|
|
|
The API presented in the RFE is a strawman and says as much. It was included to provide a simple counter to the assertion that "it's impossible to generalize such information in a portable way". The information provided is generalized and portable. I'm sure a much better job could be done by a real domain expert.
I don't know what operating systems make this information available, or what the appropriate calls are. The possibility that the information is unavailable is explicitly put in the API. If the information isn't provided by the implementation, it simply returns null. A better design would have been to define an enum, rather than overloading Boolean, but again the API is just a strawman.
Here's the argument I was countering:
"Why don't you provide information as to why the VM is shutting down?
On some platforms a native process can't distinguish a shutdown due to exit from a shutdown due to termination. Other platforms provide much richer capabilities, in some cases including notification of system suspension and restart or of imminent power failure. In short, it's impossible to generalize such information in a portable way."
"Design of the Shutdown Hooks API" http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html
|
|
|
|
|
|
|
|
Re: Detect user logout or system shutdown
Posted:
Dec 21, 2006 4:18 PM
in response to: coxcu
|
|
|
Sorry for not reading the RFE in more detail.
Isn't this issue similar to System.getEnv()? If not all platforms can provide meaningful values, is it meaningful to add this to the core API? Also, this is reminiscent of Object finalizers: if there is no guarantee they will ever get invoked, do we really want to have developers to depend upon them (some people do, regardless of how many warnings they get to the contrary)?
I don't have answers, just questions Is it possible to develop a reference implementation that plugs into the JVM or is this something that can only really be implemented on the inside?
|
|
|
|
|