code_junkie

Asynchronous shell exec in PHP

Nov 11th, 2011
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. "> /dev/null 2>/dev/null &"
  2.  
  3. " &> /dev/null &"
  4.  
  5. <?php
  6. `echo "the command"|at now`;
  7. ?>
  8.  
  9. $cmd = 'nohup nice -n 10 /usr/bin/php -c /path/to/php.ini -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & echo $!';
  10. $pid = shell_exec($cmd);
  11.  
  12. mycommand -someparam somevalue &
  13.  
  14. start mycommand -someparam somevalue
  15.  
  16. $pid=pcntl_fork();
  17. if($pid==0)
  18. {
  19. posix_setsid();
  20. pcntl_exec($cmd,$args,$_ENV);
  21. // child becomes the standalone detached process
  22. }
  23.  
  24. // parent's stuff
  25. exit();
  26.  
  27. #!/bin/sh
  28. mkfifo trigger
  29. while true; do
  30. read < trigger
  31. long_running_task
  32. done
  33.  
  34. /**
  35. * Asynchronously execute/include a PHP file. Does not record the output of the file anywhere.
  36. * Relies on the PHP_PATH config constant.
  37. *
  38. * @param string $filename file to execute
  39. * @param string $options (optional) arguments to pass to file via the command line
  40. */
  41. function asyncInclude($filename, $options = '') {
  42. exec(PHP_PATH . " -f {$filename} {$options} >> /dev/null &");
  43. }
Add Comment
Please, Sign In to add comment