Linux编写Shell脚本与Crontab定时执行

1.编写Shell脚本

新建脚本文件

1
vi goaccess

添加以下内容

1
2
3
4
5
6
7
8
9
#!/bin/bash
#
#goaccess cron every five minutes

set -e

goaccess -f /var/www/log/access.log -a > /var/www/html/report.html

exit 0

直接运行脚本

1
bash goaccess

2.crontab定时执行

新建定时任务

1
vi /etc/crontab

添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/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

*/5 * * * * root bash /etc/cron.hourly/goaccess
八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!