bash @reboot 在 CRON 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38465751/
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
@reboot is not working in CRON
提问by Jose Serodio
I'm trying to run a shell-script and a command at the start of my Ubuntu server.
我试图在我的 Ubuntu 服务器开始时运行一个 shell 脚本和一个命令。
Here is my CRON
这是我的 CRON
@reboot /home/steam/check.sh
@reboot screen -d -S up -m node /var/www/html/Up1/server/server.js
What I'm getting in the logs:
grep CRON /var/log/syslog
我在日志中得到了什么:
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)
My check.sh
.
我的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
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.
我的start.sh
。它用于启动 Unturned 游戏服务器。我不认为这个脚本很重要,但我想我应该给你看。
#!/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='3[0;31m'
GREEN='3[0;32m'
YELLLOW='3[0;33m'
NC='3[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
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.
问题是,如果我./check.sh
从/home/steam
它执行它工作正常。坏消息是,@reboot
当我重新启动 VPS 时,它对我不起作用。
screen -list
doesn't throw anything if I reboot.
screen -list
如果我重新启动不会抛出任何东西。
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.
我已经尝试了多种方法但没有奏效,我更改的最后一件事是-d
在screen
命令中添加参数,这样服务器就不需要terminal
写下启动了。
I'm not sure how much can be done here to make @reboot
work as expected.
我不确定这里可以做多少工作才能@reboot
按预期工作。
How can I make my scripts run on boot? Are there any other alternatives to CRON
's @reboot
?
如何让我的脚本在启动时运行?是否有任何其他替代CRON
的@reboot
?
Thanks in advance.
提前致谢。
采纳答案by Jonas Sch?fer
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.
查看 systemd.service 联机帮助页。它描述了如何配置 systemd 来管理服务。我相信您会在/usr/lib/systemd/system
或类似的路径中找到适合您系统的示例。
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
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.
把它放在一个文件中/etc/systemd/system/unturned.service
。然后运行systemctl daemon-reload
(一次,每当您更改unturned.service
以告诉 systemd 重新读取配置)并systemctl start unturned.service
启动游戏服务器。
If that works as expected, you can use systemctl enable unturned.service
to make sure it starts at boot.
如果它按预期工作,您可以使用systemctl enable unturned.service
它来确保它在启动时启动。
A few notes on the options used:
关于所用选项的一些说明:
- If start.sh is not supposed to run as user/group
steam
, edit appropriately. WantedBy
in theInstall
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.
- 如果 start.sh 不应该作为 user/group 运行
steam
,请适当编辑。 WantedBy
在该Install
部分中告诉 systemd 当您使用 systemctl enable 启用服务时,哪个“目标”(参见 man systemd.target)会拉入服务。Restart
定义了 systemd 在什么情况下会自动重启服务。还有更多与重启相关的选项,您可能想也可能不想更改;请参阅 systemd.service 手册页。
回答by Patrickz
Try man 5 crontab
. If your crontab supported, you should see @reboot, @yearly, @monthly,.,,,
试试man 5 crontab
。如果您的 crontab 支持,您应该看到@reboot、@yearly、@monthly、.、、、
then try add some sleep for moment may can help.
然后尝试添加一些睡眠片刻可能会有所帮助。
@reboot sleep 60;/root/s3-mount.sh