In fact, I want to develop a Windows system service.Program. After checking the documents for a long time, I did not find any way to develop the Linux daemon in Java. C language development is too troublesome, not to mention embedding the business. You can also use servlet, but you need to install Tomcat.
After a day of experiment, I finally found that it was so simple!
Easy to startCodeUpdate the job. log file every five seconds. Used for testing.
Import Java. io. bufferedwriter; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. util. date; public class program {/*** @ Param ARGs */public static void main (string [] ARGs) {file F = new file ("/home/xieping/job. log "); If (! F. exists () {try {f. createnewfile ();} catch (ioexception e) {e. printstacktrace () ;}while (true) {try {bufferedwriter output = new bufferedwriter (New filewriter (f); output. write (new date (). tostring (); output. close () ;}catch (ioexception E1) {e1.printstacktrace () ;}try {thread. sleep (1000*5);} catch (interruptedexception e) {e. printstacktrace ();}}}}
Package. I used eclipse to package the package. Just export a jar package.
Put the file in the/home/xieping/directory. File Name: job. jar.
Set to daemon !!
# Java-jar job. Jar &
Simply add an & symbol. You can view
# Ps aux | grep job
As you can see, it started.
# More/home/xieping/job. Log
The file time has also been changed.
Note that the process may not be terminated when multiple threads are enabled.
# Killall Java or # The kill process ID is invalid. Force termination is required.
# Kill-9 process ID.