简体   繁体   中英

Sleep incorrectly affecting calling PHP-script

I am trying to delay executing of script2.php by 10 seconds, but not the entire script1.php. When I place a sleep(10) in script2.php at the top, unfortunately script1 is also delayed as proven by the output on the bottom.

<?php
{
    echo date("H:i:s");
    exec("nohup php script2.php $id $host '$status' '$nagios_host' &> /dev/null &");
    echo date("H:i:s");
    echo("nohup script2.php $id $host '$status' '$nagios_host' &> /dev/null &");
    echo date("H:i:s");
    echo $id." - ".$host." - ".$status." - ".$nagios_host."\n";
}       
?>  

This is the output of script1.php:

11:09:35
nohup: redirecting stderr to stdout
11:09:45
nohup php script2.php 1341234 blabla.com 'blabla alert => CRITICAL' 'nagios01.blabla.com' &> /dev/null &
11:09:45
1341234 - blabla.com - blabla alert => CRITICAL - nagios01.blabla.com       

I don't see anything wrong with output, you delay execution by 10 seconds, you get it in result. Don't expect script2.php to execute later than first script, because PHP will wait for exec function to end, before continuing program

Also related to this (and have same answer): How to execute shell commands synchronously in PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM