First of all, I would like to point out that this is NOT a duplicate to this or this and other related questions. I am asking how to get the output as well and my question is mostly related to that.
I need to run a bash script from PHP and get its output in PHP. However, doing:
echo shell_exec('script');
dies out on the 60th second due to Apache's FcgidIOTimeout. If I use the following to run it in the background, I won't be able to get the output:
shell_exec('<myscript> > /dev/null 2>/dev/null &');
I cannot:
- Modify the bash script;
- The code and all the functionality needs to be in a single file (unless I use a temp file);
- If I were to write the output of the
shell_exec()function to a temp file, I would also need to find a way to verify whether the process has finished; - Using a database is an overkill which I cannot afford;
My current and only idea is to create a tmp file using PHP and write the output to it, then using top -c | grep <myscript> (with a refresh button) and if it returns true then it is still ongoing. However, I suspect that would not be practical and efficient in most of the time.
It is important that I use a temp file and not actually creating a "permanent" file to write to.
top -c | grepcould you usepgrep? You still have the same problem but this question shows how to check if the process is still running from phppgrepis reliable for finding processes with the right name (more so thantop+grepI'd wager), but whether it's your same instance of the script, it probably is if it has the same name and PID, but there's always a chance you could get unlucky.