Centos 创建并启动定时任务 demo 示例

crontab的一些基本命令

systemctl start crond.service          启动服务
systemctl stop crond.service          停止服务
systemctl restart crond.service        重启服务
systemctl reload crond.service        重载配置文件
systemctl status crond.service        查看状态

crontab -l       显示crontab文件(显示已设置的定时任务)
crontab -e      编辑crontab文件(编辑定时任务)
crontab -r       删除crontab文件(删除定时任务)
crontab -ir      删除crontab文件提醒用户(删除定时任务)

示例

1、编写脚本文件hello.sh

$ cd /root
$ touch hello.sh
$ vi hello.sh

输入需要执行的内容

#!/bin/bash
DATE=$(date +%Y-%m-%d' '%H:%M:%S)
echo 'hello world! '$DATE >> /root/hello.log

给予hello.sh文件执行权限

$ chmod u+x hello.sh

2、编辑crontab文件

$ crontab -e

输入内容

*/1 * * * * /root/hello.sh

*/1 * * * * 表示每分钟执行一次,crontab只能创建分钟级别的

3、重载crontab配置文件

$ systemctl reload crond.service

4、查看定时任务执行结果(一分钟之后监控hello.log文件)

$ tail -f /root/hello.log

预期结果:

$ tail -f /root/hello.log
hello world! 2019-12-25 14:05:01
hello world! 2019-12-25 14:06:01
hello world! 2019-12-25 14:07:01
hello world! 2019-12-25 14:08:01
hello world! 2019-12-25 14:09:01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值