ubuntu实现定时执行任务

linux环境下定时或者周期性的执行一些任务通常由cron这个守护进程来完成,这是一个系统自带的相对也比较方便的系统工具。

前言

crontab:是用来定期执行程序的命令
一个简单的例子,就明白如何创建并执行自己的命令。crontab可以执行各种定时任务-包括文件备份,脚本执行,文件写入等等。
简介

1:
linux环境下定时或者周期性的执行一些任务通常由cron这个守护进程来完成,这是一个系统自带的相对也比较方便的系统工具。
2:
cron进程能实现定时任务这些需求,cron搭配shell脚本,非常复杂的指令也没有问题。
3:
crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件。

crontab脚本编写

到定时时间后,需要做的一个任务,这里我分为了两种任务:因为都会用到yoyo.log文件,它用来存储我们输出的信息。
先创建一个yoyo.log文件,他的路径随意。

$ touch yoyo.log

注意:crontab的编写都是在/etc下更改的添加的,如果在其他目录更改添加,将会出现交叉混用,导致定时不成功!!
1:定时时间到了后,将实时的时间输出到一个日志文件yoyo.log上

:/etc$ crontab -e

在/etc路径下直接添加任务,接下来会是这样的:

# Edit this file to introduce tasks to be run by cron.
# 
# 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

直接上编写完成后的:

# Edit this file to introduce tasks to be run by cron.
# 
# 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
*/1 * * * * date >> ~/andriod/yoyo.log

我添加的这一行,意思是:每一分钟,将实时的时间输出到~/andriod/yoyo.log文本中。
接着按着contr+x退出,接着保存。
重新启动cron:(让新添加的命令生效)

sudo service cron restart

结果:需要自己cat一下

$ cat yoyo.log
2020年 11月 09日 星期一 18:01:01 CST
2020年 11月 09日 星期一 18:02:01 CST
2020年 11月 09日 星期一 18:03:01 CST
$

2:定时时间到后,执行一个脚本,该脚本的功能就是将当前的时间输出到上一个日志文件yoyo.log上
按照前面的做法,直接展示更改后的结果:

# Edit this file to introduce tasks to be run by cron.
# 
# 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
*/1 * * * * date >> ~/andriod/yoyo.log
*/1 * * * *  ~/andriod/shutdown.sh

这两行中的第一行是前面1添加的,第二行是现在添加的。
第二行:每一分钟,将执行 ~/andriod此路径下的shutdown.sh脚本:

扫描二维码关注公众号,回复: 13030441 查看本文章
#!/bin/sh
echo $(date) >> ~/andriod/yoyo.log
echo "00000" >> ~/andriod/yoyo.log

采用sh解析器。每次执行次脚本,将实时时间存储到yoyo.log上,随后再将00000存储到yoyo.log上。
结果:需要自己cat一下

$ cat yoyo.log 
2020年 11月 09日 星期一 18:03:01 CST
00000
2020年 11月 09日 星期一 18:04:01 CST
00000
$

基本知识

内容很简洁的讲完了,可能对于有些初学者,对于规则还不太明白,我再来普及下知识点:
第一次使用crontab 时,会出现以下提示:

root@ubuntu:# crontab -e
Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

这个提示是让用户选择编辑器,正常情况下我们选择第三个就可以。如果选错了可以执行select-editor命令在选择一次。
用crontab -e进入当前用户的工作表编辑
其他命令:

 crontab常用的几个命令格式
crontab -l //显示用户的crontab文件的内容
crontab -e //编辑用户的crontab文件的内容
crontab -r //删除用户的crontab文件
service cron start    //启动服务
service cron stop     //关闭服务
service cron restart  //重启服务
service cron reload   //重新载入配置
service cron status   //查看服务状态 
 
tail /var/log/syslog  //使用crontab进行设置定时任务,任务没有执行,查看系统日志排查
 
ps -ef | grep cron  //查看cron 进程是否存在

crontab定时任务语法如下:

minute   hour   day   month   week  user  command     #顺序:分 时 日 月 周 用户 命令
minute: 表示分钟,可以是从 0 到 59 之间的任何整数。
hour:表示小时,可以是从 0 到 23 之间的任何整数。
day:表示日期,可以是从 1 到 31 之间的任何整数。
month:表示月份,可以是从 1 到 12 之间的任何整数。
week:表示星期几,可以是从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日。
user:linux的用户身份,例如root,或者其他用户
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。
星号(*):代表所有可能的值,例如 month 字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,\
例如*/10,如果用在 minute 字段,表示每十分钟执行一次。

猜你喜欢

转载自blog.csdn.net/weixin_42271802/article/details/109582954