0

My server has a lot of photos saved. I would like to archive (.zip) those photos and download it from the server. I have created a PHP script to make archive those photos into a .zip file and download it.

If I execute that file directly through the browser, it's getting a timeout error. So I have created a CRON job to execute the script. But I only need to execute the CRON job whenever I need it. (Like clicking a button to execute the script).

Thanks for all the help!

Rav
  • 1,327
  • 3
  • 18
  • 32
sathivel
  • 3
  • 1
  • 6
  • 1
    …the whole point of cron is to schedule things to run at a particular time. Triggering a cron job when you click a button doesn't make any sense. I guess maybe you just want a way to run a job in the background, possibly without a timeout? – ChrisGPT was on strike Nov 29 '16 at 02:27
  • What is your command for crone ? Please add command here. – Prakash Saini Nov 29 '16 at 02:27
  • php /XXX/XXX/public_html/cli.php /cron_v2/index - 0 1 * * * this will working . Actually I need call the file whenever I click any button . Thank you. – sathivel Nov 29 '16 at 02:31
  • Thank Mr Chris , Is there any option control the crone Job through php scripts. Actually I Need the run a job in background without timeout . Thanks. – sathivel Nov 29 '16 at 02:35
  • @sathivel, again, trying to "control a cron job" from PHP doesn't make any sense. Forget about cron. Instead, you should be asking how to run _your code_. – ChrisGPT was on strike Nov 29 '16 at 02:42
  • Okay Noted Thanks Mr Chris . Is there anyway to execute my php script with background without timeout ? – sathivel Nov 29 '16 at 02:48
  • Possible duplicate of [Serving large files with PHP](http://stackoverflow.com/questions/432713/serving-large-files-with-php) – Mohammad Yusuf Nov 29 '16 at 05:05

1 Answers1

1

See the set_time_limit function to prevent your timeout error.

http://php.net/manual/en/function.set-time-limit.php

As for running a cron as-needed, that isn't how a cron works. A cron executes a command at a set interval. A PHP script can include code to exit immediately if it's processing isn't needed. You will need to create some way to signal to the script if it needs to run or not. You could do this by updating a file or database record.

Alternatively you can use an application server to execute commands (scripts) as-needed via web service calls. PHP doesn't have good support for multi-threading which is needed for most application servers so you will likely need to use Java, Python, or Ruby to create web services to launch scripts as-needed in real time. You can then call these web services from PHP using cURL functions.

Ralph Ritoch
  • 3,260
  • 27
  • 37