Search code examples
phpexecstdoutstderrnohup

PHP exec() nohup with redirect


I am trying to execute a command with exec() and redirecting stdout and stderr to a file.

exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");

It will create the file but it will not print anything to it.

If I run the command in a terminal everything outputs without a problem.


Solution

  • Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:

    exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");

    Thanks.