Linux定时任务

一、at定时任务工具和邮件mail服务

  • at定时任务工具,依赖于atd服务,适用于执行一次就结束的调度任务

1、at定时任务

at  [选项]  [时间] 

选项:

  -l  #列出等待中的定时任务

  -f  #通过文件读取任务,不用交互式输入

  -d #删除任务

Ctrl + D 提交并结束 at 命令的输入,连续按两次

时间的语法
HH:MM
YYYY-mm-dd
noon 正午中午12点
midnight 午夜晚12点
teatime 下午茶时间,下午四点
tomorrow 明天
now+1min #一分钟之后
now+1minutes/hours/days/weeks

#案例

[root@junwu_server ~]# at now+1min
at> echo "Hello world!" > hello.py
at> <EOT>
job 6 at Sun Mar 12 10:54:00 2023
You have new mail in /var/spool/mail/root

[root@junwu_server ~]# at -f hello.py now+1min
job 7 at Sun Mar 12 10:58:00 2023

[root@junwu_server ~]# atq    #查看at未执行的任务
11 Sun Mar 12 11:08:00 2023 a root  
[root@junwu_server ~]# at -d 11  #删除at定时任务

2、mail邮件服务

首先要检查centos6、7中的postfix用于发送邮件的服务是否开启

[root@junwu_server ~]# ss -tnl |grep 25
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 [::1]:25 [::]:*
[root@junwu_server ~]# netstat -tnl |grep 25
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN

发现未启动25端口,则需要开启postfix服务

更改postfix配置文件:vim /etc/postfix/main.cf

inet_interfaces = localhost

inet_protocols = all

重启postfixfuw

[root@junwu_server ~]# systemctl restart postfix

#mail命令(注意:Centos7通过命令mailx发送邮件,通过mail命令是收邮件)

