is there a way to enable a php script (apache user) to schedule a task using the AT daemon?
I would like to pipe the task to the atd from my php script like this:
exec( "echo 'date > /some/dir/date.txt' | at now + 1 minute > /dev/null &" );
when I run the php script from the command line (root) everything is fine, however, when I run the script via a http request the at job is never created.
I am aware that apache doesn't have shell access, is there any work around available?
exec()
does not use a shell so you can't use pipes etc..
Use backticks, shell_exec()
or system()
instead to make it work.
shell_exec("echo 'date > /some/dir/date.txt' | at now + 1 minute");
If the account your script runs under (Apache or a cgi user) does not have shell access, at will not work, because the disfunctional shell (like /bin/bad) will be tried to run the command later on.
Check if you can exec anything:
$output = array();
exec('whoami',$output);
print_r($output);
If this does work, you could try something like (pseudocode):
write "date > /some/dir/date.txt" in /tmp/atcmd
exec("/usr/bin/at -f /tmp/atcmd now + 1 minute");
So you need: - A shell to run the command in, later on - Permision inside php to run the exec function - No problems created by /etc/at.{deny,allow}
This basically means (I guess) you need a PHP that runs in php-cgi switched to a "real" account.
The only other solution I can think of right now is to create the script you need on the webserver, and to run the at job on a machine you do have access to, using wget.
echo "wget -q http://myserver.mydomain/my/atscript" | at now + 1 minute
People actually use such constructs for cron-like behaviour. Hey, I might even setup a service for stuff like that on my hosting service :-)