ubuntu定时执行脚本-cron
发布日期:2021-11-15 14:57:48 浏览次数:31 分类:技术文章

本文共 2296 字,大约阅读时间需要 7 分钟。

1.编辑crontab文件

crontab {
-l | -r | -e }

参数说明:

-e : 执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL 环境变数来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
-r : 删除目前的时程表
-l : 列出目前的时程表

root@ecs-bd21:~# crontab -eno crontab for root - using an empty oneSelect an editor.  To change later, run 'select-editor'.  1. /bin/nano        <---- easiest  2. /usr/bin/vim.basic  3. /usr/bin/vim.tiny  4. /bin/edChoose 1-4 [1]:

第一次编辑时,会出现让我们选择的四种文本编辑器,输入数字回车,即可选择其中一种。我选择的是vim,所以我输入2并回车。出现下面信息

# # Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task# # To define the time you can provide concrete values for# minute (m), hour (h), day of month (dom), month (mon),# and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system# daemon's notion of time and timezones.# # Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).# # For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/# # For more information see the manual pages of crontab(5) and cron(8)# # m h  dom mon dow   command~~~"/tmp/crontab.VAJWvb/crontab" 22L, 888C                            1,1           All

2.输入指令

输入指令:

0 0 * * * bash /root/test.sh

这条指令的意思是:每天在00:00时都执行bash /root/test.sh,运行test.sh脚本文件。

指令格式如下:

f1 f2 f3 f4 f5 program
  • 其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
  • 当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
  • 当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
  • 当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
  • 当 f1 为 a, b, c,… 时表示第 a, b, c,… 分钟要执行,f2 为 a, b, c,… 时表示第 a, b, c…个小时要执行,其馀类推
*    *    *    *    *   program-    -    -    -    -      -|    |    |    |    |      ||    |    |    |    |      +----- 要执行的程序|    |    |    |    +------------ 星期中星期几 (0 - 6) (星期天 为0)|    |    |    +----------------- 月份 (1 - 12) |    |    +---------------------- 一个月中的第几天 (1 - 31)|    +--------------------------- 小时 (0 - 23)+-------------------------------- 分钟 (0 - 59)

3.重启cron

service cron restart

就这样,每天在00:00时都执行bash /root/test.sh,运行test.sh脚本文件完成!!!!

转载地址:https://blog.csdn.net/qq_39741836/article/details/114969056 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mongodb-config集合-system.sessions
下一篇:vue history模式 部署在nginx中,nginx的配置 (根目录)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月27日 03时41分33秒