0

I call a python script from PHP but i run script in background because i don't want to wait after script to finish, so i use > /dev/null 2>/dev/null & with shell_exec.
This is the code where i call:

shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py > /dev/null 2>/dev/null &");

in python script i have a simple write file:

fileName = "temp.txt"
target = open(fileName, 'w')

for x in range(1, 59):
    target.write("test" + str(x) + "
")

target.close()

When i call script from PHP with shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py");
it's working but webpage it's waiting after script to finish and i don't want that, so i called with > /dev/null 2>/dev/null & but now, the code doesn't run.
Any ideas why code doesn't working when i want to run in background?
Thanks!

LE:

Changed /dev/null/ with nul and still no working.
Now i call shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py > NUL 2> NUL &"); and it's working but page still wating after script to finish.

VladutZzZ
  • 680
  • 1
  • 8
  • 27
  • possible duplicate of [/dev/null in Windows?](http://stackoverflow.com/questions/313111/dev-null-in-windows) – Sverri M. Olsen Aug 20 '15 at 11:19
  • No, still not working with `nul` and not `/dev/null` – VladutZzZ Aug 20 '15 at 11:24
  • It is `NUL`, not `nul`. `/dev/null` is a *Linux* bit bucket, and `C:\\...` is a Windows path... you cannot mix and match Linux and Windows paths like this. – Sverri M. Olsen Aug 20 '15 at 11:55
  • I tried with both options, still no work... It's working but it's waiting after script to finish. – VladutZzZ Aug 20 '15 at 11:56
  • Well, the entire `> NUL 2> NUL &` is a Linux command (with the Win bit bucket). As I said, you *cannot mix and match Linux and Windows stuff* like this. Outputting stuff to NUL would just be `... > NUL`. According to [this](http://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background), background processes are run using `START /B [PATH_TO_EXEC]`. Do some research before asking more questions. – Sverri M. Olsen Aug 20 '15 at 13:08
  • @SverriM.Olsen you are really near the problem. You don't understand the problem is not start without CMD, i dont give a **** if a cmd jumps on my screen. Problem is `my webpage don't need to wait after script`, it's 100% clear for you which is the problem? And the problem is here about PHP, not how to start a program without cmd (your link). – VladutZzZ Aug 20 '15 at 13:35

1 Answers1

1

The answer is:
pclose(popen("start /B ". $cmd, "r"));
You can start a program without cmd and PHP will not wait after program to finish.

VladutZzZ
  • 680
  • 1
  • 8
  • 27