Question

I need to use PHP to schedule tasks for later execution on a local machine. I am avoiding cron because I do not need to repeat the task over time.

I tried

$msg="at 10am today
date > /path/path/date.txt
\x04";
shell_exec($msg); // Nope

$msg="at 10am today
date > /path/path/date.txt
".chr(4);
shell_exec($msg); // Nope

$msg=<<<EOT
at 10am today
date > /path/path/date.txt
EOT;
shell_exec($msg); // Nope

and

shell_exec('echo "date > /some/path/date.txt" | at 10am today');

That last line works when executing the script with php in Terminal but not when going to the script in my web browser (http://localhost/script.php). I've tried system too but to no success. By the way, echo shell_exec('ls /') works without problem, so I don't think it's a permission issue? Does anyone have any ideas what's wrong?


Background information: the motivation for this is that I am building a web-based interface for specifying and submitting computationally intensive simulation jobs to remote machines. My PHP script generates R and Matlab scripts from user-input, and I can SSH into those machines from PHP just fine. I just can't schedule those scripts to be executed at a later time, independent of the SSH session.

Was it helpful?

Solution

The probem is, as you suggest, on wich user execute the script. I think that the classic www-data can't use at command

Add dedicated user as normal user (adduser phpschedule); Then change user that execute php in apache from classic www-data to phpschedule

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top