Using exec while redirecting the output to a text file seems to lock said file
On a WAMP configuration, I am executing a shell command with exec
and redirecting the output with >> log.txt
. The commando is called many times in the script. The first time, the file is created, and the first output is written on it, but it seems the file stream is not closed (I can't delete the file, and Windows tells me that the file is opened by somebody else), and as such the script try to continue loading but do nothing else, and is still try after the 5 minutes limit I set for this script.
Am I doing something wrong? If yes why? Otherwise where does the problem lies?
EDIT: I've confirmed that by commenting the code line that it is the source of my problem. Furthermore, although I interrupted the script, the file remained locked to any change (edit/move/delete).
EDIT 2: Using pclose(popen("start /B ". $cmd, "r"))
instead, as suggested in this comment doesn't seems to bring anything, regardless If I use >> log.txt
or not.
EDIT 3: After a forced unwanted reboot of the server, the issue isn't there anymore.
I've talked with some colleague which encountered a similar problem with their application, and they said that rebooti开发者_运维知识库ng their server always "solved" the problem. But I don't call that a solution if the problem comes again sooner or later. So it seems that the "silent update" scenario is the more possible.
In the meantime the server will be migrated to a machine with a newer OS (Windows 2008 Server). I'll wait and see if the problem comes again, but if possible I would like to takes measures so I don't have to deal with it in the future.
why dont you exec($cmd, $output, $result) ? this will return the output in the $output variable. you can then do a file_put_contents($file, implode("\r\n", $output), FILE_APPEND)
this will let you append the output of the command right into the log file in pure php way
精彩评论