How can I register a program to auto-relaunch if it crashes or is terminated?


A customer wanted a program that auto-relaunched itself if it crashes, or even is explicitly terminated by the user. They were wondering if there was a way to register such a program with the system. Right now, they have a second program that monitors the first, and if the first program crashes, then the second program relaunches it.

Well, if the program is a Windows service, then they can configure the recovery policy, either programmatically or via the user interface (on the Recovery tab).

But for regular interactive applications (which is what this program was), there is no such automatic functionality. And since they wanted to make the program auto-restart even if the user explicitly terminates it, I suspect the purpose of the program is not so much to assist their users but to monitor (and possibly harrass) them.

At the end of the day, all you can do is build taller and taller walls, and the hostile users will buy taller and taller ladders. In this case, the user could terminate the second program, thereby taking out the monitor, and thus allowing them to terminate the main program with impunity. Or they could suspend the main program, so that it doesn't terminate but doesn't do anything useful either. If you add a heartbeat function, the users could attach a debugger to the program and patch out the code that implements the undesirable behavior and leaves just the heartbeat.

At this point, it's a matter of how hostile a user you want to defend yourself against.

Comments (25)
  1. Killer{R} says:

    isnt RegisterApplicationRestart() designed for that?

    1. SimonRev says:

      Close, but if the application crashes the user is presented with a dialog and can elect not to restart the process. Also, it won’t restart if the user explicitly terminates the process.

  2. Karellen says:

    Or you could create two programs, maybe calling them “Robin Hood” and “Friar Tuck”, which monitor each other and restart/resume their counterpart as necessary.[0]

    Note that the originals were PoC hacks written, as a last resort, to force a vendor to patch a privilege-escalation vulnerability they’d sat on for months. OTOH, if that’s how you treat your regular users, I hope life is unfair to you right back.

    [0] http://www.catb.org/jargon/html/meaning-of-hack.html

  3. nerdile says:

    I wonder how Visual Studio manages to re-open itself after it crashes without asking. It’s particularly annoying when you tell it to Exit, it crashes while exiting, and then promptly restarts.

    1. Joshua says:

      I’m pretty sure that one is a top-level exception handler.

  4. BOFH says:

    I made an image with Windows XP Embedded for an automotive computer years ago, back in the SP1 days, and as I recall, you could register your own application as the shell instead of explorer.exe.
    This would among other things make Windows automatically start a new instance of your program in case the program ever crashed or was stopped in some way.
    That would be one way to do it I guess, but I suppose it qualifies as the ultimate example of a global solution for a local problem. :)

    It was so long ago, but looking at the list in autoruns.exe, I think it was related to the values in this key in the Registry:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

  5. The MAZZTer says:

    Of course, the real way to do this is to run the application in question under an account they don’t have permission to kill processes in. Easiest way to do this is make it a service and make the user a non-administrator.

    If the service needs user UI components, a separate app running user the user’s account can do this, just make sure the service works properly even if the app is killed .

  6. Richard says:

    Swap the shell.
    Pretty much the only case where such behaviour is legitimate is a single-purpose kiosk.

    In which case, swap the shell so it only does that single purpose.

    And if this isn’t a single-purpose kiosk, you’re being user-hostile.

  7. Kirby FC says:

    In Windows 10, if you go into Task Manager and terminate Cortana, it immediately restarts.

    So, the functionality that the customer is asking about exists and, in fact , is built into Windows 10.

    1. GL says:

      And if you kill Explorer, it restarts. Perhaps the developer should swap the shell.

    2. Darran Rowe says:

      The Cortana process (SearchUI.exe) is probably protected by the Windows Search service or one of the other services.
      So the functionality is basically a Microsoft rolling their own situation like was mentioned in the post.

      1. GL says:

        But since it’s protected by a service, the user can disable the service. And if a third-party vendor wants a process to be protected, they can do that with a service.

        1. Darran Rowe says:

          I was only responding to someone mentioning that the Cortana process restarts after being killed.

  8. DWalker07 says:

    The POINT of the original post is “this is a bad idea”. Nice to see that many people are contributing ways to do what Raymond says is a bad idea. :-)

    1. 640k says:

      A large part of the stories here explains how windows allows you to shoot yourself in the foot. I suppose it’s raymond’s way, playing devil’s advocate.

  9. Emre says:

    As an hostile user, assuming my user role on the machine allows it, I would immediately revoke the Read&Execute permission from main executable file of the offending application and kill it afterwards.

    1. Karellen says:

      Because you can’t delete an executable while it’s in use by the system, but you can change its permissions?

      Oooh, clever. That’s one of those interesting cases where you might be aware of those two facts independently, but not think to put them together in the context of this particular problem. Especially while you’re under the pressure of trying to fight the damn thing. I’ll file that one away for later use…

  10. Dave says:

    There are plenty of programmers in places like Russia who would be happy to implement this functionality for you for a few BTC. They can even have it auto-install itself on other people’s machines for you, so that others can enjoy its benefits as well.

    1. xcomcmdr says:

      Ha !

    2. GL says:

      Man, this is regionism!

      1. Joshua says:

        Is pointing out the difference in cost of living regionalism?

        There are plenty of programmers in India for the same BTC. There are plenty of programmers in the US who will do the same for a lot of BTC because the cost of living is higher and the risk is higher.

        1. GL says:

          Well, the fact is that I avoid using such terms because “白左” are way too sensitive to this, and that I do think sometime such saying hurts, but still I just take it (the situation such as this one) as a joke. In the meantime, I enjoy pretending to be over sensitive, i.e., I enjoy pretending to be a “白左” — it’s kind of sarcasm. I assume you would understand “白左” cuz the jargon has been reported by some media in the US haha.

          1. @GL Hmm, seems 白左 is a synonym for SJW (someone preaching about this or that being the problem and are unaware they are also part of the problem ).

  11. Just found this https://www.reddit.com/r/windows/comments/4td55w/using_task_scheduler_to_restart_a_program_if_it/

    Rather clever and I have not tried it. But the idea is that you use Task Scheduler to monitor the application error log and then restart the crashed application.

    But per the question in that url my answer would be to fix or remove the plugin that causes the crash instead of restarting the app.

    Crash loops are nasty, and you really do not want a situation where an app crashes is restarted and crashes again.
    Windows services let you set a limit to how many times a service should be restarted, not sure you can do that with this Task Scheduler trick though.

    1. Ben says:

      Task Scheduler also has a RestartInterval and RestartCount option which you can use to restart a task which fails for any reason. I’m pretty sure that qualifies as “a way to register such a program with the system” ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa383518(v=vs.85).aspx )

Comments are closed.

Skip to main content