Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Processes started by a Windows service never stop


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 7 - Last Post: Jul 24, 2015 3:59 PM Last Post By: George Wei
George Wei

Posts: 40
Registered: 9/18/05
Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 17, 2015 5:41 PM
Dear all,

I have a command line tool shellopen.exe compiled by Delphi 7, which is called by a Windows service timely to start other command line programs, the code is as follows:

program shellopen;
 
{$APPTYPE CONSOLE}
 
uses
  ShellExecutor in 'ShellExecutor.pas';
 
begin
  TShellExecutor.Execute;
end.


unit ShellExecutor;
 
interface
 
type
  TShellExecutor = class
  private
    class procedure WriteLog(Msg: string);
    class function GetErrorMsg(ErrCode: Integer): string;
  public
    class procedure Execute;
  end;
 
implementation
 
uses
  SysUtils, Windows, ShellAPI, StrUtils;
 
class procedure TShellExecutor.Execute;
var
  WholeOrder, Cmd, Params: string;
  Ps: Integer;
begin
    Ps := Pos(' ', CmdLine);
    if Ps <= 1 then Exit;
    WholeOrder := TrimLeft(Copy(CmdLine, Ps + 1, Length(CmdLine) - Ps));
    if WholeOrder = '' then Exit;
 
    if WholeOrder[1] = '"' then
    begin
      Ps := Pos('" ', WholeOrder);
      if Ps > 1 then Ps := Ps + 1;
    end else
      Ps := Pos(' ', WholeOrder);
    if Ps <= 1 then Exit;
    Cmd := Copy(WholeOrder, 1, Ps - 1);
    Params := Copy(WholeOrder, Ps + 1, Length(WholeOrder) - Ps);
    if Cmd = '' then Exit;
 
    Cmd := ExpandRelativePath(Cmd);
    ShellExecute(0, 'open', PChar(Cmd), PChar(Params), nil, SW_HIDE);
end;
 
end.


The problem is some programs started by shellopen.exe NEVER stop! But this does not occure if I run the Windows service program directly at command line (it still calls shellopen.exe timely to start other programs). I've tried to change the user of the Windows service to Administrator, it's useless.

So what's the problem?

Regards,
George
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 17, 2015 7:39 PM   in response to: George Wei in response to: George Wei
George wrote:

I have a command line tool shellopen.exe compiled by Delphi 7, which
is called by a Windows service timely to start other command line
programs

Is there a reason why the service is not simply using the Win32 CreateProcess()
function to run those commands directly?

The problem is some programs started by shellopen.exe NEVER stop!

Well, for starters, why are you using ShellExecute() in the first place?
What do the commands actually look like? If they are actual .exe files,
you really need to use CreateProcess(AsUser)() instead of ShellExecute().

But this does not occure if I run the Windows service program directly
at command line (it still calls shellopen.exe timely to start other
programs).

You cannot run a service program from the command line, it can only be run
by the SCM.

So what's the problem?

You tell us. What is the problem? Is it that ShellExecute() never exits?
Or that it does exit, but the command it launched does not exit (why should
it? What is the termination condition for it?)? Do the commands display
any GUIs that prompt the user for input before exiting? You need to provide
more details about your actual problem.

--
Remy Lebeau (TeamB)
George Wei

Posts: 40
Registered: 9/18/05
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2015 4:00 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Is there a reason why the service is not simply using the Win32 CreateProcess()
function to run those commands directly?

ShellExecute is simple, and it has been working well for several years until we met this problem recently.

Well, for starters, why are you using ShellExecute() in the first place?
What do the commands actually look like? If they are actual .exe files,
you really need to use CreateProcess(AsUser)() instead of ShellExecute().

The target program is a batch file as follows:

@echo off
 
set CLASSPATH=%CLASSPATH%;%ADMS_HOME%/lib/bflow.jar
set CLASSPATH=%CLASSPATH%;%ADMS_HOME%/templateflow
set CLASSPATH=%CLASSPATH%;%ADMS_HOME%/template
 
set __FLOW_PARAMS__=
 
:Loop
if [%1]==[] goto End
set __FLOW_PARAMS__=%__FLOW_PARAMS__% %1
shift
goto Loop
 
