简体   繁体   English

Cron Job-如何在特定日期两次执行脚本?

[英]Cron Job - How to execute a script between two times on specific days?

I am new to php and database stuff, but have looked around and cannot find a solution. 我是php和数据库方面的新手,但四处张望,找不到解决方案。

What I'm trying to accomplish: 我要完成的工作:

I have a slideshow of images. 我有一张图片幻灯片。 The images come from Flickr's API, stored in a database table, then the database data is generated into a JSON link. 图像来自Flickr的API,存储在数据库表中,然后将数据库数据生成到JSON链接中。

My question is, is there a way to execute the php script that calls the flickr API everytime the slideshow circle through all the images in the JSON link? 我的问题是,有没有一种方法可以执行每次幻灯片浏览JSON链接中的所有图像时都会调用flickr API的php脚本? I could specify every 1 minutes, but that may be too long if there are only a few images in the slideshow. 我可以每1分钟指定一次,但是如果幻灯片中只有几张图像,那可能会太长。

If you can't do that, 如果你做不到

is there a way to execute the script every 1 minute on only fridays from 9pm EST to 3am EST? 有没有办法在美国东部标准时间晚上9点到美国东部标准时间凌晨3点之间的每个星期五每1分钟执行一次脚本?

The answer is yes there is a way but how all depends on the slideshow you are using. 答案是肯定的。有一种方法,但是所有方法都取决于您使用的幻灯片。 If you didn't build it i would hunt for a function that's probably running at the end of the slideshow to trigger it to loop to the beginning. 如果您没有构建它,我会寻找一个可能在幻灯片末尾运行的功能,以触发它循环到开头。 When you find that function you can alter it to run the script that connects to the API to get new images. 找到该函数后,可以对其进行更改以运行连接到API的脚本以获取新图像。

You might have to delay the loop in the slideshow long enough for the new images to load. 您可能需要将幻灯片中的循环延迟足够长的时间,以使新图像可以加载。

This is the cron specification for your last line: 这是您最后一行的cron规范:

# Friday from 9pm to 11:59pm
* 21-23 * * 5 php -q /path/to/script.php
# Saturday from midnight to 2:59am
* 0-2 * * 6 php -q /path/to/script.php

On your specific question of how to schedule it, would be something like 关于如何安排它的特定问题,可能是这样的

* 21-23 * * 5 /path/to/php /path/to/script
* 0-2 * * 6 /path/to/php /path/to/script

in your crontab file. 在您的crontab文件中。 Easiest to split into two, as you actully have commands running over two days. 最容易分为两部分,因为您确实可以在两天内运行命令。 The first * means run it every minute. 第一个*表示每分钟运行一次。

See http://en.wikipedia.org/wiki/Cron 参见http://en.wikipedia.org/wiki/Cron

The stuff with your gallery, is not particully clear, exactly what you doing. 您画廊的内容还不清楚,确切地说您在做什么。

You really have two aspects of this, which you likely need to do independently of one another. 您实际上有两个方面,您可能需要彼此独立地进行。

First, you need a way to check the Flickr API for updates. 首先,您需要一种方法来检查Flickr API的更新。 I would suggest this could be a script which builds a cached representation of the data that is available for local consumption on the service. 我建议这可能是一个脚本,用于构建可用于服务本地使用的数据的缓存表示。 You can trigger this via cronjob (you already have good answers here on how to configure cron so I will skip that). 您可以通过cronjob触发它(您已经在如何配置cron的问题上有了很好的答案,因此我将跳过它)。

You don't the client to initiate the calls to the Flickr API as if you had 60 concurrent clients checking for updates all the way through to Flicker every minute, you would in essence be checking on average once a second for updates. 您不必让客户端发起对Flickr API的调用,就好像您有60个并发客户端每分钟一直在检查直到Flicker的更新一样,从本质上讲,您实际上应该平均每秒检查一次更新。

Your cached representation of the Flickr data can be in whatever format you consider easy to work with (JSON perhaps). Flickr数据的缓存表示形式可以是您认为易于使用的任何格式(也许是JSON)。 You would expose the cached data to the frontend (ie javascript) on page load and via some server-side script that could be called via AJAX to get the cached data. 您将在页面加载时通过某些服务器端脚本将缓存的数据公开给前端(即javascript),该服务器端脚本可以通过AJAX调用以获取缓存的数据。

You could make the data update into javascipt independent of the scrolling behavior by having he call running every minute to the serverside script and populating the latest data into some javascript variable. 您可以通过让他调用每分钟运行到服务器端脚本并将最新数据填充到某个javascript变量中来使数据更新为与滚动行为无关的javascipt。

You could then either directly update the slideshow every minute, or as you suggest, have the slideshow rebuilt from the latest data (which is already in javascript) when the scrolling loops back to the beginning. 然后,您可以每分钟直接更新幻灯片一次,或者根据您的建议,在滚动返回到开头时,从最新数据(已经在javascript中)重建幻灯片。

One thing you might also consider is, if possible keep data on when the slideshow information was last updated in Flickr, the last time it was cached on the server, and the last time javascript pulled the data from the server. 您可能还考虑的一件事是,如果可能的话,请在Flickr中最后一次更新幻灯片信息时,最后一次将其缓存在服务器上以及javascript最后一次从服务器提取数据时保持数据。 By comparing these update times, you can save yourself a lot of hassle in rebuilding the cache, rebuilding the representation in javascript, etc. by simply determining if there is indeed an update to be had first. 通过比较这些更新时间,您可以通过简单地确定是否确实要先进行更新来节省许多麻烦,以重建缓存,重建javascript中的表示形式等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM