6

苹果cms定时任务 Crontab设置苹果cms自动更新脚本

 1 year ago
source link: https://uzbox.com/tech/linux/cms-crontab.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
首页技术宅Linux苹果cms定时任务 Crontab设置苹果cms自动更新脚本

苹果cms定时任务 Crontab设置苹果cms自动更新脚本

苹果cms定时任务 如何使用Crontab命令设置定时任务自动更新

苹果cms定时任务 苹果cms的定时任务更新有几种方式,如果是在Windows中搭建的苹果cms,可以使用windows自带的定时任务更新,如果是Linux的操作系统可以在第三方平台上设置定时更新的任务,或者在阿里云,腾讯云这种第三方网站上设置自定更新任务。下面介绍另外一种方式,在Linux系统中使用crontab命令设置定时任务来自动更新网站

了解什么是 crontab 定时任务

通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。这个命令非常适合周期性的日志分析或数据备份等工作。

crontab [-u user] file crontab [-u user] [ -e | -l | -r ]

-u user:用来设定某个用户的crontab服务;
file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。
-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。
-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。
-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。
-i:在删除用户的crontab文件时给确认提示。

crontab的文件格式

分 时 日 月 星期 要运行的命令

  • 第1列分钟0~59
  • 第2列小时0~23(0表示子夜)
  • 第3列日1~31
  • 第4列月1~12
  • 第5列星期0~7(0和7表示星期天)
  • 第6列要运行的命令
# .---------------- 分钟,取值范围为 0-59
# | .------------- 小时,取值范围为 0-23
# | | .---------- 日,取值范围为 1-31
# | | | .------- 月,取值范围为 1-12
# | | | | .---- 星期,取值范围为 0-7,0 和 7 都表示星期日
# | | | | | .-- 要执行的命令
# | | | | | |
0 19 * * * bash /root/test.sh
# .---------------- 分钟,取值范围为 0-59
# |  .------------- 小时,取值范围为 0-23
# |  |  .---------- 日,取值范围为 1-31
# |  |  |  .------- 月,取值范围为 1-12
# |  |  |  |  .---- 星期,取值范围为 0-7,0 和 7 都表示星期日
# |  |  |  |  |      .-- 要执行的命令
# |  |  |  |  |      |
  0  19 *  *  * bash /root/test.sh

苹果cms定时任务

下面配置苹果cms的定时任务,首先在苹果cms的后台的系统选项中进行定时任务配置

苹果cms定时任务 Crontab设置苹果cms自动更新脚本

在需要自动更新的任务后面的操作选项中,在测试上点击鼠标右键,然后点击复制链接地址获取测试的URL地址。

https://xxx.com/api.php/timming/index.html?enforce=1&name=lz
https://xxx.com/api.php/timming/index.html?enforce=1&name=sd
https://xxx.com/api.php/timming/index.html?enforce=1&name=xl
https://xxx.com/api.php/timming/index.html?enforce=1&name=kc
https://xxx.com/api.php/timming/index.html?enforce=1&name=gs
https://xxx.com/api.php/timming/index.html?enforce=1&name=lz
https://xxx.com/api.php/timming/index.html?enforce=1&name=sd
https://xxx.com/api.php/timming/index.html?enforce=1&name=xl
https://xxx.com/api.php/timming/index.html?enforce=1&name=kc
https://xxx.com/api.php/timming/index.html?enforce=1&name=gs

这些url在浏览器中打开,需要是可以正常更新网站的有效链接。

接下来在Linux系统中设置定时任务。

安装 crontab

一般 CentOS 会默认安装了 crontab,执行下面命令,查看是否安装:

rpm -qa | grep crontab
rpm -qa | grep crontab

如果查询结果类似于下面,说明已安装:

苹果cms定时任务 Crontab设置苹果cms自动更新脚本

如果结果为空,说明没有安装,执行下面命令进行安装即可:

dnf install -y crontabs
dnf install -y crontabs

crontab 常用命令

查看 crontab 运行状态:

systemctl status crond
systemctl status crond

如上图,如果显示结果为 Active: active (running) 表示运行中,Active: inactive (dead) 则表示未运行。

如果 crontab 未运行,可以通过下面命令设置开机自启和启动。

设置 crontab 开机自启:

systemctl enable crond
systemctl enable crond

启动 crontab:

systemctl start crond
systemctl start crond