[root@junwu_server ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 14 messages 8 unread
1 root Wed Oct 26 11:45 24/804 "Output from your job 2"
>U 2 root Wed Oct 26 12:08 19/705
U 3 Mail Delivery System Wed Oct 26 12:08 81/2741 "Undelivered Mail Returned to Sender"
U 4 root Wed Oct 26 12:16 19/703
U 5 Mail Delivery System Wed Oct 26 12:16 81/2739 "Undelivered Mail Returned to Sender"
U 6 Mail Delivery System Tue Nov 1 08:28 76/2690 "Undelivered Mail Returned to Sender"
7 (Cron Daemon) Mon Nov 7 13:00 30/1132 "Cron <root@localhost> /usr/bin/tar -zcf /opt/"
8 root Sun Mar 12 10:46 15/594 "Output from your job 3"
9 root Sun Mar 12 10:46 15/548 "Output from your job 4"
U 10 root Sun Mar 12 10:54 15/577 "Output from your job 5"
11 root Sun Mar 12 10:58 15/573 "Output from your job 7"
12 root Sun Mar 12 11:02 15/573 "Output from your job 8"
U 13 root Sun Mar 12 11:04 15/572 "Output from your job 9"
U 14 root Sun Mar 12 11:06 15/578 "Output from your job 10"

按下q或者ctrl+d退出

#交互式写邮件

[root@junwu_server ~]# mailx -s "hello world" junwu    #指定发送给junwu用户,-s添加邮件标题
加油学linux!!你行的  #邮件内容

。
.      #输入点.,退出邮件
EOT    #end out,结束符号

#非交互式写邮件

[root@junwu_server ~]# mail -s "hello today" junwu < mail_test.txt
[root@junwu_server ~]# su - junwu

[junwu@junwu_server ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/junwu": 2 messages 2 new
>N 1 root Sun Mar 12 11:23 20/661 "hello world"
N 2 root Sun Mar 12 11:28 18/654 "hello today"

 二、crond定时任务

crond进程提交任务的方式与at不同,crond需要读取配置文件,且有固定的文件格式,通过crontab命令管理文件

cron任务分为两类

  • 系统定时任务

crond服务除了会在工作时查看/var/spool/cron文件夹下的定时任务文件以外,还会查看/etc/cron.d目录以及/etc/anacrontab下面的文件内容,里面存放每天、每周、每月需要执行的系统任务

[junwu@junwu_server ~]$ ls -l /etc/ |grep cron*
-rw-------. 1 root root 541 Aug 8 2019 anacrontab
drwxr-xr-x. 2 root root 39 Nov 11 06:18 cron.d
drwxr-xr-x. 2 root root 42 Sep 18 18:38 cron.daily
-rw-------. 1 root root 0 Aug 8 2019 cron.deny
drwxr-xr-x. 2 root root 22 Jun 9 2014 cron.hourly
drwxr-xr-x. 2 root root 6 Jun 9 2014 cron.monthly
-rw-r--r--. 1 root root 451 Jun 9 2014 crontab
drwxr-xr-x. 2 root root 6 Jun 9 2014 cron.weekly

系统定时任务配置文件/etc/crontab

[junwu@junwu_server ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
  • 用户定时任务计划

当系统管理员(root)或是普通用户(chaoge)创建了需要定期执行的任务,可以使用crontab命令配置,

crond服务在启动时,会每分钟查看/var/spool/cron路径下以系统用户名命名的定时任务文件,以确定是否有需要执行的任务。

#root用户有一个定时任务

[root@junwu_server junwu]# ls -l /var/spool/cron/
total 4
-rw-------. 1 root root 338 Nov 11 08:33 root

#查看定时任务文件内容

[root@junwu_server junwu]# cat /var/spool/cron/root
###* * * * * /usr/bin/echo "Linux好简单啊" >> /tmp/testcron.txt
##* * * * * /bin/sh /tmp/bak.sh > /tmp/html.txt 2>&1
##0 * * * * /usr/bin/tar -zcf /opt/data/ /var/www/html
##*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null
##31 10 * * * /tmp/bak.sh
##0 4 * * 6 /tmp/bak.sh
3 * * * * /usr/bin/echo "Hello" >> /tmp/chao.txt

#等同于crontab -l

[root@junwu_server junwu]# crontab -l
###* * * * * /usr/bin/echo "Linux好简单啊" >> /tmp/testcron.txt
##* * * * * /bin/sh /tmp/bak.sh > /tmp/html.txt 2>&1
##0 * * * * /usr/bin/tar -zcf /opt/data/ /var/www/html
##*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null
##31 10 * * * /tmp/bak.sh
##0 4 * * 6 /tmp/bak.sh
3 * * * * /usr/bin/echo "Hello" >> /tmp/chao.txt

crontab命令

crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似

 
参数解释使用示例
-l list查看定时任务 crontab -l
-e edit编辑定时任务,建议手动编辑 crontab -e
-i 删除定时任务,提示用户确认删除,避免出错 crontab -i
-r 删除定时任务,移除/var/spool/cron/username文件,全没了 crontab -r
-u user 指定用户执行任务,root可以管理普通用户计划任务 crontab -u chaoge -l
crontab命令就是在修改/var/spool/cron中的定时任务文件

用户查看定时任务

crontab -l #列出用户设置的定时任务,等于cat var/spool/cron/root
crontab -e #编辑用户的定时任务,等于如上命令编辑的是 vi /var/spool/cron/root文件

定时任务相关的文件

/var/spool/cron 定时任务的配置文件所在目录
/var/log/cron 定时任务日志文件
/etc/cron.deny 定时任务黑名单

生产环境用户配置定时任务流程

第一步:新手要注意确保定时任务的正确执行,切莫编写了定时任务就不管不问了

第二步:编辑定时任务文件,写入需要定时执行的任务

crontab -e

写入

* * * * * /usr/bin/echo "定时任务学完了,加油!" >> /cron_test/定时任务学完了.txt

第三步:检查定时任务

[root@junwu_server junwu]# crontab -l
* * * * * /usr/bin/echo "定时任务学完了,加油!" >> /cron_test/定时任务学完了.txt

第四步:可以检测文件内容

[root@junwu_server ~]# tail -f /cron_test/定时任务学完了.txt
定时任务学完了,加油!
定时任务学完了,加油!
定时任务学完了,加油!
posted @ 2023-03-13 00:58  Junwu’sblog  阅读(114)  评论(0编辑  收藏  举报