Linux中的定时自动执行功能(at,crontab)
概念
在Linux系统中,提供了两种提前对工作进行安排的方式
-
at 只执行一次
-
crontab 周期性重复执行
通过对这两个工具的应用可以让我们在操作系统层面实现定时自动运行功能
实验环境
操作系统:CentOS Linux release 7.3.1611 (Core)
IP: 192.168.230.134
at介绍
at命令会,并且会执行一次
此功能会使用到atd工具,需要保证其为启动状态
[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-09-07 18:18:18 CST; 4h 2min ago
Main PID: 921 (atd)
CGroup: /system.slice/atd.service
└─921 /usr/sbin/atd -f
Sep 07 18:18:18 localhost.localdomain systemd[1]: Started Job spooling tools.
Sep 07 18:18:18 localhost.localdomain systemd[1]: Starting Job spooling tools...
at使用
常用命令如下
[root@localhost ~]# at -help
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
at [-V] [-q x] [-f file] [-mMlbv] -t time
at -c job ...
atq [-V] [-q x]
at [ -rd ] job ...
atrm [-V] job ...
batch
时间格式:
格式 | 示例 | 意义 |
---|---|---|
HH:MM | 01:01 | 下一次1点1分执行 |
HH:MM YYYY-MM-DD | 01:00 2017-09-08 | 指定具体年月时间执行 |
HH:MM[am/pm]+number[minutes/hours/days/weeks] | now+3 days | 某时间点再加些时间后 |
at实例
创建测试用sh,并修改权限
[root@localhost ~]# vim test.sh
echo '豆姐威武'
[root@localhost ~]# chmod 755 test.sh
[root@localhost ~]# ./test.sh
豆姐威武
查看当前时间
[root@localhost ~]# date
Thu Sep 7 23:19:51 CST 2017
使用at命令,设定23:21执行test.sh命令,注意以Ctrl+d结束
[root@localhost ~]# at 23:21
at> /root/test.sh
at> <EOT>
job 5 at Thu Sep 7 23:21:00 2017
坐等一分钟后,收到系统给的信息,查看时间以及到了设定时间
You have new mail in /var/spool/mail/root
[root@localhost ~]# date
Thu Sep 7 23:21:04 CST 2017
查看系统收到的信息
[root@localhost ~]# cat /var/spool/mail/root
From root@localhost.localdomain Thu Sep 7 23:21:00 2017
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
id A139B243D2; Thu, 7 Sep 2017 23:21:00 +0800 (CST)
Subject: Output from your job 5
To: root@localhost.localdomain
Message-Id: <20170907152100.A139B243D2@localhost.localdomain>
Date: Thu, 7 Sep 2017 23:21:00 +0800 (CST)
From: root@localhost.localdomain (root)
豆姐威武
抄一个比较实用的脚本:
定时关机
[root@localhost ~]# at 18:00 2017-09-10
at> /bin/sync
at> /bin/sync
at> /sbin/shutdown -h now
at> <EOT>
job 6 at Sun Sep 10 18:00:00 2017
将在2017/09/07 18:00自动关机
at命令的取消
查看当前at命令
使用atq命令
[root@localhost ~]# atq
6 Sun Sep 10 18:00:00 2017 a root
取消命令
使用atrm命令
[root@localhost ~]# atrm 6
crontab介绍
crontab命令会按照配置周期性的执行
他的记录会保存在/var/log/cron中
此功能需要用到crond服务,需要保证其状态正常
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-09-07 18:18:18 CST; 5h 51min ago
Main PID: 920 (crond)
CGroup: /system.slice/crond.service
└─920 /usr/sbin/crond -n
Sep 07 18:18:18 localhost.localdomain systemd[1]: Started Command Scheduler.
Sep 07 18:18:18 localhost.localdomain systemd[1]: Starting Command Scheduler...
Sep 07 18:18:18 localhost.localdomain crond[920]: (CRON) INFO (RANDOM_DELAY will be ...)
Sep 07 18:18:19 localhost.localdomain crond[920]: (CRON) INFO (running with inotify ...)
Hint: Some lines were ellipsized, use -l to show in full.
crontab使用
常用命令如下:
[root@localhost ~]# crontab -help
crontab: invalid option -- 'h'
crontab: usage error: unrecognized option
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging
时间格式:
* * * * *
- - - - -
| | | | |
| | | | |
| | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)
crontab实例
还是使用之前创建的test.sh做实验
使用crontab命令新建计划
先查看当前时间
[root@localhost ~]# date
Thu Sep 7 23:52:12 CST 2017
创建计划,设定每天23:55执行 /root/test.sh脚本
[root@localhost ~]# crontab -e
55 23 * * * /root/test.sh
no crontab for root - using an empty one
crontab: installing new crontab
查看当前计划:
[root@localhost ~]# crontab -l
55 23 * * * /root/test.sh
坐等两分钟后:
[root@localhost ~]# date
Thu Sep 7 23:55:07 CST 2017
You have new mail in /var/spool/mail/root
查看记录:
[root@localhost ~]# cat /var/spool/mail/root
From root@localhost.localdomain Thu Sep 7 23:55:01 2017
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
id 45D1028EB7; Thu, 7 Sep 2017 23:55:01 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /root/test.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=52>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20170907155501.45D1028EB7@localhost.localdomain>
Date: Thu, 7 Sep 2017 23:55:01 +0800 (CST)
豆姐威武
crontab计划的删除
如果需要删除一两个计划,直接使用crontab -e进入编辑页面删除
如果要清空计划,-r即可
[root@localhost ~]# crontab -l
55 23 * * * /root/test.sh
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
crontab补充
也可以直接编辑/etc/crontab文件来实现计划脚本
[root@localhost ~]# vim /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
但是使用这种方式有时候不会立刻生效,需要重启crond服务