4

In А.php is executed

foreach($allUsers as $cnt=>$arUser){
    shell_exec("nohup php /B.php $arUser > TestFile.log & echo $!");
}

in script B.php I write log

file_put_contents('Log.log', " action \r\n", FILE_APPEND | LOCK_EX); 

But part of the data is not logged. This is most likely due to the file locking by another process. How to log correctly without losing data when multi-threaded writing to a single file?

  • not really duplicate, not a full answer, but contains some helpful insight: https://stackoverflow.com/questions/7842511/safe-to-have-multiple-processes-writing-to-the-same-file-at-the-same-time-cent – Jan Myszkier Dec 17 '18 at 09:12
  • The writing to the file part looks correct, I think the issue might be with the shell_exec call. Try printing the output of that and see what happens - http://php.net/manual/en/function.shell-exec.php. Also there are usually problems with file locking when you use shared / network file systems (NFS, Gluster). If it's development and you are using VM to run the script, placing the Log.log file in the mounted directory might cause problems. But that's just a guess – Bogdans Dec 17 '18 at 09:45
  • You must to use ">>" instead of ">" on your command. – Gabriel Pereira Jan 25 '19 at 13:39

0 Answers0