:End
 
%JAVA_HOME%\bin\java.exe  -Xms256m -Xmx1000m  -classpath "%CLASSPATH%" adms.flow.FlowStart %__FLOW_PARAMS__%


We use this batch file to start other Java processes. Because the environment (like CLASSPATH) may be changed, a batch file can be easy to modify.

And can you explain why you prefer CreateProcess to ShellExecute?

You tell us. What is the problem? Is it that ShellExecute() never exits?
Or that it does exit, but the command it launched does not exit (why should
it? What is the termination condition for it?)? Do the commands display
any GUIs that prompt the user for input before exiting? You need to provide
more details about your actual problem.

The issue is some Java processes do not quit after finished their work. We use log4j to record their output messages, the log shows they were finished, but the processes (and the batch processes) still exist in memory.

Remy Lebeau (TeamB) wrote:
George wrote:

I have a command line tool shellopen.exe compiled by Delphi 7, which
is called by a Windows service timely to start other command line
programs

Is there a reason why the service is not simply using the Win32 CreateProcess()
function to run those commands directly?

The problem is some programs started by shellopen.exe NEVER stop!

Well, for starters, why are you using ShellExecute() in the first place?
What do the commands actually look like? If they are actual .exe files,
you really need to use CreateProcess(AsUser)() instead of ShellExecute().

But this does not occure if I run the Windows service program directly
at command line (it still calls shellopen.exe timely to start other
programs).

You cannot run a service program from the command line, it can only be run
by the SCM.

So what's the problem?

You tell us. What is the problem? Is it that ShellExecute() never exits?
Or that it does exit, but the command it launched does not exit (why should
it? What is the termination condition for it?)? Do the commands display
any GUIs that prompt the user for input before exiting? You need to provide
more details about your actual problem.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2015 4:15 PM   in response to: George Wei in response to: George Wei
George wrote:

ShellExecute is simple, and it has been working well for several years
until we met this problem recently.

Doesn't mean it was the best choice to begin with.

The target program is a batch file as follows:
<snip>

But what is the actual command line you are using to run it with ShellExecute()?
What are the actual values of the Cmd and Params variables inside of TShellExecutor.Execute()?

The issue is some Java processes do not quit after finished their
work. We use log4j to record their output messages, the log shows
they were finished, but the processes (and the batch processes)
still exist in memory.

That has nothing to do with ShellExecute() itself, especially if it has already
exited before the Java processes have finished their work. I would suspect
a problem with your command line parameters, which is why I keep asking you
to show them.

--
Remy Lebeau (TeamB)
George Wei

Posts: 40
Registered: 9/18/05
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2015 8:19 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
This is a sample of Cmd and Params:

Cmd: "H:\sunstar\adms51\bin\flow.bat"
Params: -f adms.flow.ifp.batch.core.BPoBillForm -id 532967 -l H:\sunstar\adms51\log\20150723\532967.log -e H:\sunstar\adms51\log\20150723\532967_err.log > nul

flow.bat is the batch file I mentioned in the previous message I reply;
-f adms.flow.ifp.batch.core.BPoBillForm means to run java class adms.flow.ifp.batch.core.BPoBillForm;
-id 532967 is a parameter required for adms.flow.ifp.batch.core.BPoBillForm
-l H:\sunstar\adms51\log\20150723\532967.log means to write output messages to file H:\sunstar\adms51\log\20150723\532967.log;
-e H:\sunstar\adms51\log\20150723\532967_err.log means to write error messages to file H:\sunstar\adms51\log\20150723\532967_err.log;
nul means to redirect stdout & stderr to Windows' null device.

Remy Lebeau (TeamB) wrote:
George wrote:

ShellExecute is simple, and it has been working well for several years
until we met this problem recently.

Doesn't mean it was the best choice to begin with.

The target program is a batch file as follows:
<snip>

But what is the actual command line you are using to run it with ShellExecute()?
What are the actual values of the Cmd and Params variables inside of TShellExecutor.Execute()?

The issue is some Java processes do not quit after finished their
work. We use log4j to record their output messages, the log shows
they were finished, but the processes (and the batch processes)
still exist in memory.

