开发者

How to get the "at" shell command output in php?

开发者 https://www.devze.com 2023-03-08 19:45 出处:网络
I want to run the \"at\" command of the shell and get back the resulting status, I used the following script which adds an entry in the list of scheduled jobs and executes it after one minute (just an

I want to run the "at" command of the shell and get back the resulting status, I used the following script which adds an entry in the list of scheduled jobs and executes it after one minute (just an example):

try{
    $output = shell_exec("at now + 1 minutes \n curl http://www.mysite.com/test.php?key=value");
    echo $output;
}
catch (Exception $e) {
    echo "Error :" . $e->getMessage();
}

The command is well executed (I checked this with "atq" shell command and the script of the page test.php was also executed as planned after a minute) but the variable $output returned nothing!

I used exec, system and passthru function instead of shell_exec but always nothing as output.

I want to have the returned value of this function to retrieve the added job ID and keep it for later use (e.g. removal of this j开发者_运维百科ob).


that wont work like this. the program input data that you seperated using a newline character is not part of the shell command invoking the program. this data needs to be passed to STDIN of the program once its running.

you have to open the program and have resource pointers for both standard input and standard output. than you can read/write to/from them

have a look at the documentation and examples of this php function:

http://www.php.net/manual/en/function.proc-open.php

as for "how to get the output":

this is somehow irrelevant because the you cannot use the program like you probably wish with this method. but if you just want to capture/pass the data from STDOUT you can use passthru(). you can turn on output buffering to capture the output. its for example useful if you want to display the status bar of rsync in a php-cli script invoking it.


Instead of trying to setup your command like that, you can use echo with a pipe to do what you want:

try{
    $output = shell_exec('echo "curl \'http://www.mysite.com/test.php?key=value\'" | at now + 1 minutes');
    echo $output;
}
catch (Exception $e) {
    echo "Error :" . $e->getMessage();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号