Java Development daemon

Source: Internet
Author: User

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.
 
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.