[英]run the script every 30 minutes bash
I want to run the script every 30 minutes with cron but I have problem with my code. 我想使用cron每30分钟运行一次脚本,但是我的代码有问题。 In every 30 min I have to kill old script and run it again.
每隔30分钟,我必须杀死旧脚本并再次运行它。 I have somethink like this, but it is not working:
我有这样的想法,但它不起作用:
cd /var/www/scripts
pkill -f bot
now="$(date +%Y%m%d%H%M%S)"
screen -S bot
node mybot.js >> logi/logi_$now.txt
use crontab
: 使用
crontab
:
crontab -l
*/30 * * * * /path/to/your/command
save and run 保存并运行
You may not use screen
for running things in background in a script. 您可能无法使用
screen
在脚本中在后台运行事物。 Use ampersand ( &
) to background a process and nohup
so it won't be killed when cron script exits. 使用&号(
&
)来使进程和nohup
成为后台,这样在cron脚本退出时它不会被杀死。 Also remember a subprocess PID in a file. 还请记住文件中的子流程PID。
Something like this: 像这样:
kill -- "$(cat mybot.pid)"
now="$(date +%Y%m%d%H%M%S)"
nohup node mybot.js >> "logi/logi_$now.txt" &
echo $! > mybot.pid
The line 线
node mybot.js >> logi/logi_$now.txt
is never reached, as screen -S <session name>
will start a screen session and therefore a new shell and connect to it . 永远不会到达,因为
screen -S <session name>
将启动一个屏幕会话,因此将建立一个新的shell 并连接到它 。 The rest of the script would only execute once that 'inner' session terminates. 脚本的其余部分仅在该“内部”会话终止后才执行。
screen
is more for interactive use. screen
更多用于交互式使用。 Calling it in a script like this is rather strange. 在这样的脚本中调用它是很奇怪的。 I guess you want to have
node mybot.js >> logi/logi_$now.txt
running in the background, so that your script can terminate while node
keeps running. 我想您想让
node mybot.js >> logi/logi_$now.txt
在后台运行,以便您的脚本可以在node
继续运行时终止。 See Redirecting stdout & stderr from background process and Node.js as a background service for options how to do that. 有关如何执行此操作的选项,请参阅从后台进程重定向stdout和stderr以及作为后台服务的Node.js。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.