简体   繁体   中英

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. I will try to explain:

1 - File run a query in my data base and save some data in the database. (It takes few seconds) 2 - Run script number 2, this will create 2 new files of configuration inside my web server. (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.

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.

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?

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:

<?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
  • script #2 transfers backup file to another server
  • script #3 cleans up from the backup

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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