That has nothing to do with ShellExecute() itself, especially if it has already
exited before the Java processes have finished their work. I would suspect
a problem with your command line parameters, which is why I keep asking you
to show them.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2015 9:52 AM   in response to: George Wei in response to: George Wei
George wrote:

Cmd: "H:\sunstar\adms51\bin\flow.bat"
Params: -f adms.flow.ifp.batch.core.BPoBillForm -id 532967 -l
H:\sunstar\adms51\log\20150723\532967.log -e
H:\sunstar\adms51\log\20150723\532967_err.log > nul

You cannot use '> nul' with ShellExecute(). Pipe operators are part of cmd.exe,
so you have to run cmd.exe directly in order to use piping:

%COMSPEC% /C "H:\sunstar\adms51\bin\flow.bat" -f adms.flow.ifp.batch.core.BPoBillForm 
-id 532967 -l H:\sunstar\adms51\log\20150723\532967.log -e H:\sunstar\adms51\log\20150723\532967_err.log 
<div class="jive-quote">nul</div>


You can use CreateProcess() to run such a command line:

var
  CmdExe, CmdLine: String;
  si: TStartupInfo;
  pi: TProcessInformation;
begin
  CmdExe := GetEnvironmentVariable('COMSPEC');
  CmdLine := AnsiQuotedStr(CmdExe, #34) + ' /C "H:\sunstar\adms51\bin\flow.bat" 
-f adms.flow.ifp.batch.core.BPoBillForm -id 532967 -l H:\sunstar\adms51\log\20150723\532967.log 
-e H:\sunstar\adms51\log\20150723\532967_err.log > nul';
  ZeroMemory(@si, sizeof(si);
  si.cb := sizeof(si);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_HIDE;
  if CreateProcess(nil, PChar(CmdLine), nil, nil, False, CREATE_NO_WINDOW, 
nil, nil, si, pi) then
  begin
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
  end;
end;


--
Remy Lebeau (TeamB)
George Wei

Posts: 40
Registered: 9/18/05
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2015 3:59 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
George wrote:

Cmd: "H:\sunstar\adms51\bin\flow.bat"
Params: -f adms.flow.ifp.batch.core.BPoBillForm -id 532967 -l
H:\sunstar\adms51\log\20150723\532967.log -e
H:\sunstar\adms51\log\20150723\532967_err.log > nul

You cannot use '> nul' with ShellExecute(). Pipe operators are part of cmd.exe,
so you have to run cmd.exe directly in order to use piping:

%COMSPEC% /C "H:\sunstar\adms51\bin\flow.bat" -f adms.flow.ifp.batch.core.BPoBillForm 
-id 532967 -l H:\sunstar\adms51\log\20150723\532967.log -e H:\sunstar\adms51\log\20150723\532967_err.log 
<div class="jive-quote">nul</div>

As I can see in the Task Manager, Windows automatically inserted "cmd.exe /C " before ""H:\sunstar\adms51\bin\flow.bat" ...".

You can use CreateProcess() to run such a command line:

var
  CmdExe, CmdLine: String;
  si: TStartupInfo;
  pi: TProcessInformation;
begin
  CmdExe := GetEnvironmentVariable('COMSPEC');
  CmdLine := AnsiQuotedStr(CmdExe, #34) + ' /C "H:\sunstar\adms51\bin\flow.bat" 
-f adms.flow.ifp.batch.core.BPoBillForm -id 532967 -l H:\sunstar\adms51\log\20150723\532967.log 
-e H:\sunstar\adms51\log\20150723\532967_err.log > nul';
  ZeroMemory(@si, sizeof(si);
  si.cb := sizeof(si);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_HIDE;
  if CreateProcess(nil, PChar(CmdLine), nil, nil, False, CREATE_NO_WINDOW, 
nil, nil, si, pi) then
  begin
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
  end;
end;

I'll try your code, thanks a lot.

--
Remy Lebeau (TeamB)
George Wei

Posts: 40
Registered: 9/18/05
Re: Processes started by a Windows service never stop  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2015 8:30 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Duplicated.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02