Right now it only runs on initial spin-up, recipe below shows how to run on stop/start
I understand you are not able to install "tmux" package from user-data at launch on Amazon Linux2 instance i-0a6a52c0afcea695e.
I reviewed the instance and the console output, I see that the command from user-data was executed, but gave an error "cloud-init[2470]: __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'sudo yum install tmux -y...'".
I replicated a similar issue by launching an Amazon Linux 2, and used the same user-data as you used during launch, I was getting the same error.
Next, I launched a similar instance and this time I used the below user-data and the installation of "tmux" package was successful.
#!/bin/bash
yum install tmux -y
echo userdata_ok >> /tmp/is_initialized
User data shell scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash). That is the reason you were getting an error and tmux was not getting installed. [1]
If you want to run user-data script after a stop-start instance, then use the below template template:
Add any commands after #!/bin/bash
==========================
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
echo ...
chmod ...
==========================
Next, I launched an Ubuntu instances, I see that "tmux" is installed by default. To make user data shell script execute at launch always start with shebang (#!/bin/bash).
Please add the shebang line to the user-data when launching the instance, and let me know if you are still facing any problems.
Please feel free to initiate a call / chat and one of our engineers will be able to assist you further, if you have any other questions or concern.
References:
[1] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts