Linux Ubuntu系统版本通过Crontab设置定时任务的执行

Linux Ubuntu系统版本通过Crontab设置定时任务的执行

本文由本人收集网络信息总结而来

特别鸣谢://linux.zone/2258

1 crontab 简单介绍以及语法使用

参阅://www.runoob.com/linux/linux-comm-crontab.html

2 Ubuntu使用crontab

2.1 Ubuntu中crontab的简单命令
# 查看是否已安装crontab
crontab -l 
# Ubuntu通过apt命令安装crontab软件
apt-get-install cron
# 启动
service cron start
# 重启
service cron restart
# 停止
service cron stop
# 检查状态
service cron stop
## 本部分命令由本人通过别人的博客得来,验证可用
2.2 创建需要定时执行的bash脚本文件
# 创建名为 test 的文件,在该文件中编写定时任务的内容
vim test
# 创建 output.txt 该文件用于下面的步骤中接收定时任务输出的结果
touch output.txt
# 在创建的文件中写入以下内容
# 每分钟运行一次 date 命令同时往 /home/soft/output.txt 文件中写入输出内容
* * * * * date >> /home/soft/output.txt
2.3 执行定时任务文件
# 将写好的定时任务文件装载到cron中,该步骤很重要
crontab test
# 重启crontab
service cron restart
# 查看当前用户下的定时任务
crontab -l
root@incercadi:/home/soft/# crontab -l
* * * * * date >> /home/soft/output.txt
# 几分钟之后查看定时任务是否执行成功,如果里面写入了内容则说明定时任务运行成功
cat /home/soft/output.txt