如何在 ubuntu 中启动、停止和重启 bash 脚本

我有一个在 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脚本来使命令启动、停止和重新启动进程吗?谢谢


胡说叔叔
浏览 277回答 1
1回答

墨色风雨

如评论中所述,更好的方法是遵循running-a-java-process-from-systemd但是如果你真的对 bash 脚本感兴趣,那么你可以做这样的事情,但请记住,可能有更好的方法。#!/bin/bashfunction 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java