Linux下crontab定时任务的使用

79 阅读1分钟

1.编写shell脚本,表明需要执行的代码,并赋权限

vim save.sh

#!/bin/bash
echo "我呀">>/usr/local/test/haha.txt

chmod 777 save.sh

2.进入系统定时任务管理

vim /etc/crontab

image.png

参数详解可参考crontab执行时间计算 - 在线工具 (tool.lu)

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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
* * * * * root /bin/bash /usr/local/save.sh

3.重载定时任务

systemctl restart crond

进入redis-cli命令

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a zhoujian save