我试图在特定时间将文件从一个文件夹移动到另一个文件夹。为了实现这一点,我试图使用Linux的at命令与管道:
`mv file /to/dest | at h:m d.m.y`
这是我写的:
$move = "mv $filename /destination/folder";
$at = "at $my_datetime";
$res = execute_pipe($move,$at);
其中execute_pipe函数定义如下:
function execute_pipe($cmd1 , $cmd2)
{
$proc_cmd1 = proc_open($cmd1,
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
$output_cmd1 = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_cmd1 = proc_close($proc_cmd1);
$proc_cmd2 = proc_open($cmd2,
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
fwrite($pipes[0], $output_cmd1);
fclose($pipes[0]);
$output_cmd2 = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_cmd2 = proc_close($proc_cmd2);
return $output_cmd2;
}
问题是文件被立即移动,忽略了at
命令。我错过了什么?有更好的方法吗?
对我来说,你的问题似乎与PHP无关。你只是用错了壳层。at
的Man页显示:
at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh.
但是您使用的shell确实执行了"mv file/destination"命令,然后将该命令的输出管道传输到at
。在一个成功的移动操作上,输出将什么都没有。因此,通过使用管道,您实际上可以立即移动文件,并告诉at
在指定的时间不做任何事情。
阅读at
的手册页,在终端中输入man at
来解决这个问题。提示:如果您想使用STD INPUT,回显命令可能会有所帮助;)