Linux:处理成服务

yal*_*ris 47 linux service process

我正在尝试将linux可执行文件作为服务

我在下面执行我的程序

java -jar mytestprogram.jar
Run Code Online (Sandbox Code Playgroud)

创建一个持续运行并提供REST请求的进程.但我想把它作为一项服务运行,我可以做

service mytestprogram start
service mytestprogram stop
service mytestprogram status
chkconfig mytestprogram on
Run Code Online (Sandbox Code Playgroud)

等最简单的方法是什么?

Spe*_*ave 53

这取决于您的系统管理员

在debian/ubuntu上最常用的方法是构建一个initscript并将其放入/etc/init.d/etc/rc/init.d放置一个以其命名的脚本mytestprogram.

这是一个示例initscript:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          testone
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: Example init script
# Description:       Start/stop an example script
### END INIT INFO

DESC="test script"
NAME=testone
#DAEMON=

do_start()
{
   echo "starting!";
}

do_stop()
{
   echo "stopping!"
}


case "$1" in
   start)
     do_start
     ;;
   stop)
     do_stop
     ;;
esac

exit 0
Run Code Online (Sandbox Code Playgroud)

我建议你在那个目录中看一些脚本,如果你知道bash一点就很简单;)

  • 还记得设置文件可执行文件`chmod + x filename` (7认同)

Mat*_*ira 20

这是一个示例shell脚本(确保将MAT名称替换为您的应用程序的名称):

我使用最新版本的脚本创建了一个GitHubGist,并提供了一个简短的解释来帮助那些需要它的人.GitHub Gist链接

#!/bin/bash

### BEGIN INIT INFO
# Provides:                 MATH
# Required-Start:           $java
# Required-Stop:            $java
# Short-Description:        Start and stop MATH service.
# Description:              -
# Date-Creation:            -
# Date-Last-Modification:   -
# Author:                   -
### END INIT INFO

# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0

# Start the MATH
start() {
    echo "Starting MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        echo "The service is already running"
    else
        #Run the jar file MATH service
        $JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
        #sleep time before the service verification
        sleep 10
        #Verify if the service is running
        $PGREP -f MATH  > /dev/null
        VERIFIER=$?
        if [ $ZERO = $VERIFIER ]
        then
            echo "Service was successfully started"
        else
            echo "Failed to start service"
        fi
    fi
    echo
}

# Stop the MATH
stop() {
    echo "Stopping MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        #Kill the pid of java with the service name
        kill -9 $($PGREP -f MATH)
        #Sleep time before the service verification
        sleep 10
        #Verify if the service is running
        $PGREP -f MATH  > /dev/null
        VERIFIER=$?
        if [ $ZERO = $VERIFIER ]
        then
            echo "Failed to stop service"
        else
            echo "Service was successfully stopped"
        fi
    else
        echo "The service is already stopped"
    fi
    echo
}

# Verify the status of MATH
status() {
    echo "Checking status of MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        echo "Service is running"
    else
        echo "Service is stopped"
    fi
    echo
}

# Main logic
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart|reload)
        stop
        start
        ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload}"
    exit 1
esac
exit 0
Run Code Online (Sandbox Code Playgroud)

  • 这些评论是良好实践,也可供其他人分析使用您的应用程序所需的内容.它们是否与游戏的最低要求相同......它们至关重要吗?不.但是它可以帮助那些使用它的人知道它需要能够使用的东西. (2认同)