@reboot 在 CRON 中不起作用 [英] @reboot is not working in CRON

查看:75
本文介绍了@reboot 在 CRON 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 Ubuntu 服务器开始时运行一个 shell 脚本和一个命令.

I'm trying to run a shell-script and a command at the start of my Ubuntu server.

这是我的CRON

@reboot /home/steam/check.sh
@reboot screen -d -S up -m node /var/www/html/Up1/server/server.js

我在日志中得到了什么:grep CRON/var/log/syslog

What I'm getting in the logs: grep CRON /var/log/syslog

Jul 19 19:48:28 vc1s cron[3185]: (CRON) INFO (pidfile fd = 3)
Jul 19 19:48:28 vc1s cron[3185]: (CRON) INFO (Running @reboot jobs)
Jul 19 19:48:28 vc1s CRON[3209]: (root) CMD (screen -d -S up -m node /var/www/html/Up1/server/server.js)
Jul 19 19:48:28 vc1s CRON[3211]: (root) CMD (/home/steam/check.sh)
Jul 19 19:51:20 vc1s cron[3779]: (CRON) DEATH (can't lock /var/run/crond.pid, otherpid may be 3185: Resource temporarily unavailable)
Jul 19 19:55:01 vc1s CRON[3996]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

我的check.sh.

#!/bin/bash

until screen -d -S unturned -m /home/steam/start.sh; do
        echo "Server 'myserver' crashed with exit code $?.  Respawning.." >&2
        sleep 1
done

我的start.sh.它用于启动 Unturned 游戏服务器.我不认为这个脚本很重要,但我想我应该给你看.

My start.sh. It's for launching an Unturned game server. I don't think this script is important but I guess I should show you.

#!/bin/bash
# This script starts a Unturned 3 server on Linux machines
# Syntax: start.sh <instance name>
# Author: fr34kyn01535

#CONFIG
INSTANCE_NAME=1
STEAMCMD_HOME="./steamcmd"
UNTURNED_HOME="./unturned"

#COLORS
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLLOW='\033[0;33m'
NC='\033[0m'

#Steam checks
STEAMCMD_API=$STEAMCMD_HOME/linux32/steamclient.so
UNTURNED_API=$UNTURNED_HOME/Unturned_Data/Plugins/x86/steamclient.so
printf "Steam: "
if [ -f $STEAMCMD_API ]; then
        if diff $STEAMCMD_API $UNTURNED_API >/dev/null ; then
                printf "${GREEN}UP TO DATE${NC}\n\n"
        else
                cp $STEAMCMD_API $UNTURNED_API
                printf "${YELLLOW}UPDATING${NC}\n\n"
        fi
else
        printf "${RED}NOT FOUND${NC}\n\n"
fi

cd $UNTURNED_HOME

if [ -f RocketLauncher.exe ]; then
        ulimit -n 2048
        mono RocketLauncher.exe $INSTANCE_NAME
else
        echo "RocketLauncher not found."
fi

问题是,如果我从 /home/steam 执行 ./check.sh 它工作正常.坏消息是当我重新启动 VPS 时 @reboot 对我不起作用.

The thing is that if I execute ./check.sh from /home/steam It works fine. The bad news is that the @reboot doesn't work for me when I reboot my VPS.

screen -list 如果我重新启动就不会抛出任何东西.

screen -list doesn't throw anything if I reboot.

我尝试了多种方法但没有奏效,我更改的最后一件事是在 screen 命令中添加 -d 参数,这样服务器就不需要了一个终端来记录启动.

I've tried multiple things but didn't work, the last thing I changed was adding -d parameter in the screen commands so the server wouldn't need a terminal to write down the start-up.

我不确定这里可以做多少工作才能使 @reboot 按预期工作.

I'm not sure how much can be done here to make @reboot work as expected.

如何让我的脚本在启动时运行?CRON@reboot 有没有其他替代品?

How can I make my scripts run on boot? Are there any other alternatives to CRON's @reboot?

提前致谢.

推荐答案

查看 systemd.service 联机帮助页.它描述了如何配置 systemd 来管理服务.我相信您会在 /usr/lib/systemd/system 或类似路径中找到适用于您的系统的示例.

Take a look at the systemd.service manpage. It describes how to configure systemd to manage a service. I am sure you will find examples for your system in /usr/lib/systemd/system or similar paths.

在您的情况下,服务看起来像这样:

In your case, the service would look somewhat like this:

[Unit]
Description=Unturned Game Server

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash /home/steam/start.sh
Type=simple
User=steam
Group=steam
WorkingDirectory=/home/steam
Restart=on-failure

将其放入文件 /etc/systemd/system/unturned.service.然后运行 ​​systemctl daemon-reload(一次,每当你改变 unturned.service 以告诉 systemd 重新读取配置)和 systemctl start unturned.service 启动游戏服务器.

Put this in a file /etc/systemd/system/unturned.service. Then run systemctl daemon-reload (once, and whenever you change unturned.service to tell systemd to re-read the configuration) and systemctl start unturned.service to start the game server.

如果按预期工作,您可以使用 systemctl enable unturned.service 确保它在启动时启动.

If that works as expected, you can use systemctl enable unturned.service to make sure it starts at boot.

关于所用选项的一些说明:

A few notes on the options used:

  • 如果 start.sh 不应该作为用户/组 steam 运行,请适当编辑.
  • WantedByInstall 部分告诉 systemd 当您使用 systemctl enable 启用服务时,哪个目标"(参见 man systemd.target)会拉入服务.
  • Restart 定义了 systemd 在什么情况下会自动重启服务.还有更多与重启相关的选项,您可能想也可能不想更改;请参阅 systemd.service 手册页.
  • If start.sh is not supposed to run as user/group steam, edit appropriately.
  • WantedBy in the Install section tells systemd which "target" (see man systemd.target) pulls the service in when you enable it using systemctl enable.
  • Restart defines under which circumstances systemd will automatically restart the service. There are more restart-related options, which you may or may not want to change; see the systemd.service man page.

这篇关于@reboot 在 CRON 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