Nohup on Windows, Exec Without Waiting for Finish

nohup on windows, exec without waiting for finish

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...

PHP Exec: Without Waiting, Without Discarding the Output, Without nohup

Add an ampersand to the end of the command, so:

exec('dosomething > saveit.txt &');

PHP exec whit nohup works on commandline but not on http calls

Create a file on your server called phpinfo.php.

This file will only have one line of code:

<?php echo phpinfo() ?>

If you're uncertain, there is a full tutorial here:
http://www.inmotionhosting.com/support/website/php/create-phpinfo-page-to-see-php-settings
A lot of hosting companies may disable exec() and you may be unable to change your php.ini file.

If so, your best bet would be to start with the php mail function.
http://php.net/manual/en/function.mail.php

Additional Information about php.ini:
Your original statement that it works from the command line but not from a call through the browser makes, to me, the php.ini directives the most likely culprit. When PHP is executed through a webserver, the execution of any script is dictated by these directives. The best reference is the PHP documentation: http://php.net/manual/en/ini.core.php. If you run the phpinfo.php from a browser on your server, you're looking for a section "Core" and within that the disable_functions table row.

On my development server, there are a lot of functions that are disabled; including exec() and fork().

If you can edit your php.ini file, you can remove the exec function from the line

disable_functions = 

and restart your webserver; although I would discourage it. That function is disabled for valid security reasons.

Is there a way to use shell_exec without waiting for the command to complete?

How about adding.

"> /dev/null 2>/dev/null &"

shell_exec('php measurePerformance.php 47 844 email@yahoo.com > /dev/null 2>/dev/null &');

Note this also gets rid of the stdio and stderr.

PHP on a windows machine; Start process in background

Will this function from the PHP Manual help?

function runAsynchronously($path,$arguments) {
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut("temp.lnk");
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$oExec = $WshShell->Run("temp.lnk", 7, false);
unset($WshShell,$oShellLink,$oExec);
unlink("temp.lnk");
}

Run executable in powershell without waiting for return

If it's acceptable to terminate the server when the PowerShell session exits, use a background job:

In PowerShell (Core) 7+

ng server &

In Windows PowerShell, explicit use of Start-Job is required:

Start-Job { ng server }

Both commands return a job-information object, which you can either save in a variable ($jb = ...) or discard ($null = ...)

If the server process produces output you'd like to monitor, you can use the Receive-Job cmdlet.

See the conceptual about_Jobs topic for more information.


If the server must continue to run even after the launching PowerShell session exits, use the Start-Process cmdlet, which on Windows launches an independent process in a new console window (by default); use the -WindowStyle parameter to control the visibility / state of that window:

Start-Process ng server # short for: Start-Process -FilePath ng -ArgumentList server

Note: On Unix-like platforms, where Start-Process doesn't support creating independent new terminal windows, you must additionally use nohup - see this answer.

Is there a way to not wait for a system() command to finish? (in c)

system() simply passes its argument to the shell (on Unix-like systems, usually /bin/sh).

Try this:

int a = system("python -m plotter &");

Of course the value returned by system() won't be the exit status of the python script, since it won't have finished yet.

This is likely to work only on Unix-like systems (probably including MacOS); in particular, it probably won't work on MS Windows, unless you're running under Cygwin.

On Windows, system() probably invokes cmd.exe, which doesn't accept commands with the same syntax used on Unix-like systems. But the Windows start command should do the job:

int a = system("start python -m plotter");

As long as you're writing Windows-specific code (start won't work on Unix unless you happen to have a start command in your $PATH), you might consider using some lower-level Windows feature, perhaps by calling StartProcess. That's more complicated, but it's likely to give you more control over how the process executes. On the other hand, if system() meets your requirements, you might as well use it.



Related Topics



Leave a reply



Submit