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