• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Running Processes from a Service

Hi all,
  I'm trying to write a service in Win NT that acts as a auto-starter which starts other processes. I will start the process by calling CreateProcess() in ServiceMain(), which works fine.
  But it's not so good when the created process crashes. There seems to be no console associated with the created process (it's a console app.) and it sort of lingers as a runaway process, taking up memory space. I can't kill it, even with the task manager, and the only way I can remove it is to restart.
  I've written my service such that it will restart the process if it comes down for any reason, but this 'lingering process' problem would not allow me to do that. My process therefore fails and would not recover automatically.
  Making the process crash-proof is the ultimate solution of course (let's just say I'm in the midst of sorting THAT out), but I'm more interested right now in the mechanics of creating processes inside services, because I need to behaviour of these processes to be at least manageable.


PS: I'm apologise for offering a bit low on points - it's all I have at the moment!!
  • 4
  • 2
1 Solution
To get rid of the 'lingering' process, simply terminate it from your service using 'TerminateProcess()'. In order to do this, you'll have to store the process handle (returned in the PROCESS_INFO struct filled in by 'CreateProcess()' and enable the SE_DEBUG_NAME privilege for your service. e.g.

   HANDLE           hToken;

   if   (   !OpenProcessToken   (   GetCurrentProcess   (),
        )   return  (   FALSE);

   tp.PrivilegeCount    =   1;
   LookupPrivilegeValue (   NULL,
                            &tp.Privileges  [   0].Luid

   tp.Privileges    [   0].Attributes   =       bEnable
                                            ?   SE_PRIVILEGE_ENABLED
                                            :   0;

   AdjustTokenPrivileges    (   hToken,
                                sizeof  (   tp),

jernhungAuthor Commented:
  Erm.. that's kinda difficult, because I don't want to explicitly terminate my process when it's running. I want to restart it only when it fails or stops for any reason, but because it lingers, I have no way to tell if it's still active or if something bad has happened to it.
  I need to know therefore if it's possible for the process to terminate itself normally and yet not linger in memory.

Here the code I used to create my process, is there any special parameters that I need to set?

        int status = 0;
      STARTUPINFO stateInfo;
        PROCESS_INFORMATION processInfo;


        // Path name of process to run
      char* fullpath = "Executable.EXE";
      while (status == 0 && _pause == 0)
            // try to start process every 3 mins. if start failed and service not paused or stopped
            status = CreateProcess(
            if (status == 0)
                  _stream<<"Startup Failed"<<endl;
            Sleep(3000 * 60);      

>>I need to know therefore if it's possible for the process
>>to terminate itself normally and yet not linger in

Hmm, if the process crashes, it should be removed anyway...

My idea would be to have the child process send sth.' like a 'ping' to the service in order to let the service know that it is still alive...
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

The common technique for detecting dead or lingering processes is setting up a callback function to the parent process and calling that function every minute for example. If there is no callback activity from the child process for about twice the interval between the callbacks (2 minutes) the parent process should assume the child is dead and terminate it.

To address your problem with the console application: In order to relieve yourself from the headache in creating consoles for console applications, use ShellExecute() instead of CreateProcess. Basically, services are not allowed to interact with the desktop.

Another word about lingering processes - make your service run as a user which you have full control of (for example, YOUR user, or any user but the system account when you are the Administrator)
That way you will be able to terminate the process from Task Manager.

>>The common technique for detecting dead or lingering
>>processes is setting up a callback function to the parent
>>process and calling that function every minute for

zbang - err, isn't that what I suggested? Please read the whole thread before throwing in answers that are already being discussed...
jernhungAuthor Commented:
  Apologies for awarding the points to someone else. I know zbang did repeat what you have said before, but to be fair, he/she also pointed out some other useful advice in addition to that.
  Nevertheless thanks for your help. If I could split the points amongst the 2 of you, I would.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now