How to shutdown an elastic instance from a task?

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?

2 answers

1 accepted

1 vote

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.

Added reply in question.

For Windows, using ec2-terminate-instances is your best bet.

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.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 &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 , 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?


Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 18, 2017 in Bamboo

FAQ: How to Upgrade Bamboo Server

Bamboo 5.9 will no longer be supported after June 12, 2017. What does this mean? As part of our End of Life policy, Atlassian supports major versions for two years after the first major iteratio...

1,786 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you