阅读量:61
nohup 命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
以下是如何将 nohup 命令与 cron 作业结合使用的步骤:
-
打开终端。
-
输入
crontab -e命令来编辑当前用户的cron作业表。这将打开一个文本编辑器,如vi或nano。 -
在编辑器中,添加一行新的
cron作业。cron作业的格式如下:
* * * * * command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday to Saturday; 7 is also Sunday)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
例如,如果你想每天凌晨 1 点运行名为 my_script.sh 的脚本,你可以添加以下行:
0 1 * * * nohup /path/to/your/script/my_script.sh > /path/to/output.log 2>&1 &
这里,nohup 命令用于在后台运行 my_script.sh 脚本。> 符号将脚本的输出重定向到 output.log 文件,2>&1 将错误输出重定向到标准输出,& 符号使脚本在后台运行。
- 保存并关闭编辑器。
cron作业现已添加,将在指定的时间运行。
注意:确保 my_script.sh 具有可执行权限。你可以使用 chmod +x /path/to/your/script/my_script.sh 命令为脚本添加可执行权限。