Skip to content

Latest commit

 

History

History
15 lines (10 loc) · 1.39 KB

run-script-on-ec2-instance-termination.md

File metadata and controls

15 lines (10 loc) · 1.39 KB

Running script or command on EC2 instance on termination

To perform that you need to add the script on init level 0, which is the halt level. The script will be also invoked on shutdown and termination. The easiest way is to create symlink in /etc/rc0.d/ starting with S01 so the script will be executed first in the sequence of scripts. The usual time that you have for execution is ~2 minutes, but it may vary so don't count on it.

Here is an example:

ln -s /etc/ec2-termination /etc/rc0.d/S01ec2-termination

One important thing to consider is that in this run level you might loose environment variables such as $PATH so you need to specify the full path to your programs such as /bin/cp or /usr/bin/curl

There are other ways to achieve execution of a script before termination: