简体   繁体   中英

How can I make a bash script start, stop and restart in ubuntu

I have a script to run java applications on ubuntu

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 &

can I make a .sh script to make the command start, stop and restart the process? thanks

As mentioned in the comment, the better way to do that follow running-a-java-process-from-systemd

But if you are really interested in a bash script, then you can do something like this, but keep in mind there is might be a better approach for that.

#!/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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM