加载中...
加载中...
Linux下定时执行shell脚本

Linux下定时执行shell脚本 原创

1、定义shell脚本
vi test.sh

录入要执行的命令

source /etc/profile
source ~/.cshrc

date >> /opt/data.txt
保存退出,并且对脚本进行授权

:wq

chmod 777 test.sh

生产文件data.txt

touch /opt/data.txt

2、配置定时执行
vi /etc/crontab

录入:
一分钟执行一次

*/1 * * * * root /opt/test.sh
保存退出即可每个一分钟执行一次

:wq
配置说明:


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
0 5 * * * root /root/startup.sh

*/1 * * * * root /opt/test.sh



linux 查看crontab

[root@luolei ~]# rpm -qa | grep cron
cronie-anacron-1.4.11-20.el7_6.x86_64
crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-20.el7_6.x86_64

crontab -l查看定时任务列表

查看执行日志

[root@luolei ~]# tail -f /var/log/cron
Apr 1 09:11:21 luolei crontab[14368]: (root) LIST (root)
Apr 1 09:11:30 luolei crontab[14369]: (root) LIST (root)
Apr 1 09:12:01 luolei crond[558]: (*system*) RELOAD (/etc/crontab)
Apr 1 09:12:01 luolei CROND[14378]: (root) CMD (/root/startup.sh)



每小时执行一次

0 */1 * * * root /root/startup.sh



crontab每分钟定时执行:

*/1 * * * * service mysqld restart //每隔1分钟执行一次
*/10 * * * * service mysqld restart //每隔10分钟执行一次

crontab每小时定时执行:

0 */1 * * * service mysqld restart //每1小时执行一次
0 */2 * * * service mysqld restart //每2小时执行一次

crontab每天定时执行:

0 10 * * * service mysqld restart //每天10点执行
30 19 * * * service mysqld restart //每天19点30分执行

crontab每周定时执行:

0 10 * * 1 service mysqld restart //每周一10点执行
30 17 * * 5 service mysqld restart //每周五17点30分执行

crontab每年定时执行:

0 10 1 10 * service mysqld restart //每年的10月1日10点执行
0 20 8 8 * service mysqld restart //每年的8月8日20点执行



没有更多推荐了 [去首页]
image
文章
376
原创
293
转载
83
翻译
0
访问量
183411
喜欢
73
粉丝
5
码龄
7年
资源
3

文章目录

加载中...
0
0