# Auto-terminate instance from shell

## post by iboates on Jan 23, 2021

Is it possible to make an instance auto-terminate with a command? Like for example when the model is finished training I would like to have it dump the model & reports & whatever else to somewhere else via sftp and then terminate the instance automatically.

---

## post by walter on Feb 7, 2025

Use a shell trap.

`help trap` / google / ai chatbot for more info.

Try something like this…

```php

# Your stuff ... snip ...

function post_training_cleanup()
{
  # You'll need to read the docs / tinker here..
  # But I imagine a shutdown or poweroff would be sufficient
  # Might require privilege escalation (sudo etc.)

shutdown
}

function main()
{
  # ... Your setup stuff ...

trap "post_training_cleanup ;" EXIT
}

#####

main "$@"
```
