Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Running Processes from a Service

Posted on 1999-07-29
Medium Priority
Last Modified: 2010-04-02
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!!
Question by:jernhung
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 86

Expert Comment

ID: 1201347
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),


Author Comment

ID: 1201348
  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);      

LVL 86

Expert Comment

ID: 1201349
>>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...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

zbang earned 140 total points
ID: 1201350
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.

LVL 86

Expert Comment

ID: 1201351
>>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...
LVL 86

Expert Comment

ID: 1201352

Author Comment

ID: 1201353
  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.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question