centos7中使用bash脚本动态添加crontab定时任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26870933/article/details/83746052

1.首先将在已创建的定时任务放在某个txt文件中

例如:/file/test/tab.txt

20 * * * * /tmp/shdir/test.sh  >> /file/test/tab.txt

2.创建一个执行脚本

例如:new_test.sh

写入以下内容

#!/bin/bash
#删除旧任务
sed -i '/test.sh/d' /var/spool/cron/root 
#添加新任务
cd /file/test/tab.txt
cat tab.txt | while read line
 do
   echo "${line}" >> /var/spool/cron/root
 done

3.调用上面的脚本程序即可

bash new_test.sh
 

猜你喜欢

转载自blog.csdn.net/qq_26870933/article/details/83746052