Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ubuntu 如何使用crontab每4小时重新启动一个进程?_Ubuntu_Crontab_Restart - Fatal编程技术网

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

Ubuntu 如何使用crontab每4小时重新启动一个进程?,ubuntu,crontab,restart,Ubuntu,Crontab,Restart,有人能告诉我如何使用crontab每4小时重新启动一个进程吗?我有一个Starbound服务器正在运行(这是一个类似Terarria的游戏,最近才推出),它占用了很多资源,所以我想停止这个进程,然后每6小时启动一次 我认为我需要在crontab中做的是: kill-9 | grep starbound_服务器 cd/home/steam/starbound/linux64&&screen-S starbound-d-m./launch\u starbound\u server.sh 但我对此不确

有人能告诉我如何使用crontab每4小时重新启动一个进程吗?我有一个Starbound服务器正在运行(这是一个类似Terarria的游戏,最近才推出),它占用了很多资源,所以我想停止这个进程,然后每6小时启动一次

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

kill-9 | grep starbound_服务器 cd/home/steam/starbound/linux64&&screen-S starbound-d-m./launch\u starbound\u 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
0 */4 * * * user-name command to be executed
要每4小时(零分钟)运行一次脚本,必须将这一行添加到crontab文件中

* */4 * * * user-name command to be executed
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。

前提是您已在
/etc/init.d

你把它命名为
starbound.sh

然后,在
/etc/crontab
中添加一行,如下所示:

0/4***根/etc/init.d/starbound.sh重新启动


(注意:这种情况下,starbound服务器是由
根用户启动的:如果不需要,请检查服务器本身是否在启动时放弃其特权)

可能重复:哇,谢谢,我真不敢相信你解释得有多好!但愿我能投你一票。如果你不介意我再问一个问题,这是正确的:*/4***我的用户名screen-X-S | grep starbound kill&kill-9 | grep starbound\u server&cd/home/steam/starbound/linux64&&screen-S starbound-d-m./launch\u starbound\u\u server.shHey真棒,你是我的救世主!但是screen-X-S | grep starbound kill似乎不起作用,我在谷歌上搜索过,但找不到任何东西,如何搜索/找到一个屏幕,然后杀死它。我想搜索它,因为有时它不想被杀死,我必须把pid.screen.当心,第一个*会在第四个小时内每分钟运行一次脚本。我强烈建议将其设置为0,使其每4小时仅运行一次。也就是说,0*/4***我做了一次编辑,所以现在它写的是正确的
0*/4***
而不是
***/4***
只是想知道-我们真的需要屏幕吗?我的意思是,cron会让后台进程保持活动状态,对吗?它应该是
0/4***root/etc/init.d/starbound.sh restart
,每小时执行一次。不是每4小时的每一分钟。我想这显然是OP想要的。