• 通过python启动java程序,挺方便的分享下。


    最近部署了java程序,刚开始用shell来启动,启动过程不友好也不方便,也没有守护进程。今年在整理资料的时候,发现了珍藏一套启动脚本。

    挺方便的。记录下。以免忘记

    这是我的java程序结构

    一个jar文件,一个config配置文件 一个lib依赖包。

    重点贴出 appStoreServer.py脚本代码

    #!/usr/bin/python
    import 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 result
    
    def getDaemonPid():
        result = commands.getoutput("ps aux | grep python \
            | grep '%s monitor' | grep -v grep | awk '{print $2}'" % _daemon)
        return result
    
    def 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 == 'restart':
            stopDaemon()
            stopProgram()
            time.sleep(3)
            startProgram()       
            startDaemon()
        elif args == 'monitor':
            monitor()
        else:
            print('nothing to do')  
    

      只需要修改你的jar 和main class 入口(这就是文中 com.xxx.bootstrap部分)。python调用了nuhop命令,退出终端也可以后台运行,挺方便的,重点的是还有daemo进程

    修改了,开始启动 java程序

    关闭

  • 相关阅读:
    java算法--循环队列
    java算法--普通队列
    java算法--稀疏数组
    HelloWorld
    css
    自定义事件并且主动触发
    数组字符串操作
    进阶路上有你我-相互相持篇之ES6里箭头函数里的this指向问题
    关于一道面试题
    异步函数回调
  • 原文地址:https://www.cnblogs.com/montya/p/python.html
Copyright © 2020-2023  润新知