0

For some strange reason this

echo system("echo 'echo hello > /dev/pts/2' | /usr/bin/at 19:36");

Refuses to work from my php script, however the command works fine when I just enter it through command line.

I know php has permission to execute some commands. I can run 'ls' from the php script but not the 'at' command. I've tried playing around with file permissions, but so far to no avail :(

edit

Permissions for /usr/bin/at are:

-rwxr-sr-x 1 daemon daemon 42752 Jan 15 2011 at

I think it's a permissions problem, if I execute the php file from my ssh terminal it works fine, but not from the web.

Emmanuel
  • 4,933
  • 5
  • 46
  • 71

2 Answers2

1

What you are executing is

echo 'hello' > /dev/pts/2 | /usr/bin/at 19:36

meaning

echo 'hello' > /dev/pts/2

and pipe stdout to /usr/bin/at 19:36 but since you already redirected the echo to /dev/pts/2, this will be empty. What you probably meant to do is:

echo system("echo 'echo hello > /dev/pts/2' | /usr/bin/at 19:36");

You might also want to use shell_exec to pass the command through a shell or alternatively proc_open which gives you better control over stdin/out/err of the command you are executing. Your example would correspond to (adapted example from php.net docs):

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")  // stderr is a pipe that the child will write to
);

$process = proc_open('/usr/bin/at', $descriptorspec, $pipes);

if (is_resource($process)) {
    fwrite($pipes[0], 'echo "hello" > /dev/pts/2');
    fclose($pipes[0]);

    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    $return_value = proc_close($process);
    echo "command returned $return_value. stdout: $stdout, stderr: $stderr\n";
} else {
    echo "Process failed";
}
?>
mensi
  • 9,580
  • 2
  • 34
  • 43
  • Mmm... I wish it worked, but it still doesn't :(. I think it's a permissions problem, if I execute the php file from my ssh terminal it works fine, but not from the web. – Emmanuel May 22 '12 at 19:55
  • Your original code will write hello instantly and completely ignore `at` – mensi May 22 '12 at 19:57
  • Okay, so that's one problem solved :), but at the moment php won't even touch the `at` command :( – Emmanuel May 22 '12 at 19:58
  • 1
    For permission settings, also have a look at the manpage, you can control which users can use `at` in `/etc/at.allow` and `/etc/at.deny` – mensi May 22 '12 at 19:58
  • www-data was in the /etc/at.deny file. I've removed it from there and added it to the at.allow file, but! it still doesn't work :( – Emmanuel May 22 '12 at 20:03
  • So the script invoked via CLI works as expected now, but when run via Apache it does not? Make sure it really is `at` failing and not `system()` in general, i.e. try to execute `system("echo "bla" > /tmp/test")` and check if it wrote to `/tmp/test` – mensi May 22 '12 at 20:07
  • system("echo "bla" > /tmp/test") works perfectly well. So it's not a problem with system(). Let us [continue this discussion in chat (click here)](http://chat.stackoverflow.com/rooms/11600/discussion-between-emmanuel-and-mensi) – Emmanuel May 22 '12 at 20:09
0

In your php.ini file check for disable_functions sometimes functions like system are disabled for security reasons.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37