You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Graceful shutdowns for coder agents and shutdown scripts #6175
In the ideal scenario where a workspace agent is connected to the coder server, we should initiate agent shutdown before we initiate Terraform provisioning to destroy/re-create the resource(s).
Why?
Providers behave differently, some may not initiate graceful shutdowns and we might not be able to control timeouts for them
Template authors may use the agent shutdown_script to perform a critical task that must complete successfully (e.g. backing up filesystem)
The task/script may take a long time
We can leave the workspace running and allow debugging an agent that didn't successfully execute its shutdown_script
To consider:
At the end we must not exit the agent process
Only applies when graceful shutdown is initiated by coder server!
We should de-register signal handlers and wait "indefinitely"
Let the next signal terminate the process
Why? This prevents e.g. a systemd service from restarting the agent
Should behavior differ if a workspace is started/stopped?
What happens when an agent is disconnected and we can't tell it to shut down? -> Block or allow de-provision? Require the use of "force"?
Hello, is there any update to this issue? Our organization has needs to run a script that would save state into something more permanent. It would be super useful if we were able to easily place a script in a predetermined directory or some other method that would allow it to run the script during shutdown.
Hi @kaypeter87, there are no real updates at this time (this issue is still relevant and hasn't changed).
However, what you're looking to achieve should be possible, with a caveat.
If you look over at #6174 you may find a solution for how to allow graceful shutdown to be extended depending on the provider you're using, caveat being that it can't be changed dynamically and you will have to pick a duration as the maximum grace time for shutting down.
Once you've ensured that stop only sends an interrupt signal to the workspace and waits for the agent to fully shut down, you can update the agent in your template to something like this:
resource"coder_agent""main" {
# ...shutdown_script=<<-EOT DIR=/path/to/shutdown.d if [ -d "$DIR" ]; then for script in "$DIR"/*.sh; do if [ -x "$script" ]; then "$script" fi done fi EOT
}
With that done, you can place executable shell files (.sh) into the directory specified in $DIR and have them run at shutdown.
Here's an example usage:
mkdir -p /path/to/shutdown.d
echo$'#!/bin/bash\necho Hi from shutdown\n'> /path/to/shutdown.d/hello.sh
chmod +x /path/to/shutdown.d/hello.sh
(The #!/bin/bash shebang is important here, since it's what makes the script executable in combination with the chmod, the shutdown script could alternatively be written differently if other use-cases need to be supported.)
Does this help?
NOTE: Providers usually have a short grace period, so this should work without any additional changes, but if you want to run long-running operations, please do take a look at the issue I linked earlier.
Activity
github-actions commentedon May 15, 2023
mafredri commentedon Jan 9, 2024
This is still relevant, so re-opening.
1 remaining item
[-]Implement pre-provision signaling to stop/shutdown the agent[/-][+]Graceful shutdowns for coder agents[/+][-]Graceful shutdowns for coder agents[/-][+]Graceful shutdowns for coder agents and shutdown scripts[/+]kaypeter87 commentedon Sep 25, 2024
Hello, is there any update to this issue? Our organization has needs to run a script that would save state into something more permanent. It would be super useful if we were able to easily place a script in a predetermined directory or some other method that would allow it to run the script during shutdown.
mafredri commentedon Sep 26, 2024
Hi @kaypeter87, there are no real updates at this time (this issue is still relevant and hasn't changed).
However, what you're looking to achieve should be possible, with a caveat.
If you look over at #6174 you may find a solution for how to allow graceful shutdown to be extended depending on the provider you're using, caveat being that it can't be changed dynamically and you will have to pick a duration as the maximum grace time for shutting down.
Once you've ensured that stop only sends an interrupt signal to the workspace and waits for the agent to fully shut down, you can update the agent in your template to something like this:
With that done, you can place executable shell files (
.sh) into the directory specified in$DIRand have them run at shutdown.Here's an example usage:
(The
#!/bin/bashshebang is important here, since it's what makes the script executable in combination with thechmod, the shutdown script could alternatively be written differently if other use-cases need to be supported.)Does this help?
NOTE: Providers usually have a short grace period, so this should work without any additional changes, but if you want to run long-running operations, please do take a look at the issue I linked earlier.
mafredri commentedon Jan 20, 2026
This has come up again. @spikecurtis identified additional failure scenarios:
And clarified the solution:
See #21538 (comment)