4

Is it possible to schedule a single php function to run at a specific time in the future using the Unix 'at' command? If so how would this be done?

Also is this the best way to handle scheduling a single function to run at a later date?

hlovdal
  • 26,565
  • 10
  • 94
  • 165
Sherms
  • 1,567
  • 1
  • 15
  • 31

3 Answers3

6

Sure, you can use at or cron, and pass the -R flag to PHP to execute the command line:

# Run phpinfo() at 12:30...
echo php -R 'phpinfo();' | at 12:30

Or it is perhaps better to call a file which may contain multiple commands

echo 'php /path/to/yourfile.php' | at 12:30
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • What about a function that I created? Is there any way to have that called, or can it only be standard php functions? Are variables set in the script that calls the above available to the function when it is run later? – Sherms Feb 27 '12 at 19:30
  • 1
    @RhodesianHunter To execute your own function, you would need to include the file then call it. At that point you're much better off just to execute a file that defines and executes your function. To make values available later, you would need to write them to a database or text file, since there would be no persistence between calls. – Michael Berkowski Feb 27 '12 at 19:43
  • I was forgetting to write "echo" before the command that will execute my php file. Thank you! – cawecoy Jun 06 '13 at 19:14
  • 1
    @cawecoy Right - it would attempt to execute and push the output of the PHP file into `at` rather than the command to execute it, I suppose. – Michael Berkowski Jun 06 '13 at 19:18
1

if you want to run it from php script try using exec() function

Sergei Kutanov
  • 825
  • 6
  • 13
0

To do that a PHP script would need to be persistent inside the PHP interpreter for some long interval of time.

The best you could do would be to use something like exec to create the unix command you's use and then call the php command line version to make it work. Look at Scheduling php scripts for more information.

Community
  • 1
  • 1
artlung
  • 33,305
  • 16
  • 69
  • 121