gpt4 book ai didi

ubuntu - 如何使用 crontab 每 4 小时重新启动一个进程?

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:03 25 4
gpt4 key购买 nike

有人可以告诉我如何使用 crontab 每 4 小时重新启动一个进程吗?我有一个正在运行的 Starbound 服务器(这是一个类似于最近推出的 Terarria 的游戏)并且它占用了大量资源,所以我想终止该进程然后每 6 小时重新启动它。

我认为我需要在 crontab 中做的是:

kill -9 | grep starbound_servercd/home/steam/starbound/linux64 && screen -S starbound -d -m ./launch_starbound_server.sh

但我对此不确定,也不了解时间。

我希望有人能帮助我:)

最佳答案

crontab 是这样工作的。

# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

因此,如果您想每隔 4 小时每分钟运行一次脚本,就必须将此行添加到 crontab 文件中。

* */4 * * * user-name command to be executed

要每 4 小时(零分钟)运行一次脚本,您必须将此行添加到 crontab 文件。

0 */4 * * * user-name command to be executed

编辑(回复评论):

是的,我相信这是正确的,但作为我自己,我通常为此做单独的文件,例如,script.sh 以保持干净。

例如内容:

#!/bin/sh

# Kill 1
screen -X -S | grep starbound kill

# Kill 2
kill -9 | grep starbound_server

# Change directory
cd /home/steam/starbound/linux64

# Start the server again
screen -S starbound -d -m ./launch_starbound_server.sh

您可以将它保存到您喜欢的位置并使用:

chmod +x yourcript.sh

使其可执行,然后将其添加到 crontab。

关于ubuntu - 如何使用 crontab 每 4 小时重新启动一个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com