How to shutdown an elastic instance from a task?
Asked by hughperkins:

I have a single-instance license, which I want to assign to a linux-32 job, then to a linux-64 job, with each job containing a 'final' task to shut down the instance, so that the other job can have a chance.

I tried creating a 'script' task with 'sudo shutdown -h now', but it didn't have any effect. Possibly the task doesn't have 'sudo' privileges I suppose?

What is best practice method, or what is any possible method, to shut down an instance from a task?

Edit, in response to answer below:

Ok, I'm trying to avoid having to create a custom image if possible, since it's often easier to just set everything up from a startup script when this is possible.

\> You can avoid that by putting shutdown into nohup / background

Yes, this is my current strategy. In the startup script I put:  

|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 | `cat` `<<END >flaggedshut.sh` `while` `true``; ``do` `{` ` ``echo` `"polling..."` ` ``if` `[[ -f ``/tmp/shutdown``.flg ]]; ``then` `{` ` ``echo` `"shutdown now..."` ` ``shutdown` `-h now` ` ``} ``fi` ` ``sleep` `3` `} ``done` `END` `chmod` `+x flaggedshut.sh` `nohup` `bash` `flaggedshut.sh > ``/tmp/nohupout``.txt 2>&1 &` |

... then I create a task to create the /tmp/shutdown.flg file.

What about for Windows? If I've understood correctly fromhttps://answers.atlassian.com/questions/175456/instance-startup-script-vs-ec2-user-data-on-windows-elastic-agents , the startup script doesn't get executed on Windows boxes? And the tasks are run as a non-admin user, so not obvious how to install things / trigger shutdown on Windows?

\> Another approach would be to use ec2-terminate-instances in your script task.

Ah, that sounds like it would answer this specific question, which means I probably need to make a new question to ask how to run a startup script, and install things, on Windows?


Answers:
author: Przemek Bruski
body: You can grant sudo rights to bamboo user by adding this to the instance startup script in you image configuration:

sudoersFile=/etc/sudoers.d/90-bamboo-sudo  
echo "$ADMIN_USER ALL=(ALL) NOPASSWD:ALL" \>$sudoersFile  
chmod 0440 $sudoersFile

The problem is that your job will always fail. You can avoid that by putting shutdown into nohup / background, but then there's a chance that yuor agent will pick up another job and that job will fail.

Another approach would be to use ec2-terminate-instances in your script task.

comments: author: hughperkins
body: Added reply in question.


author: Przemek Bruski
body: For Windows, using ec2-terminate-instances is your best bet.


author: hughperkins
body: Ok, I'm trying to avoid having to create a custom image if possible, since it's often easier to just set everything up from a startup script when this is possible.

\> You can avoid that by putting shutdown into nohup / background

Yes, this is my current strategy. In the startup script I put:

```
cat &lt;&lt;END &gt;flaggedshut.shwhile true; do {   echo "polling..."   if [[ -f /tmp/shutdown.flg ]]; then {      echo "shutdown now..."      shutdown -h now   } fi   sleep 3} doneENDchmod +x flaggedshut.shnohup bash flaggedshut.sh &gt; /tmp/nohupout.txt 2&gt;&amp;1 &amp;
```

... then I create a task to create the /tmp/shutdown.flg file.

What about for Windows? If I've understood correctly from [https://answers.atlassian.com/questions/175456/instance-startup-script-vs-ec2-user-data-on-windows-elastic-agents](/questions/175456/instance-startup-script-vs-ec2-user-data-on-windows-elastic-agents) , the startup script doesn't get executed on Windows boxes? And the tasks are run as a non-admin user, so not obvious how to install things / trigger shutdown on Windows?

\> Another approach would be to use ec2-terminate-instances in your script task.

Ah, that sounds like it would answer this specific question, which means I probably need to make a new question to ask how to run a startup script, and install things, on Windows?

<br />


Tags: cloud