Reputation: 195
I need a scheduler (for one time only actions) for a site I'm coding (in php), and I had two ideas:
1- Run a php script with crontab and verify against a database of scheduled actions and execute ones that are older than current time.
2- Schedule various tasks with the "at" command.
The second option seems much better and simpler, so that's what I'm trying to do. However, I haven't found a way to tell "at" to run a command using the PHP interpreter, and so far I've been creating a .sh script, which contains a single command, which is to run a file through the php interpreter. That is far from the optimal setting, and I wish I could just execute the php code directly through "at", something like:
at -e php -f /path/to/phpscript time
Is it possible? I haven't found anything about using environments other than bash in either the man or online.
Upvotes: 0
Views: 2065
Reputation: 263627
The command you specify to at
is executed by /bin/sh
, but sh can invoke any command, executed directly or by any specified interpreter.
The following works on my Ubuntu 12.04 system with the bash shell:
$ cat hello.php
#!/usr/bin/php
<?php
echo "Hello, PHP\n";
?>
$ echo "$PWD/hello.php > hello.php.out" | at 16:11
warning: commands will be executed using /bin/sh
job 4 at Sat Aug 25 16:11:00 2012
$ date
Sat Aug 25 16:11:05 PDT 2012
$ cat hello.php.out
Hello, PHP
$
In some cases, you'll have to do some extra work to set environment variables correctly (it's not necessary for this simple case). Quoting the man page:
For both at and batch, commands are read from standard input or the file specified with the -f option and executed. The working directory, the environment (except for the variables BASH_VERSINFO, DISPLAY, EUID, GROUPS, SHELLOPTS, TERM, UID, and _) and the umask are retained from the time of invocation.
As at is currently implemented as a setuid program, other environment variables (e.g. LD_LIBRARY_PATH or LD_PRELOAD) are also not exported. This may change in the future. As a workaround, set these variables explicitly in your job.
Upvotes: 0
Reputation: 12673
You can prepend phpscript
with a #!/usr/bin/php
(or wherever your php script is stored) and make /path/to/phpscript
executable. This is exactly what the #!
syntax is for.
Just so it's clear, your phpscript
would look like this:
#!/usr/bin/php
...your code goes here
Upvotes: 2