5

Is there something like this for Windows?

exec("nohup /usr/bin/php -f sleep.php > /dev/null 2>&1 &");
Ry-
  • 218,210
  • 55
  • 464
  • 476
Marcin
  • 5,469
  • 15
  • 55
  • 69
  • http://serverfault.com/questions/39451/there-is-any-way-to-run-processes-in-the-background-in-windows-nohup-equivalent – ajreal Nov 16 '10 at 18:27

2 Answers2

19

It's not that hard (albeit with some minor differences)... You just need to use the WScript.Shell COM object:

$shell = new COM("WScript.Shell");
$shell->run($command, 0, false);

That's it...

ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • What's the exception? Try wrapping it in `try { $shell... } catch (Exception $e) { var_dump($e->getMessage(), $e->getCode()); }` to see what's causing it... – ircmaxell Nov 16 '10 at 18:39
  • What's the error code? And what's the full error? And can you post the full code of the code that's generating the error? – ircmaxell Nov 16 '10 at 18:48
  • ok your method works except that $shell->release(); throwing an exception – Marcin Nov 16 '10 at 18:59
  • My command's I/O redirection `> somefile.txt` was not working until I added `%comspec% /c ` to the beginning of the command. – Kevin Somers-Higgins Feb 28 '14 at 19:43
  • Great answer, took me a while to find it, COM needs to be installed: http://php.net/manual/en/com.installation.php – user1620090 Apr 27 '16 at 10:57
  • This is the only answer that worked for me (`pclose`, `start`, etc didn't). Also worth mentioning is the second parameter. 0 = hidden, 1 = activate and display, 2 = activate and minimize, etc.. Full list is here: http://ss64.com/vb/run.html – supersan Dec 12 '16 at 04:08
2

By default, the Windows command start does not wait for the child process. You may want the /b switch to avoid creating a Command Prompt window.

exec("start /b c:\\php\\php.exe -f sleep.php");
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    Not completely true. According to [the docs on `start`](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/start.mspx?mfr=true): it only does not wait if you are launching a GUI process. If you are launching a console process it does wait (at least that's what it says for XP)... – ircmaxell Nov 16 '10 at 18:31