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

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

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

参阅:https://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 crond start
# 重启
service crond restart
# 停止
service crond status
# 检查状态
service crond stop
2.2 创建需要定时执行的bash脚本文件
# 以下命令均在root用户下运行
# 切换crontab编辑器命令
select-editor
# 创建名为 test 的文件,在该文件中编写定时任务的内容
vim test
# 创建 output.txt 该文件用于下面的步骤中接收定时任务输出的结果
touch output.txt
# 在创建的test文件中写入以下内容
# 每分钟运行一次 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
posted @ 2021-01-23 22:51  Incercadi  阅读(271)  评论(0编辑  收藏  举报