bash 在 Linux Ubuntu 上将 jar 文件作为守护程序运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24268890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:41:40  来源:igfitidea点击:

Run jar file as Daemon on Linux Ubuntu

linuxbashshelljardaemon

提问by Henok

I want to install a bot to my Teamspeak3 and run this bot as a daemon on startup. I wrote my own script and copied it to init.d and then added it with update-rc.dto defaults.

我想在我的 Teamspeak3 上安装一个机器人,并在启动时将此机器人作为守护程序运行。我编写了自己的脚本并将其复制到 init.d,然后将其添加update-rc.d到默认值中。

#!/bin/sh
#
# JTS3ServerBot Script
#
USER="ts"
NAME="jts3"
DIR="/home/ts/jts3/"
case  in
    start)
        echo "Starting ${NAME} ..."
        if [ ! -f $DIR/pid ]; then
            sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
            echo $! > $DIR/pid
            echo "${NAME} started ..."
        else
            echo "${NAME} is already running ..."
        fi
    ;;
    stop)
        if [ -f $DIR/pid ]; then
            PID=$(cat $DIR/pid);
            echo "Stopping ${NAME} ..."
            kill $PID;
            echo "${NAME} stopped ..."
            rm $DIR/pid
        else
            echo "${NAME} is not running ..."
        fi
    ;;
    restart)
        if [ -f $DIR/pid ]; then
            PID=$(cat $DIR/pid);
            echo "Stopping ${NAME} ...";
            kill $PID;
            echo "${NAME} stopped ...";
            rm $DIR/pid

            echo "Starting ${NAME} ..."
            sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
            echo $! > $DIR/pid
            echo "${NAME} started ..."
        else
            echo "${NAME} is not running ..."
        fi
    ;;
esac

A pid file in generated, but if i try to kill the process with this pid i get an error that the process does not exist. If i use topthere is no process with the pid listed.

生成了一个 pid 文件,但是如果我尝试用这个 pid 终止进程,我会收到一个错误,表明该进程不存在。如果我使用top,则没有列出 pid 的进程。

root@vps-1023645-8462:~# service jts3 start
Starting jts3 ...
jts3 started ...
root@vps-1023645-8462:~# cat /home/ts/jts3/pid 
10206
root@vps-1023645-8462:~# kill 10206
bash: kill: (10206) - No such process

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 1762 ts        20   0 1881m  14m 3408 S    0  1.4 215:47.28 ts3server_linux    
32356 ts        20   0  164m 1576 1336 S    0  0.2   0:09.85 tsdnsserver_lin 

回答by Henok

I have found another solution for my problem. I use upstart(works only with Ubuntu) to run my jar-File as a daemon. Upstart manages the PIDs. Just add myservice.confto /etc/init(not /etc/inid.d) and the daemon will be started on boot and you can mangage it as a service. You do not have to make the file runnable or anything else

我为我的问题找到了另一种解决方案。我使用upstart(仅适用于 Ubuntu)将我的 jar 文件作为守护程序运行。Upstart 管理 PID。只需添加myservice.conf/etc/init(not /etc/inid.d),守护程序将在启动时启动,您可以将其作为服务进行管理。您不必使文件可运行或其他任何东西

You can manage the service as normal for example

例如,您可以照常管理服务

service myservice restart
service myservice status
...

My Config-File:

我的配置文件:

description "myservice"
author "your name"

start on runlevel [3]
stop on shutdown

expect fork

script   
    cd /home/username/
    sudo -u username java -jar /home/username/myservice/myservice.jar >/home/username/myservice.log 2>&1
    emit myservice_running
end script

This solution is really easy and works well on my Ubuntu 12.04 Server.

这个解决方案非常简单,并且在我的 Ubuntu 12.04 服务器上运行良好。

回答by nobody

You have an error in this line:

你在这一行有一个错误:

sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>>/dev/null >>/dev/null&

You appear to be mixing the syntaxes of sudoand su. Before version 1.8, sudohad no -coption - you just give it the command to run after any other options. In 1.8 there is a -coption but it's not for specifying the command (it's for limiting resource usage to that of a given login class). sudois printing an error message about this invalid syntax, but you're not seeing it because you're redirecting all the output to /dev/null.

您似乎混合了sudo和的语法su。在 1.8 版之前,sudo没有-c选项 - 您只需给它命令以在任何其他选项之后运行。在 1.8 中有一个-c选项,但它不是用于指定命令(它用于将资源使用限制为给定登录类的资源)。sudo正在打印有关此无效语法的错误消息,但您没有看到它,因为您将所有输出重定向到/dev/null.

Simply remove the -cto form a valid command:

只需删除-c即可形成有效命令:

sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &

Also, you can simplify the command a little by using the 2>&1syntax to send stderrto the same handle as stdout, and there is no need for append mode when writing to /dev/null:

此外,您可以通过使用2>&1语法发送stderr到与 相同的句柄来稍微简化命令stdout,并且在写入时不需要附加模式/dev/null

sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR >/dev/null 2>&1 &