开发者

PsExec does not detect my c# console app is finished

I use PsExec for executing several command line programs, but when I call my own build C# (.net 4.0) console app, PsExec does hang, and will not finish...

When calling other console apps, PsExec will return output like this:

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinte开发者_如何学Crnals - www.sysinternals.com
Connecting to localhost...
Starting PsExec service on localhost...
Connecting with PsExec service on localhost...
Starting [MyExe] on localhost...
[myExe] exited on localhost with error code 0.

But on my own C# console app only show this output:

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

My console app has some logging itself, and it executes fine, and exits with exit code 0 in about 2 seconds, but after 40 seconds, PsExec has still no output.

On the target machine the PsExecSVR is still installed, and running.

I've tried to exit the console app with

Environment.Exit();

And also removed this line, and let the console app exit on it's own (by letting void main to end on its own). But still no luck on PsExec.


I've had this problem in my automated builds and deployment scripts. For an unknown reason, psexec can get stuck when it has some standard output to "capture" on the remote host. And this doesn't happen every time...

To work around this avoid having your remote executables output on the console, either produce no output at all (just an exit code) or redirect the standard output to a file.

You may have to call cmd.exe to redirect the standard output

cmd /c "yourprogram.exe" > yourlog.txt

Then open the file remotely (from a system share for example \\server\d$\folder\yourlog.txt) and dump it your local console.

Hope this helps.


When you execute the console from PsExec, check whether the process actually does finish. My suspicion is that it has either hung waiting on input (for example, is it trying to read from stdin, and blocking?), or has thrown an exception and is currently (for example) in a "oops: {0} happened; press any key to exit".

Classic reasons for an exe to fail when executed like this:

  • the working directory is not what you expected it to be (i.e. it is in the PsExec folder, not your app's folder)
  • the user (who is no longer the interactive user - it could even be a guest account) lacks a necessary permission, or something ins't installed for that user
  • it might even be trying to show you the UAC dialog somewhere


Here is the code of the program that I get stuck with:

// Holds the Log4Net logger.
private static readonly Logger _Log = Logger.GetLogger(typeof(Program));
internal static void Main(string[] args)
  {
     Environment.ExitCode = 0;
     var result = false;
     try
     {
        _Log.Debug(new string('=', 80));
        _Log.Debug("Starting");
        _Log.Debug("Location: {0}", Assembly.GetExecutingAssembly().Location);

        if (args.Length != 5)
        {
           throw new InvalidOperationException("Invalid number of arguments");
        }
        if (args[0] != "/install" && args[0] != "/uninstall")
        {
           throw new InvalidOperationException("First argument should be \"/install\" or \"/uninstall\"");
        }
        if (args[0] == "/install")
        {
           _Log.Debug("Installing...");
           _Log.Debug("DO NOTHING, BUT START AND STOP!!!");
           result = true;
           _Log.Debug("Installed: Result={0}", result);
        }
        else if (args[0] == "/uninstall")
        {
           _Log.Debug("Uninstalling...");
           _Log.Debug("DO NOTHING, BUT START AND STOP!!!");
        }
     }
     catch (Exception e)
     {
        _Log.Error("ERROR: " + e.Message + "\r\n" + e.StackTrace);
        Environment.ExitCode = -1;
     }
     finally
     {
        _Log.Debug("Stopping...");
        if (result)
        {
           _Log.Debug("Setting ExitCode to 0.");
           Environment.ExitCode = 0;
        }
        else
        {
           _Log.Debug("Setting ExitCode to -1.");
           Environment.ExitCode = -1;
        }
     }

     _Log.Dispose();

     GC.Collect();
     GC.WaitForPendingFinalizers();

     _Log.Debug("Exiting with exitcode: {0}", Environment.ExitCode);
     Environment.Exit(Environment.ExitCode);
  }

The first time I call the code, the PsExec works fine, the second time (3rd etc.) do not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