using exec() to run unix `at` command
I need to run unix at
command using PHP. How is it possible to do it? curl
is not an option for what I am doing. If I try开发者_开发技巧 to run it simply exec('at')
it gives no response. Though, if I run it using ssh, it works fine. So I guess this is permissions/path problem.
Remember that "at" receives its script from stdin, so you'll want to open a pipe to the at command using popen(). Write your commands to that pipe and close it. The time that you want "at" to run the command should be part of the command line when you open the pipe.
<?php
$r = popen('/usr/bin/at noon');
fwrite($r,"ls -l");
pclose($r);
?>
I suppose you have some real at
command line because only at
is invalid syntax. Your command should include some arguments.
However maybe your problem is that at
is not in $PATH
. Try to run at with the full path which is typically /usr/bin/at
. Check this with whereis at
.
Run cat /etc/passwd
to check whitch shell is using the apache user, if is bin/false
you can NOT run 'at' from apache user.
I found a tricks for this issue by exporting shell over bin/bash before running the shell command, something like this:
export SHELL=/bin/bash; echo "shell command" | at :time: :date:
and passing it as argument for the php exec() function.
In this way you'll be able to run your 'at' scheduling from apache.
The reason is in the file "/etc/at.deny". Scan through the file you will see a line "www-data" which is the user that apache2 is running (the last line in the file in my case). This denies user "www-data" to run the "at" command. Simply delete that line from /etc/at.deny. Then you can run "at" using "shell_exec".
Hope it helps.
精彩评论