如何在Ubuntu中启动,停止和重新启动bash脚本

时间:2019-07-17 04:41:53

标签: java linux bash ubuntu

我有一个脚本可以在ubuntu上运行Java应用程序

nohup java -Xms512m -Xmx8g -jar /data/software/190716/myapp-0.0.1.jar --server.port=8080 --spring.config.location=/data/software/myapp.properties > ~/running.myapp.log &

我可以制作一个.sh脚本来启动,停止和重新启动命令吗? 谢谢

1 个答案:

答案 0 :(得分:-1)

如评论中所述,更好的方法遵循running-a-java-process-from-systemd

但是,如果您真的对bash脚本感兴趣,则可以执行类似的操作,但是请记住,可能有更好的方法。

#!/bin/bash

function start_app() {

    # tested with this , uncomment your command
    nohup node bin/www > logfile.txt  2>&1 & 
    # nohup java -Xms512m -Xmx8g -jar /data/software/190716/myapp-0.0.1.jar --server.port=8080 --spring.config.location=/data/software/myapp.properties
    # write the pid to text to file to use it later
    app_pid=$!
    echo "Process started having PID $app_pid"
    # wait for process to check proper state, you can change this time accordingly 
    sleep 3
    if ps -p $app_pid > /dev/null
    then
        echo "Process successfully running having PID $app_pid"
        # write if success
        echo $app_pid > process_id.txt
    else
        echo "Process stopped before reached to steady state"
    fi
}

function stop_app() {
    # Get the PID from text file
    application_pid=$(cat process_id.txt)
    echo "stopping process, Details:"
    # print details
    ps -p $application_pid
    # check if running
    if ps -p $application_pid > /dev/null
    then
        # if running then kill else print message
        echo "Going to stop process having PId $application_pid"
        kill -9 $application_pid
        if [ $? -eq 0 ]; then
        echo "Process stopped successfully"
        else
        echo "Failed to stop process having PID $application_pid"
        fi
    else
        echo "Failed to stop process, Process is not running"
    fi
}


case "$1" in 
    start_app)   start_app ;;
    stop_app)    stop_app ;;
    restart) stop_app; start_app ;;
    *) echo "usage: $0 start_app|stop_app|restart_app" >&2
       exit 1
       ;;
esac

enter image description here