Author Topic: Automatic Application Termination or Pre/Post Build & Run Commands  (Read 1484 times)

AlexanderT

  • New Member
  • *
  • Posts: 37
It would be helpful to have a simple option in the IDE, such as "Terminate the running application before Build". This would solve a common workflow issue and remove the need to manually close the application before rebuilding or starting a new debugging session.

As a more advanced feature, Lazarus could also support configurable Pre-build, Post-build, Pre-run, and Post-run commands or scripts. This would allow developers to automate project-specific tasks such as terminating applications, copying files, generating resources, cleaning temporary files, signing binaries, or any other custom actions.

When a project is built directly from the IDE rather than through external build scripts, it would be useful to have these additional build customization options available as part of the IDE itself.

If such an option already exists, please let me know. I couldn't find anything similar in the IDE settings or the documentation.

Thaddy

  • Hero Member
  • *****
  • Posts: 19950
  • Pointless static linking author
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #1 on: July 26, 2026, 11:27:10 am »
You can already do that. kill<pid>, man kill, on windows, terminateprocess
« Last Edit: July 26, 2026, 11:42:44 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

AlexanderT

  • New Member
  • *
  • Posts: 37
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #2 on: July 26, 2026, 11:46:35 am »
You can already do that. kill<pid>, man kill, on windows, terminateprocess

Thank you. Could you please clarify where this can be configured in Lazarus?
I mean executing it automatically before Build/Run from the IDE, not running kill manually from the command line.

n7800

  • Hero Member
  • *****
  • Posts: 800
  • Lazarus IDE contributor
    • GitLab profile
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #3 on: August 22, 2026, 11:29:08 pm »
Project Options > Compiler Options > Compiler Commands > Execute Before/After

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12760
  • Debugger - SynEdit - and more
    • wiki
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #4 on: August 23, 2026, 12:18:47 am »
It would be helpful to have a simple option in the IDE, such as "Terminate the running application before Build".

That works, if you are running the app in the debugger. At least it should, if it doesn't then more info is needed.

Well, the IDE does not blindly kill the app. It asks you. But the IDE will actually not start the build, while the debug session is still running.

Of course, that is if your app's main process is still running. Nothing keeps track of 2ndary processes (not threads, but separate processes), if your app start any such processes. "app running" = "the process with the initial pid running"

---
If you don't run in the debugger, unfortunately the IDE does not seem to keep track of the app at all. I am not sure if that is intentional, because actually there is code that should track it....

But I can't investigate that now. If need, report as issue/feature request

n7800

  • Hero Member
  • *****
  • Posts: 800
  • Lazarus IDE contributor
    • GitLab profile
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #5 on: August 23, 2026, 12:58:38 am »
If you don't run in the debugger, unfortunately the IDE does not seem to keep track of the app at all. I am not sure if that is intentional, because actually there is code that should track it....

But I can't investigate that now. If need, report as issue/feature request

Release mode launches the process "detached" by default - the IDE doesn't indicate that debugging is in progress, and it can't stop it. However, if enable the "Project Options > Compiler Options > Debugging > Run uses the debugger" checkbox, this becomes possible, just like in normal debug mode. The debugging status is displayed, and [Ctrl+F2] works.

But there's a slight oddity: a window will pop up asking you to select the "dwarf" version. As far as I understand, since it compiled without debug information anyway, this question is unnecessary in this case? Should I create an issue?

n7800

  • Hero Member
  • *****
  • Posts: 800
  • Lazarus IDE contributor
    • GitLab profile
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #6 on: August 23, 2026, 01:01:19 am »
As for the author's question, I understand. I have several of my own applications that are always running (for example, for hotkeys). Sometimes I want to change something, and after opening the project, I can't run it in the IDE until I close the running program (since it was running before the IDE).

