Linux——crontab,crond,计划任务全搞定!

目录

计划任务

计划任务:crontab

添加计划任务

列出计划任务

查看运行结果

小练习:


计划任务

计划任务: 按照计划去完成任务: 定时去完成任务

价值:  定时替人去执行程序,解放人力,更加保质保量的完成任务

备份的时候,一定要是用户数最少的时候

备份的时候会消耗cpu,内存,磁盘,网络带宽


计划任务:crontab

进程的名字crond  ,不需要我们去按照,安装linux系统的时候就已经安装了,而且程序开机自动启动。周期性任务:重复执行的任务。

[root@sc ~]# ps aux|grep  crond

root        949  0.0  0.4  36300  3584 ?        Ss   09:26   0:00 /usr/sbin/crond -n

root       1619  0.0  0.1  12320   988 pts/0    S+   10:10   0:00 grep --color=auto crond

[root@sc ~]#

/etc/crontab 是crond的配置文件

[root@sc ~]# 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

添加计划任务

[root@sc feng2021-7-3]# crontab -e 创建(编辑) 计划任务  

计划任务:

*/1 * * * * bash  /feng2021-7-3/create_sc.sh

30 12 3 7 * bash /feng2021-7-3/create_sc.sh

列出计划任务

[root@sc feng2021-7-3]# crontab -l  

*/1 * * * * bash  /feng2021-7-3/create_sc.sh

30 12 3 7 * bash /feng2021-7-3/create_sc.sh

查看运行结果

[root@sc log]# tail -f  cron

Jul  3 11:35:01 sc CROND[2268]: (root) CMD (bash  /feng2021-7-3/create_sc.sh)

注意这些文件夹里的脚本或者程序:因为crond会定时去执行这些文件夹里的命令

/etc/cron.hourly/

/etc/cron.daily/

小练习

1.写一个脚本create_sc.sh实现在/backup目录下新建一个空文件,空文件的名字里要有当时的日期,日期精确到秒,例如:/backup/sc_20210703101530.txt

2.每分钟执行一次上面的脚本create_sc.sh

[root@sc ~]# mkdir   /feng2021-7-3

[root@sc ~]# cd /feng2021-7-3/

[root@sc feng2021-7-3]# ls

[root@sc feng2021-7-3]# cat create_sc.sh

#!/bin/bash



#确保/backup目录的存在

mkdir  -p /backup



#新建空文件,文件名里使用当前的日期

touch  /backup/sc_$(date +%Y%m%d%H%M%S).txt



[root@sc feng2021-7-3]# ls

create_sc.sh
[root@sc feng2021-7-3]# */1 * * * * bash  /feng2021-7-3/create_sc.sh

写脚本:去完成某个任务

crond--》去执行我们的脚本

计划任务的最短时间间隔是1分钟

 练习

(1)30 */2  *  *  *  bash  /mydata/script/test.sh

30 1,3,5,7,9,11  *  *  *  bash  /mydata/script/test.sh

(2)50 23  * * *  bash  /mydata/script/test.sh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chaochao️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值