yano3nora
11/5/2017 - 9:36 AM

[php: exec()] Executing OS shell from php. #linux #php

[php: exec()] Executing OS shell from php. #linux #php

<?php

/**
 * Synchronous.
 * The below processing waits for the end of the executed command.
 */
exec('ls', $output, $return_vars);
var_dump($return);
// [
//   0 => 'dr-xr-xr-x    2 root  wheel      512 Jan  1  2009 proc',
//   1 => 'drwxr-xr-x    2 root  wheel     2560 Sep 24  2010 rescue',
//   2 => 'drwxr-x---    5 root  wheel      512 Aug  2  2010 root',
// ]


/**
 * Asyncronous.
 * Redirect the command executed to dev/null and proceed the process.
 */
$command = '何かしら重たい非同期でやりたい処理';
exec('nohup '.$command.' > dev/null &');