查看当前用户的定时任务:

crontab -l
crontab -l

设置定时任务

首先在root目录下创建一个vod.sh的脚本。

vi /root/vod.sh
vi /root/vod.sh

按 i 进入编辑状态,粘贴下面内容:

#! /bin/bash
a=$(curl -k https://xxx.com/api.php/timming/index.html?enforce=1&name=lz)
b=$(curl -k https://xxx.com/api.php/timming/index.html?enforce=1&name=sd)
c=$(curl -k https://xxx.com/api.php/timming/index.html?enforce=1&name=xl)
d=$(curl -k https://xxx.com/api.php/timming/index.html?enforce=1&name=kc)
e=$(curl -k https://xxx.com/api.php/timming/index.html?enforce=1&name=gs)
echo $a $b $c $d $e
#! /bin/bash
a=$(curl -k  https://xxx.com/api.php/timming/index.html?enforce=1&name=lz)
b=$(curl -k  https://xxx.com/api.php/timming/index.html?enforce=1&name=sd)
c=$(curl -k  https://xxx.com/api.php/timming/index.html?enforce=1&name=xl)
d=$(curl -k  https://xxx.com/api.php/timming/index.html?enforce=1&name=kc)
e=$(curl -k  https://xxx.com/api.php/timming/index.html?enforce=1&name=gs)
echo $a $b $c $d $e

按 Esc 键,再输入 :wq 保存文件。

执行下面命令可以编辑当前用户的定时任务:

crontab -e
crontab -e

执行完 crontab -e 命令后,会打开一个文档,按 i 进入编辑状态。将下面内容粘贴在文档中。然后保存退出!

0 19 * * * bash /root/vod.sh
0 19 * * * bash /root/vod.sh

上面这段代码的意思是,每天的19点,执行 /root/vod.sh 脚本。

如果你想每个小时执行一次脚本,可以逐条添加。

注意:在定时任务的配置文件中,时间的24点,不能输入 24 ,要输入 0 ,否则会报错!

0 1 * * * bash /root/vod.sh
0 2 * * * bash /root/vod.sh
0 3 * * * bash /root/vod.sh
0 4 * * * bash /root/vod.sh
0 5 * * * bash /root/vod.sh
0 6 * * * bash /root/vod.sh
0 7 * * * bash /root/vod.sh
0 8 * * * bash /root/vod.sh
0 9 * * * bash /root/vod.sh
0 10 * * * bash /root/vod.sh
0 11 * * * bash /root/vod.sh
0 12 * * * bash /root/vod.sh
0 13 * * * bash /root/vod.sh
0 14 * * * bash /root/vod.sh
0 15 * * * bash /root/vod.sh
0 16 * * * bash /root/vod.sh
0 17 * * * bash /root/vod.sh
0 18 * * * bash /root/vod.sh
0 19 * * * bash /root/vod.sh
0 20 * * * bash /root/vod.sh
0 21 * * * bash /root/vod.sh
0 22 * * * bash /root/vod.sh
0 23 * * * bash /root/vod.sh
0 0 * * * bash /root/vod.sh
0 1 * * * bash /root/vod.sh
0 2 * * * bash /root/vod.sh
0 3 * * * bash /root/vod.sh
0 4 * * * bash /root/vod.sh
0 5 * * * bash /root/vod.sh
0 6 * * * bash /root/vod.sh
0 7 * * * bash /root/vod.sh
0 8 * * * bash /root/vod.sh
0 9 * * * bash /root/vod.sh
0 10 * * * bash /root/vod.sh
0 11 * * * bash /root/vod.sh
0 12 * * * bash /root/vod.sh
0 13 * * * bash /root/vod.sh
0 14 * * * bash /root/vod.sh
0 15 * * * bash /root/vod.sh
0 16 * * * bash /root/vod.sh
0 17 * * * bash /root/vod.sh
0 18 * * * bash /root/vod.sh
0 19 * * * bash /root/vod.sh
0 20 * * * bash /root/vod.sh
0 21 * * * bash /root/vod.sh
0 22 * * * bash /root/vod.sh
0 23 * * * bash /root/vod.sh
0 0 * * * bash /root/vod.sh

crontab 不执行的相关问题

  • 检查脚本路径前是否添加了 bash 或 /etc/profile;/bin/sh
  • 检查 crontab 服务是否正常
  • 检查脚本路径是否绝对路径

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK