您的位置:首页 > 运维架构 > Linux

Linux-系统的延时和定时

2019-01-23 22:49 113 查看

 

 

1、系统的延时任务

[code]at 09:46                      #设定任务的执行时间
at> touch /mnt/file{1..9}     #任务的动作
at> <EOT>                     #ctrl+d 发起任务

at -l      #查看任务列表
注:多个任务可以同时被执行

at -c 任务号       #查看任务的内容
at -r 任务号       #取消任务的执行  取消一定在任务执行的时间之前

注:当任务有输出的时候,输出会以邮件的形式发送给at任务的发起者
at now+1min
at> echo hello
at> <EOT>
job 5 at Sun Jan 20 10:08:00 2019   #邮件

例:

[code]mail -u root           #查看超级用户邮件
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/root": 2 messages 2 new
>N  1 root                  Sun Jan 20 09:46  22/1028  "Output from y"
N  2 root                  Sun Jan 20 10:08  14/494   "Output from y"
& 2
Message  2:             #查看第二封邮件
From root@desktop0.example.com  Sun Jan 20 10:08:00 2019
Return-Path: <root@desktop0.example.com>
X-Original-To: root
Delivered-To: root@desktop0.example.com
Subject: Output from your job        5
To: root@desktop0.example.com
Date: Sun, 20 Jan 2019 10:08:00 +0800 (CST)
From: root@desktop0.example.com (root)
Status: R

hello

&

q:退出

>/var/spool/mail/root     #清空邮件

系统中的其他用户,也可以通过at命令发起任务的执行

例:

[code][student@desktop0 ~]$ at + 10:13
syntax error. Last token seen: +
Garbled time
[student@desktop0 ~]$ at 10:13
at> touch /home/student/ss
at> <EOT>
job 6 at Sun Jan 20 10:13:00 2019
[student@desktop0 ~]$ at -l
6	Sun Jan 20 10:13:00 2019 a student
[student@desktop0 ~]$ at 10:15
at> touch /root/file     #在任务的发起中一定要注意:不同用户的权限问题
at> <EOT>
job 7 at Sun Jan 20 10:15:00 2019
[student@desktop0 ~]$ at -l
7	Sun Jan 20 10:15:00 2019 a student
[student@desktop0 ~]$ ll
total 0
-rw-rw-r--. 1 student student 0 Jan 20 10:13 ss

2、at任务的黑白名单

[code]黑名单:
/etc/at.deny     #系统中默认存在,在此文件中出现的用户不能执行at命令
[root@desktop0 mnt] ll /etc/at.deny
-rw-r--r--. 1 root root 1 Jan 29  2014 /etc/at.deny
[root@desktop0 mnt]  vim /etc/at.deny
[root@desktop0 mnt]  cat /etc/at.deny
student
[root@desktop0 mnt]  su - stduent
su: user stduent does not exist
[root@desktop0 mnt]  su - student
Last login: Sun Jan 20 10:11:57 CST 2019 on pts/0
[student@desktop0 ~]$ at 10:16
You do not have permission to use at.

白名单:
/etc/at.allow    #系统中默认不存在,当文件出现,普通用户不能执行at,只用在名单中出现的用户可以,/etc/at.deny这个文件失效

3、系统的定时任务

例:

[code]分钟   小时   天   月   周
*     *     *    *   *        #每分钟
*/2    *     *    *   *        #没两分钟
*/2   09-17  *    *   *        #早9-晚5每两分钟
*/2   09-17  *   3,5  5        #3月和5月每周五
*/2   09-17  *    *   5        #每周五早9晚5
[code]命令方式设置定时任务:
crontab -e              #root用户的定时任务
crontab -l -u root      #列出crontab任务
crontab -e -u student   #root让普通用户执行定时任务

文件方式设置定时任务:
cd /etc/cron.d
vim file(文件名任意)
* * * * * username    action
#用户名   #执行什么命令

例:
分钟   小时   天   月   周
*     *     *    *   *   root touch  /mnt/file  #每分钟root用户在/mnt下建立一个file文件
*/2    *     *    *   *        #没两分钟
*/2   09-17  *    *   *        #早9-晚5每两分钟
*/2   09-17  *   3,5  5        #3月和5月每周五
*/2   09-17  *    *   5        #每周五早9晚5

非交互式:
echo "* * * * * root rm -rf /mnt/*" >/etc/cron.d/westos
#把* * * * * root rm -rf /mnt/*命令打印到/etc/cron.d/westos文件里面
[code]注:在文件的方式定义crontab任务的时候,使用crontab -l是看不到内容的

以下目录只对超级用户可写:
[root@desktop0 cron.d] ll -d /etc/cron.d
drwxr-xr-x. 2 root root 85 Jan 20 11:12 /etc/cron.d

crontab的黑名单:
/etc/cron.deny  #系统中默认存在,在此文件中出现的用户不能执行crontab

crontab的白名单:
/etc/cron.allow  #系统中默认不存在,当文件创建出来的时候,普通用户不能执行
#只有在名单中的用户可以使用

4、系统临时文件的管理方式

[code]cd /usr/lib/tmpfile.d/
vim westos.conf

d      /mnt/westos       777     root      root      5s
目录    要建立的文件目录     权限     拥有者     所属组

systems-tmpfiles --create /usr/lib/tmpfiles.d/*   #读取里面的所有文件并按照规则去建立目录
cd /mnt/westos
touch file{1..9}
等待五秒
systemd-tmpfiles --clean /usr/lib/temfiles.d/*    #清理目录里面的文件(只有5秒钟后才能清理)

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: