Linux的shell文件如何来执行命令添加计划任务?

发布网友 发布时间:2022-02-26 09:36

我来回答

6个回答

懂视网 时间:2022-02-26 13:57

产品型号:thinkpad E15

系统版本:centos7

软件版本:crontab 1.5

在linux系统中,执行周期的任务,可以使用crontab。crontab会把你指定的工作或任务,按照你设定的周期一直循环执行下去启动crond服务[root@xuegod63 ~]# systemctl start crond 

crontab具体计划任务书写格式:

*  *  *  *  *  command需要执行的命令

分 时 日 月 周 命令

第1列表示分钟1~59 每分钟用*或者 */1表示

第2列表示小时1~23(0表示0点)

第3列表示日期1~31

第4列表示月份1~12

第5列表示星期0~6(星期日用0或7表示)

第6列要运行的命令

 

一行对应一个任务,特殊符号的含义:    

*        代表取值范围内的数字      (每)

/        指定时间的间隔频率 */10   0-23/2

-        代表从某个数字到某个数字       8-17 

,      分开几个离散的数字 6,10-13,20

 

创建计划任务

例1:每天凌晨2点1分开始备份数据

[root@xuegod63 spool]# crontab -e           #添加计划任务

1 2 * * *  tar zcvf /opt/grub2.tar.gz  /boot/grub2

[root@xuegod63 ~]# crontab  -l                 #查看

例2:以非root用户添加计划任务。  这里使用bin用户来添加

[root@xuegod63 ~]# crontab -u bin -e

*/10  * * * * echo "aaaaaaa"  >> /tmp/bin.txt

系统级别的计划任务

[root@xuegod63 etc]# vim /etc/crontab

1 2 * * *  root echo "aaaaaaa"  >> /tmp/1.txt

热心网友 时间:2022-02-26 11:05

crontab 命令

crontab -e  ——>编辑当前用户的crontab任务;

crontab -l   ——>列出当前用户的crontab任务;

crontab -r   ——>删除当前用户的crontab任务;


crontab书写格式


PS:

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作;

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”;

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”;

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次;

热心网友 时间:2022-02-26 12:23

#同步express的备份
10 0 * * * /opt/dts_rsync.sh &
#导入备份文件
01 7 * * * /opt/dts_input.sh &
那么需要的做的就是提取下载完成时间,然后将导入备份文件时间中的小时部分增加1小时。
由于shell无法直接编辑任务计划。
可以将任务计划内容写入到一个txt文件,然后使用命令将txt的内容覆盖至任务计划
命令如下:
/usr/bin/crontab /opt/cron.txt
完整的shell脚本cron如下:
#!/bin/bash
echo "#######################" >> /opt/cron.log
#记录执行时间
statime=`date +%Y-%m-%d" "%H:%M:%S`
echo "$statime" >> /opt/cron.log
#任务计划开始时间,提取小时
a=`cat /opt/cron.txt | grep dts_input.sh | awk '{print $2}'`
#传输完成时间,提取小时
b=`tail -1 /tmp/dts_message.log | awk '{print $2}' | cut -d ":" -f 1`
echo "任务计划开始时间$a" >> /opt/cron.log
echo "传输完成时间$b" >> /opt/cron.log
#判断传输完成时间是否大于等于任务计划时间
if [ $b -ge $a ];then
#增加1个小时
c=`/usr/bin/expr $a + 1`
#修改任务计划文件
/bin/sed -i "s/$a/$c/g" /opt/cron.txt
#覆盖任务计划
/usr/bin/crontab /opt/cron.txt
echo "任务计划开始时间状态ERROR,变更小时为$c" >> /opt/cron.log
else
echo "任务计划开始时间状态OK,小时是$a" >> /opt/cron.log
fi
cron.txt内容如下:
#同步express的备份
10 0 * * * /opt/dts_rsync.sh &
#导入备份文件
01 7 * * * /opt/dts_input.sh &
#检查下载时间
01 */1 * * * /opt/cron.sh
#每周日清空日志记录
01 23 * * 0 /opt/clean_log.sh

热心网友 时间:2022-02-26 13:58

1 导出旧的crontab -l > oldcron
2 echo "* * * * * mynewcron" >> oldcron
3 导回去 crontab < oldcron
==
重点是你要理解crontab是怎么工作的:
man crontab
CRONTAB(1) Cronie UsersâS™ Manual CRONTAB(1)

NAME
crontab - maintain crontab files for indivial users

SYNOPSIS
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]

DESCRIPTION
Crontab is the program used to install, remove or list the tables used
to drive the cron(8) daemon. Each user can have their own crontab, and
though these are files in /var/spool/ , they are not intended to be
edited directly. For SELinux in mls mode can be even more crontabs -
for each range. For more see selinux(8).

The cron jobs could be allow or disallow for different users. For clas-
sical crontab there exists cron.allow and cron.deny files. If
cron.allow file exists, then you must be listed therein in order to be
allowed to use this command. If the cron.allow file does not exist but
the cron.deny file does exist, then you must not be listed in the
cron.deny file in order to use this command. If neither of these files
exists, only the super user will be allowed to use this command.

热心网友 时间:2022-02-26 15:49

crontab -e      #编辑计划任务
* * * * *     /root/1.sh       # *从左到右分别代表分时天月周
20 13 * * *   /root/1.sh       # 每天13点20执行 1.sh

热心网友 时间:2022-02-26 17:57

crontab -e
0 0 1 * * /root/example.sh

/etc/init.d/crond restart

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com