简体   繁体   English

如何在不同的时间运行3个PHP脚本,每30分钟作为cron作业?

[英]How to run 3 php scripts in different times, every 30 minutes as cron job?

Well, 好,

I have 3 different php scripts and actually it is running by a cron job, but also I have 3 entries in my cron file, and I would like to have only one. 我有3个不同的PHP脚本,实际上它是由一个cron作业运行,但我在我的cron文件中有3个条目,我想只有一个。 I will try to explain: 我会试着解释一下:

1 - File run a query in my data base and save some data in the database. 1 - 文件在我的数据库中运行查询并将一些数据保存在数据库中。 (It takes few seconds) 2 - Run script number 2, this will create 2 new files of configuration inside my web server. (需要几秒钟)2 - 运行脚本编号2,这将在我的Web服务器中创建2个新的配置文件。 (It takes more than 1 minute to execute) 3 - The third file open a ssh connection and exec some commands in the server, it will put the files generated is step 2 into the server and reload the server. (执行需要1分钟以上)3 - 第三个文件打开一个ssh连接并在服务器中执行一些命令,它会将生成的文件放到服务器中并重新加载服务器。

The actual situation: 实际情况:

I execute the 3 scripts each time, all the 3 scripts need to run every 30 minutes, then I run the first one, after 5 minutes I run the second one and finally after 15 minutes I had run the script 2 I run the script 3. 我每次执行3个脚本,所有3个脚本需要每30分钟运行一次,然后我运行第一个脚本,5分钟后运行第二个脚本,最后15分钟后我运行脚本2运行脚本3 。

It works perfect, but I would like to do every thing inside a single php script, is that possible and give only one call in my cron? 它工作得很完美,但我想在一个PHP脚本中做所有事情,这是可能的,只能在我的cron中调用一次吗?

If the scripts all depend on the previous one having completed its work, you'd want to run only the first one at fixed intervals, then have that script execute the other two in sequence. 如果脚本都依赖于前一个完成其工作的脚本,那么您希望以固定的时间间隔仅运行第一个脚本,然后让该脚本按顺序执行其他两个脚本。

stage1.php: stage1.php:

<?php

... do important stuff...

include('stage2.php');
include('stage3.php');

That way, the second and third scripts will NOT start running until after first one has completed whatever it has to do, and gives you the opportunity to abort the whole thing if something failed in the first sequence. 这样,第二个和第三个脚本将在第一个脚本完成任何必须执行的任务之后才开始运行,并且如果第一个序列中的某些内容失败,您将有机会中止整个脚本。

Any other way would be very racey. 任何其他方式都会非常有竞争力。 Consider the following sequence: 请考虑以下顺序:

  • script #1 runs a backup 脚本#1运行备份
  • script #2 transfers backup file to another server 脚本#2将备份文件传输到另一台服务器
  • script #3 cleans up from the backup 脚本#3从备份中清除

For whatever reason script #1 runs long, to the point that script #2 kicks in and starts transferring the backup file BEFORE the backup has completed. 无论出于什么原因脚本#1运行很长时间,脚本#2启动并在备份完成之前开始传输备份文件。 The file transfer takes extra long because the network's busy, so now you've the backup running, it's being transferred to the other server, AND the cleanup script has now gone and deleted the .tar.gz that script #1 was making. 文件传输需要额外的长,因为网络繁忙,所以现在你已经备份运行,它被转移到其他服务器, 清理脚本现在已经消失了,并删除了名为.tar.gz该脚本#1制作。

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

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