0

I have a php function to save multiple rows in database, I can't use this on the web requests since it takes much time. In the function, first I need to obtain a list of ids from database, do some processing, then save new set data into another table. Is it possible to run this script from php, for example from a controller?

6
  • exec('php other_script.php') Commented Apr 28, 2014 at 3:42
  • 3
    Why don't you just use a cronjob and run the page that takes time? Set its execution time to 0 so it won't stop... Commented Apr 28, 2014 at 3:42
  • @Thomas what's the "execution time" ? Commented Apr 28, 2014 at 3:48
  • 1
    @Dagon I think you were being sarcastic, but he means set_time_limit() for anyone else who wasn't aware. php.net/manual/en/function.set-time-limit.php Commented Apr 28, 2014 at 3:50
  • no, i really did not know what he meant and was curious, and that's not an issue with cron jobs, php cli has no default time limit Commented Apr 28, 2014 at 3:50

1 Answer 1

1

Use shell_exec

$output = shell_exec('php YourFile.php');

OR

exec

exec('php YourFile.php')

OR

cronjob ( As @Thomas mentioned )

* * * * * php /var/www/file.php >> /var/www/log/cron.log 2>&1

If you want to run it continuously set_time_limit(0)

<?php

set_time_limit(0);
Sign up to request clarification or add additional context in comments.

3 Comments

Problem is this will block until it finishes, better to use a cron so it doesn't block.
or just add " > /dev/null 2>/dev/null &" to exec
as above you don't need to set_time_limit(0) when the script is called from cli. its 0 by defult

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.