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

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

苹果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

苹果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

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

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

安装 crontab

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

rpm -qa | grep crontab

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

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

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

dnf install -y crontabs

crontab 常用命令

查看 crontab 运行状态:

systemctl status crond

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

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

设置 crontab 开机自启:

systemctl enable crond

启动 crontab:

systemctl start crond

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

crontab -l

设置定时任务

首先在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 
sleep 10
echo $b
sleep 10
echo $c
sleep 10
echo $d
sleep 10
echo $e

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

也可以使用下面的写法,效果是一样的。

#! /bin/bash

func()
{
     curl  'https://xxx.com/api.php/timming/index.html?enforce=1&name=gs'
     touch  gs
     echo  "gs打开完毕"
}
 
func &
sleep  5
if  [ -f gs ]
then
     echo   "gs成功"
else
     echo  "gs失败"
fi
rm  -f gs

sleep  10

func()
{
     curl 'https://xxx.com/api.php/timming/index.html?enforce=1&name=kc'
     touch  kc
     echo  "kc打开完毕"
}
 
func &
sleep  5
if  [ -f kc ]
then
     echo  "kc成功"
else
     echo  "kc失败"
fi
rm  -f kc

sleep  10

func()
{
     curl 'https://xxx.com/api.php/timming/index.html?enforce=1&name=sd'
     touch  sd
     echo  "sd打开完毕"
}
 
func &
sleep  5
if  [ -f sd ]
then
     echo  "sd成功"
else
     echo  "sd失败"
fi
rm  -f sd

sleep  10

func()
{
     curl 'https://xxx.com/api.php/timming/index.html?enforce=1&name=xl'
     touch  xl
     echo  "xl打开完毕"
}
 
func &
sleep  5
if  [ -f xl ]
then
     echo  "xl成功"
else
     echo  "xl失败"
fi
rm  -f xl

sleep  10

func()
{
     curl 'https://xxx.com/api.php/timming/index.html?enforce=1&name=lz'
     touch  lz
     echo  "lz打开完毕"
}
 
func &
sleep  10
if  [ -f lz ]
then
     echo  "lz成功"
else
     echo  "lz失败"
fi
rm  -f lz

sleep命令语法以下是Bash中sleep命令的语法:
sleep number[suffix]
可以使用正整数或小数作为时间值。后缀是可选部分。可以将以下任意一项用作后缀:

  • s – 表示秒
  • m – 表示分钟
  • h – 表示小时
  • d – 表示天

注意:如果没有后缀,则数字以秒为单位(默认情况下)。

如果指定了两个或多个参数,则总的时间将被视为等于值之和的时间。以下是一些简单的示例,演示了如何使用sleep命令:

  • 睡眠9秒钟,使用:
    sleep 9 或 sleep 9s
  • 睡眠0.5秒钟,使用:
    sleep 0.5 或 sleep 0.5s
  • 睡眠2分30秒,使用:
    sleep 2m 30s
  • 睡眠8小时,使用:
    sleep 8h
  • 睡眠2天9小时5分55秒,使用:
    sleep 2d 9h 5m 55s

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

crontab -e

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

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

crontab 不执行的相关问题

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

curl请求url里含有&请求失败

curl请求里,url含有多个参数时,url需要使用单引号括起来,否则请求会被截断,使用单引号将url的内容括起来就好了。

Ubuntu用命令行打开网页的三种方法

第一种方法 links命令

apt install links
links 'https://uzbox.com'

第二种方法 w3m命令

apt install w3m
w3m 'https://uzbox.com'

第三种方法 lynx命令

apt install lynx
lynx 'https://uzbox.com'

 

 

5/5 - (1 vote)

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注