sc.exe and c# System.Diagnostic problems

Y

yqyq22

Hi, i have created a service with sc.exe in this manner:

sc config JBoss-Custom binpath= "C:\jboss-test.exe D:\jboss421GA\bin
\run.bat"
the service is running with local system account...
then i wrote in this: (jboss-test.exe)

Process jbosstest = new Process();
jbosstest.StartInfo.FileName = @" c:\windows
\system32\cmd.exe";
jbosstest.StartInfo.UseShellExecute = false;
jbosstest.StartInfo.RedirectStandardOutput = false;
jbosstest.StartInfo.RedirectStandardInput = false;
jbosstest.StartInfo.RedirectStandardError = false;
jbosstest.Start();
jbosstest.WaitForExit();
jbosstest.Close();
jbosstest.Dispose();
catch (IOException e)
{

Console.WriteLine(e.StackTrace);
}

I don't understand if the problem is on my code or how i have create a
service with sc.exe..
someone could help me?
thanks a lot in advance
 
Y

yqyq22

Hi, i have created a service with sc.exe in this manner:

sc config JBoss-Custom binpath= "C:\jboss-test.exe D:\jboss421GA\bin
\run.bat"
the service is running with local system account...
then i wrote in this: (jboss-test.exe)

 Process jbosstest = new Process();
                jbosstest.StartInfo.FileName = @" c:\windows
\system32\cmd.exe";
                jbosstest.StartInfo.UseShellExecute = false;
                jbosstest.StartInfo.RedirectStandardOutput = false;
                jbosstest.StartInfo.RedirectStandardInput= false;
                jbosstest.StartInfo.RedirectStandardError= false;
                jbosstest.Start();
                jbosstest.WaitForExit();
                jbosstest.Close();
                jbosstest.Dispose();
                 catch (IOException e)
                {

                       Console.WriteLine(e.StackTrace);
                }

I don't understand if the problem is on my code or how i have create a
service with sc.exe..
someone could help me?
thanks a lot in advance

i forgot.. the error is : error 1053:the service did not respond to
the start or control request a tinely fashion
 
J

Jeroen Mostert

yqyq22 said:
Hi, i have created a service with sc.exe in this manner:

sc config JBoss-Custom binpath= "C:\jboss-test.exe D:\jboss421GA\bin
\run.bat"

First of all, is jboss-test.exe actually written as a service? That is, does
it contain a class deriving from ServiceBase, and is ServiceBase.Run()
called in the main method? You cannot simply run any executable as a service.
the service is running with local system account...
then i wrote in this: (jboss-test.exe)

Process jbosstest = new Process();
jbosstest.StartInfo.FileName = @" c:\windows
\system32\cmd.exe";
jbosstest.StartInfo.UseShellExecute = false;
jbosstest.StartInfo.RedirectStandardOutput = false;
jbosstest.StartInfo.RedirectStandardInput = false;
jbosstest.StartInfo.RedirectStandardError = false;
jbosstest.Start();
jbosstest.WaitForExit();
jbosstest.Close();
jbosstest.Dispose();

You're trying to start an interactive command prompt here. The problem is
that services can't interact with the user (except indirectly). Your command
prompt is waiting for input that never comes.

If you just want to test if something's working, try writing to a file or
doing something else that doesn't require interaction. If you really want
interaction, don't do it from a service.
 
J

Jeroen Mostert

Jeroen said:
First of all, is jboss-test.exe actually written as a service?

Well, I obviously need more tea (I'm remedying that right now). I wasn't
even paying attention to your command line.

It looks as if you're trying to launch a batch file from a service, which
makes very little sense. Are you sure you're not looking for a scheduled
task instead?
 
Y

yqyq22

Well, I obviously need more tea (I'm remedying that right now). I wasn't
even paying attention to your command line.

It looks as if you're trying to launch a batch file from a service, which
makes very little sense. Are you sure you're not looking for a scheduled
task instead?

ok, thanks for your reply... yes.. i'm trying to launch a .bat.. the
jboss-test.exe is a normal console application ( I used sc.exe because
is a tool that is able to create a native windows service..( i know
that is possible to create a windows service via c#). I don't want use
a scheduled task..
thanks a lot
 
J

jmostert

ok, thanks for your reply... yes.. i'm trying to launch a .bat.. the
jboss-test.exe is a normal console application ( I used sc.exe because
is a tool that is able to create a native windows service..( i know
that is possible to create a windows service via c#). I don't want use
a scheduled task..

Please note that sc.exe does not create services as such, it merely
registers and installs them. You cannot launch arbitrary console
applications as services, they need to be specially written *as*
services. If not, you get exactly the behavior you describe: the
"service" can't start because it's not a service at all. There are
programs that are capable of wrapping regular console applications as
services if you need this, but writing an actual service from the
start is a better idea. Create a new "Windows Service" project from
Visual Studio for this, not a console application.

Using a service to launch a batch file is possible, but note that
regardless of what you do, you will never "see" anything happening for
the reason I described elsethread, nor will the user be able to supply
any input. For this reason it's dubious at best, and it needs solid
error recovery or there's a very real chance your service will hang
and not do anything at all any more -- the lack of interactive output
also makes diagnosing errors difficult. Last but certainly not least,
services typically run with elevated privileges and executing
atrbitrary batch files could pose a security risk (which may or may
not matter in your particular situation).
 
Y

yqyq22

Please note that sc.exe does not create services as such, it merely
registers and installs them. You cannot launch arbitrary console
applications as services, they need to be specially written *as*
services. If not, you get exactly the behavior you describe: the
"service" can't start because it's not a service at all. There are
programs that are capable of wrapping regular console applications as
services if you need this, but writing an actual service from the
start is a better idea. Create a new "Windows Service" project from
Visual Studio for this, not a console application.

Using a service to launch a batch file is possible, but note that
regardless of what you do, you will never "see" anything happening for
the reason I described elsethread, nor will the user be able to supply
any input. For this reason it's dubious at best, and it needs solid
error recovery or there's a very real chance your service will hang
and not do anything at all any more -- the lack of interactive output
also makes diagnosing errors difficult. Last but certainly not least,
services typically run with elevated privileges and executing
atrbitrary batch files could pose a security risk (which may or may
not matter in your particular situation).

ok, now it's clear...
thanks
 
Top