linux自动化管理crontab任务
2024-01-22 本文已影响0人
夜空最亮的9星
crontab
作用:添加,查询,删除系统计划任务的命令。
-e: 编辑crontab定时任务
-l: 查询crontab任务
-r: 删除当前用户所有的crontab任务
#!bin/bash
current_time=$(date "+%Y%m%d_%H%M%S")
# 备份当前定时任务
crontab -l /tmp/crontab_${current_time}
# 删除当前所有定时任务
crontab -r
cat > /tmp/crontab << EOL
# 注释内容
# 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
# 定时任务
* * * * * /bin/bash /home/user01/task001.sh
EOL
# 从文件导入当前定时任务
crontab /tmp/crontab
# 重启定时任务
sudo service cron restart
# 查看当前任务列表
crontab -l
# 查看当前任务状态
sudo service cron status