what's the best design pattern to check to see if an app has shutdown cleanly(eg. didn't crash)?
It seems creating a lock file in the current directory and deleting the file upon normal shutdown is a good way, or is it?
are there better ways?
The app is in java, so I plan to add a shutdown hook to the instance to delete the lock file. is this a good way?
            Asked
            
        
        
            Active
            
        
            Viewed 122 times
        
    0
            
            
        
        user678070
        
- 1,415
 - 3
 - 18
 - 28
 
- 
                    possible duplicate of [how-to-implement-a-single-instance-java-application](http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application) – happymeal May 29 '11 at 12:07
 
1 Answers
0
            Adding a shutdown hook doesn't seem ideal to me, as I'd expect a shutdown hook to still be executed if the application crashed. I'd just make the deletion part of "normal system exit".
But yes, having a file seems pretty reasonable to me. One caveat though - consider what would happen if your application were opened twice or more concurrently. You may want to create a randomly-named file instead of one with a fixed name.
        Jon Skeet
        
- 1,421,763
 - 867
 - 9,128
 - 9,194
 
- 
                    @Jon Skeet: I'm going to implement single instance application, would this also rely on a lock file? – user678070 May 29 '11 at 11:59
 - 
                    @Jon Skeet: if I create a randomly named file and crashed, upon restart, how do I know what name I gave the file? – user678070 May 29 '11 at 12:00
 - 
                    @user678070: If you're only going to allow a single instance to be launched at a time, then a fixed name is fine. Otherwise, you could give your files a *pattern* (e.g. (randombit).lock) and look for files matching the pattern. – Jon Skeet May 29 '11 at 12:05
 - 
                    @Jon Skeet: I just tried, it seems that shutdown hook does not get executed when the app is terminated abnormally. – user678070 May 29 '11 at 12:08
 - 
                    @Jon Skeet: it seems that using java preference api might be a better way to persist this information than using a lock file. – user678070 May 29 '11 at 12:18
 - 
                    @user678070: I'm not sure I'd use a preference API for this myself, to be honest. It doesn't sound like a good fit to me. And your point about the shutdown hook depends on *how* the abnormal termination occurs. It may not happen if you kill the process externally - but what about if it crashes due to an exception? – Jon Skeet May 29 '11 at 12:48