Fog Creek Software
g
Discussion Board




Windows Console question

Unix shells allows to run apps in background.

  > doit &

I know how start Windows app detached from console using CreateProcess but it would be nice to find such feature in Win Console. Any idea ?

Michael Popov
Tuesday, January 13, 2004

Use the start command.

Almost Anonymous
Tuesday, January 13, 2004

Thanks.

It almost works :) The only problem is the process is not detached from console and it dies when console is closed.

I need kind of "service" without services' complexity.

Michael Popov
Tuesday, January 13, 2004

When I use start on a console application a new console is opened that is independent of the first console (ie I can close it).  Same is true of native Windows applications (except no console is created).  What version of Windows are you using?

Almost Anonymous
Tuesday, January 13, 2004

There is a tool (I forget it's name) that will let you turn any Windows application into a service.  You just run it from the command line and it installs the process as a service.

Almost Anonymous
Tuesday, January 13, 2004

Cygwin's Cygrunsrv allows arbitrary processes to be started as a service.

http://www.cygwin.com/packages/cygrunsrv/

Michael Koziarski
Tuesday, January 13, 2004

also srvany or some similar name which is part of the windows resource kit somewhere. i plan to use it to run cruisecontrol.net sometime soon.

mb
Tuesday, January 13, 2004

It's both a plus and a minus that Microsoft decided to force programs to specifically cope with the idea of becoming a service.

Using SRVANY or SRVANYW or similar programs does mitigate, but there can be problems (such as the fact that any console output is lost, as opposed to having a Unix daemon redirect its I/O while being put into the background).

Brad Wilson (dotnetguy.techieswithcats.com)
Tuesday, January 13, 2004

Well, as usual it is simpler to write it by myself :)
CreateProcess allows to start a new process detached from any console.The process will not survive user's log off but at this stage that's good enough.

Michael Popov
Tuesday, January 13, 2004

The Start command seems to work for me. I can kill the original console, and the started application keeps running. I use Windows XP SP1.

Raj Chaudhuri
Wednesday, January 14, 2004

Yep, but if you close the spawned console, the process dies off. The OP is asking how processes can be run similar to a Windows service or the unix '&' command.

Rhys Keepence
Wednesday, January 14, 2004

OS/2 has a "detach" command which does this.  When I found I needed this during a port from OS/2 to NT I wrote my own in C.
Basic sequence is:
- If any of stdin/stdout/stderr are associated with the current console (_isatty), redirect them to the NUL device.
- Call CreateProcess to execute "cmd.exe /c " followed by the command line entered by the user.  Use SW_HIDE to hide the new console window.
- If successful, display a message with the process ID of the new process.
Code available to anyone who wants it.

Joe
Wednesday, January 14, 2004

If you schedule a job to run your task as a different user (still with the same rights), you shouldn't see the console windown.  We do this all of the time because we have to run scripts that we do not want the users to see

Matt Watson
Wednesday, January 14, 2004

*  Recent Topics

*  Fog Creek Home