A shutdown script can be a really convenient solution (as long as the program doesn't lose data). Ideally, the script should send a message so that the application itself saves and closes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12760
  • Debugger - SynEdit - and more
    • wiki
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #7 on: August 23, 2026, 09:36:07 am »
If you don't run in the debugger, unfortunately the IDE does not seem to keep track of the app at all. I am not sure if that is intentional, because actually there is code that should track it....
I don't think it's fully detached. Not checked recently, but in the past "hide IDE while running" worked without debugger.


Quote
But there's a slight oddity: a window will pop up asking you to select the "dwarf" version. As far as I understand, since it compiled without debug information anyway, this question is unnecessary in this case? Should I create an issue?
No. That question remains intentional. Debug info is required for a debug run.

Switching debug info off, doesn't stop it being a debug run. That is what "run with/without debug does".

There is an issue, that debug may target a library, and the main app may not have to have debug info. But for now, simply removing the dialog is NOT the answer.

AlexanderT

  • New Member
  • *
  • Posts: 37
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #8 on: August 23, 2026, 10:26:36 am »
Project Options > Compiler Options > Compiler Commands > Execute Before/After

Thank you very much! I somehow completely missed these options - looks like I was looking everywhere but there :D

It works. For example, I can use:

Code: Text  [Select][+][-]
  1. cmd /C "taskkill /F /IM trayslate.exe >nul 2>nul & exit /b 0"

This terminates the already running application before building, while still allowing the build to continue if the application isn't running.

Or with a message when the process is terminated

Code: Pascal  [Select][+][-]
  1. cmd /C "taskkill /F /IM trayslate.exe >nul 2>nul && echo taskkill trayslate.exe & exit /b 0"

Another option is to determine the executable name from the .lpr file:

Code: Text  [Select][+][-]
  1. cmd /C "for %F in (*.lpr) do taskkill /F /IM %~nF.exe >nul 2>nul & exit /b 0"

The last option works too, but unfortunately it produces some extra output.

Although I'm still not sure how to make this cross-platform. :)

Thausand

  • Hero Member
  • *****
  • Posts: 618
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #9 on: August 23, 2026, 10:30:53 am »
Although I'm still not sure how to make this cross-platform. :)
It can also make execute script file... other can be write small program(Pascal/other language).
A docile goblin always follow HERMES.md

AlexanderT

  • New Member
  • *
  • Posts: 37
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #10 on: August 23, 2026, 10:39:21 am »
Although I'm still not sure how to make this cross-platform. :)
It can also make execute script file... other can be write small program(Pascal/other language).

Do you mean writing a small program, for example in Pascal, using conditional compilation for Windows/Linux, and then executing it here?

Another option would be to have a separate build mode/configuration for Linux with its own Execute Before command.

Thausand

  • Hero Member
  • *****
  • Posts: 618
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #11 on: August 23, 2026, 11:05:37 am »
Do you mean writing a small program, for example in Pascal, using conditional compilation for Windows/Linux, and then executing it here?
Yes, like small program is fpc progam and can use ifdef platform define

If want can make lazarus have pass option before after switch and can all make command in same small program.

fpc have example runcommand for crossplatform (exact command example "kill" is differ for all platform).

Quote
Another option would be to have a separate build mode/configuration for Linux with its own Execute Before command.
I not sure if this is implement. May be today lazarus have this. Then this also option and have all setting in Lazarus.

Ik make suggest because you have write this one lazarus solution is output text and if write own program then can make own program behave like want.
« Last Edit: August 23, 2026, 11:12:49 am by Thausand »
A docile goblin always follow HERMES.md

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12760
  • Debugger - SynEdit - and more
    • wiki
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #12 on: August 23, 2026, 11:15:18 am »
Afaik those "run before/after" take IDE macros like $(foo)

There should be one that holds the target platform or widgetset to which you compile.
So you can probably put your scripts into folders for each target, and use such a macro.

I haven't tested, and you need to search (wiki?) for the correct macro...

Thausand

  • Hero Member
  • *****
  • Posts: 618
Re: Automatic Application Termination or Pre/Post Build & Run Commands
« Reply #13 on: August 23, 2026, 11:32:32 am »
Afaik those "run before/after" take IDE macros like $(foo)
Yes, I make forget. can also make build macro for all option like script.

Quote
...
I haven't tested, and you need to search (wiki?) for the correct macro...
may be this ?
- https://wiki.lazarus.freepascal.org/Macros_and_Conditionals
- https://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames

And also is work fpc defines.
A docile goblin always follow HERMES.md

 

TinyPortal © 2005-2018