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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月27日 03时41分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【组队学习】【26期】Linux教程
2019-04-27
LeetCode-LCP 18. 早餐组合(Goland实现)
2019-04-27
C++从入门到进阶近100本书推荐电子书pdf
2019-04-28
蓝桥杯 - [2014年第五届真题]分糖果(模拟)
2019-04-28
蓝桥杯 - [2013年第四届真题]大臣的旅费(DFS)
2019-04-28
蓝桥杯 - [2013年第四届真题]带分数(全排列)
2019-04-28
蓝桥杯 - [2013年第四届真题]幸运数(模拟)
2019-04-28
蓝桥杯 - [2013年第四届真题]横向打印二叉树(排序二叉树)
2019-04-28
蓝桥杯 - [历届试题]网络寻路(枚举)
2019-04-28
牛客网 - [中南林业科技大学第十一届程序设计大赛]兑换零钱(背包问题)
2019-04-28
HDU - Robberies(01背包)
2019-04-28
HDU - 最大报销额(01背包|贪心)
2019-04-28
HDU - Coins(完全背包)
2019-04-28
JXFCZX — 砝码称重1(DFS+背包)
2019-04-28
JXFCZX — 质数和分解(完全背包)
2019-04-28
JXFCZX — 花店橱窗(动态规划)
2019-04-28
JXFCZX — 逃亡的准备(多重背包)
2019-04-28
JXFCZX — 庆功会(多重背包)
2019-04-28
AcWing - 扩展欧几里得算法(扩欧)
2019-04-28
AcWing - 高斯消元解线性方程组(高斯消元)
2019-04-28