Use Java programs as Linux's daemon process and prevent programs from run multiple times
Author: frogdan Source: 9CBS
(1) Make the Java program a Daemon process for Linux
The Java program is executed by the Java Virtual Machine (JVM), starting from executing "Java Someprogram" until the end will return the command to the user, and the midway user exits the login or shut down the shell will interrupt the execution of the program. However, there is a need to do some service procedures that do not need to be interactive with users, and you need to stay in memory and do not affect other jobs of users, such as many Daemon processes in Linux. Use the methods described below to make Java programs to:
>
Nohup Java Someprogram &
NOHUP is a command of Linux that can make the process that will be executed into init's sub-process, not affected by other Hangup Signals, only if the kill command or restart the machine disappears. The last "&" command line indicates that the command is returned to the user.
(2) Make the Java program only allowed to run once, ie only one instance is kept in memory.
Sometimes I hope that the program only runs once, and then reminds this program to be running. However, since the Java program is executed by JVM unless you cannot read the processes of the operating system cannot be read from other JVM communications. Java 1.4 has added a NIO class library, where the FileLock class can lock the operation file and you can achieve the above purpose. The following is a method of implementation:
Import
Java.io.
*
;
Import
Java.nio.channels.
*
;
public
Final
Class
OneInstance
...
{Public static void main (string [] args) ... {system.out.println ("Program Start ..."); string filename = new string ("/ tmp / test.txt"); // here you can change filename to Windows format File testFile = new File (filename); RandomAccessFile raf; // Here you can use either FileInputStream or FileOutputStream FileChannel fc; FileLock fl; try ... {testFile.createNewFile (); if (testFile.canWrite () == true) ... {RAF = New RandomaccessFile (Testfile, "RW"); fc = raf.getchannel (); fl = fc.trylock (); if (fl == null || fl.isvalid ) == false) ... {system.out.println ("The File is use by another program!");} Else ... {system.out.println ("The file is locking by me! My name is : " Args [0]); Raf.Writechars (" I am running ... my name is: " args [0]); thread.sleep (1000 * 30); // you c An try to run other program bagween this while fl.release ();} raf.close ();}} catch (filenotfoundexception fnfe) ... {system.out.println (fnfe);} catch (securityException se) .. {System.out.println (se);} catch (closed) ... {system.out.println (cce);} catch (overlappingfilelockexception ofle) ... {system.out.println (OFLE); Catch (IOException IoE) ... {system.out.println (IOE);} catch (exception ex) ... {system.out.println (ex);
} System.out.println ("Program End!");}} Run:
>
Javac OneInstance
.
Java
>
Java OneInstance AbcProgram
Start
...
The file is locking by me! My name is: ABC
(Open another shell within 30 seconds)
>
Java OneInstance DEFPROGRAM
Start
...
The file is using by Another Program! Program
End
!
Note: This is the first one in 9cbs, I hope everyone can criticize advice! If you want to post, please add your own name and source, thank you!