maggotknx
maggotknx

Reputation: 37

Start independent process (keep alive after program exit)

I'm developing a program to update all the other programs developed by my department.

I want this program to be capable of updating itself, to do so I need to replace it's running .exe with the one I get from my FTP server.

What I tried to do is creating a batch file that kills the process of my application, copies the "new" executable to my application's path and then start it.

The problem is that when the batch file kills the process, it doesn't run the other commands, it is being killed "together" with the application.

Is it possible to run this process in a way that it doesn't depend on my application?

This is the batch file code:

@ECHO OFF
@BREAK OFF
taskkill /f /t /im "MyApplication.exe"
rmdir /S /Q "%APPDATA%\DepartmentTools\WorkplaceUpdate\"
mkdir "%APPDATA%\DepartmentTools\WorkplaceUpdate\"
copy "%APPDATA%\DepartmentTools\MyApplication.exe" "%APPDATA%\DepartmentTools\WorkplaceUpdate\MyApplication.exe"
"%APPDATA%\DepartmentTools\WorkplaceUpdate\MyApplication.exe"

Upvotes: 0

Views: 316

Answers (1)

Eugene Podskal
Eugene Podskal

Reputation: 10401

You are passing the /t flag, that

/t Terminates the specified process and any child processes started by it.

So we have

  1. Application starts.
  2. Application starts cmd.exe update script.
  3. cmd.exe terminates application and its child processes, thus terminating itself.

Assuming that the application itself knows when it updates itself, it can (and should) cleanly terminate its child processes before killing itself. When updater and "updatee" applications are different applications, it obviously won't be an issue.

So just don't pass that flag or use it only when updating other applications, and everything should work.

P.S.: While self-updating the application in such a way during its run is not exactly unreasonable and is at least very simple, it may be dangerous to use such approach for updating other application as you are force-terminating them, potentially leading to inconsistent/partially done results and other issues depending on how robust those other applications are.

Upvotes: 1

Related Questions