Start the Java program using python, which is easy to share.

Source: Internet
Author: User

Recently deployed JavaProgramStarted with shell, the startup process is unfriendly and inconvenient, and there is no Daemon. This year, we found a collection of startup scripts during data sorting.

Very convenient. Record. To avoid forgetting

This is my Java program structure

A jar file, a Config configuration file, and a lib dependent package.

Paste the appstoreserver. py scriptCode

#! /Usr/bin/pythonimport OS, sys, time, commands_program = 'Appstoreserver. jar' _ Daemon = 'Appstoreserver. py' Def getprogrampid (): Result = commands. getoutput ("PS aux | grep Java \ | grep % S | grep-V grep | awk '{print $2}'" % _ program) return resultdef getdaemonpid (): result = commands. getoutput ("PS aux | grep Python \ | grep '% s monitor' | grep-V grep | awk '{print $2}'" % _ Daemon) return resultdef startprogram (): p_pid = getprogrampid () If p_pid! = '': Print ('It seems this program is already running... ') else: Print ('starting program... ') If OS. system ('nohup Java-dfile. encoding = UTF-8-server-xms1024m-xmx1024m-xmn256m \-CP % s: Config/: lib /*   Com. xxxx. Bootstrap > Log/stdio. log 2> & 1 & '% _ program) = 0: Print ('start program successfully and PID is' + getprogrampid () def startdaemon (): d_pid = getdaemonpid () if d_pid! = '': Print ('It seems this daemon is already running... ') else: Print ('starting daemon... ') If OS. system ('nohup Python % s monitor> log/daemon. log 2> & 1 & '% _ Daemon) = 0: Print ('start daemon successfully and PID is' + getdaemonpid () def stopprogram (): p_pid = getprogrampid () if p_pid = '': Print ('It seems this program is not running... ') else: OS. system ('Kill '+ p_pid) print ('program stopped ') Def stopdaemon (): d_pid = getdaemonpid () If d_pid = '': Print ('It seems daemon is not running... ') else: OS. system ('Kill '+ d_pid) print ('daemon stopped') def Monitor (): While 1: time. sleep (10) p_pid = getprogrampid () If p_pid = '': Print ('It seems this program is not running. start it now! ') Startprogram () If _ name _ =' _ main _ ': If (LEN (sys. argv) = 2): ARGs = sys. argv [1] else: ARGs = raw_input ('enter ARGs: ') logpath = OS. path. curdir + OS. SEP + 'log' if not OS. path. exists (logpath): OS. mkdir (logpath) If ARGs = 'start': startprogram () startdaemon () Elif ARGs = 'stop': stopdaemon () stopprogram () Elif ARGs = 'restar ': stopdaemon () stopprogram () time. sleep (3) startprogram () startdaemon () Elif ARGs = 'monitor': Monitor () else: Print ('Nothing to do ')

You only need to modify your jar and main class entries (this is the com. XXX. Bootstrap section in this article ). Python calls the nuhop command, and exit the terminal and run it in the background, which is quite convenient. The key point is that there are daemo processes.

 

Modified to start the Java program.

Close

 

Related Article

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.