stopping an linux aws instance from linux command line - linux

Is there a way to stop and AWS ec2 instance from the VM itself?
If i start a ec2 linux based instance, is there a way for me to stop that instance by giving some linux command like "shutdown now"?

It's working with
shutdown -h now
or
shutdown -h +10
If you don't use "-h" parameter to halt, the instance will keep on running state.

Yes, with a couple of caveats.
If you are using an instance store backed instance, your only option will be to terminate. Without EBS volumes, the instance cannot exist in a stopped state.
There is also a flag that can be set on the instance as to how instance initiated shutdown is handled. This can be stop or terminate. If you want to stop your instance, make sure that this flag is configured correctly.
Other than that, you would use the normal Linux shutdown commands. shutdown now

Try these commands.
1. sudo poweroff
2. sudo shutdown -h now

Related

Start and stop EC2 and then enable/check services on instance using bash

I need to stop start multiple EC2 instances and run few command after they start, using bash on the same manage server without needing to ssh.
I know how to start and stop EC2 instances, for example:
start instance:
#! /bin/bash
aws ec2 start-instances --instance-ids i-1a1234
Stop instance:
#! /bin/bash
aws ec2 stop-instances --instance-ids i-1a1234
I figured how to list all EC2 IDs by running:
aws ec2 describe-instances --filters "Name=tag:Name,Values=Test: Instance 1" --query 'Reservations[].Instances[].[InstanceId] --output text
My questions are:
How can I stop/start multiple EC2 instances?
How can I grab a specific EC2 instance and run a command to check a service enabled after the instance started?
May be I need to run if condition to compare the id or tag name with the EC2 instance needed then run the shell command?
I know the logic but please help me with the script details.
You can start/stop multiple instances by specifying their IDs:
aws ec2 start-instances --instance-ids i-1a1234 i-1a1235 i-1a1236
If you want to run a script on an instance every time it starts, you can put the script in:
/var/lib/cloud/scripts/per-boot/
This will use cloud-init to automatically run the script, which can then do whatever checking/processing you wish.
When your program on the instance has finished its processing, it can stop its own instance with:
sudo shutdown now -h
For more details, see: Auto-Stop EC2 instances when they finish a task - DEV Community
Alternatively, you cold use AWS Systems Manager Run Command to run a script on the instance. This is a great way to run jobs on instances that are already running. The command can even be executed on multiple instances simultaneously.

Run script in userdata Detached

I am trying to start and run a long running process on my ec2 instance via userdata.
This process needs to stay running for the lifespan of the instance.
This process also needs to run as another user i.e centos and not root.
I am able to run the script and it works fine but it will not allow me to ssh into the server until I stop/start the instance.
Here is how I am running the process now.
I get the error Connection refused
/bin/su -c "/home/centos/downloadAndStart.sh" - centos /dev/null &/dev/null &
without running the script I am able to ssh into the server as soon as it initializes.
I believe this is because of something like the process is still attached to the PID of the userdata script? Does that make sense?
I am not entirely sure why I cannot ssh into the instance until after I restart the instance.

Cassandra process killed on exit

When I run dsc cassandra on CoreOS(tarball) using telnet everything comes up fine. But when i close the telnet session, it kills the process. How do i keep the cassandra server running?
I tried sudo bin/cassandra and sudo bin/cassandra -f
both didnt help.
I have no issues in other OS.
Option Description
-f Start the cassandra process in foreground. The default is to start as background process.
-h Help.
-p filename Log the process ID in the named file. Useful for stopping Cassandra by killing its PID.
-v Print the version and exit.
When you are starting cassandra using -f it runs in foreground, hence it will stop as soon as terminal is closed. Same is true for background process.
This will happen with any application you run in telnet session.
You can try
sudo service cassandra start OR nohup bin/cassandra this will keep your application running even when terminal is closed
You need to run Cassandra as a systemd service, as described here: https://coreos.com/os/docs/latest/getting-started-with-systemd.html
Running in the foreground with cassandra -f as your ExecStart= command will allow systemd to manage the state of the process (ideally inside a container).
While this is a bit different than what you're used to, it will lead to an overall more stable mechanism since you'll be using an init system that understands dependency chains, restart and reboot behavior, logging, etc.
Run the process in a screen or tmux session. Detaching from the screen session should allow the process to keep running.

shut down ("destroy") libvirt VM when it reboots

For a while, I was using virt-install to install an OS on libvirt VMs. I learned that the OS has an autoinstaller feature that requires the use of a second CD-ROM (to feed information about the desired configuration to the installer), but I found that virt-install unfortunately ignores all but one --cdrom argument. The alternative that I came up with is to output the VM configuration virt-install would use with just one CD-ROM to a file using the --print-xml argument, edit that file to add the second CD-ROM, and then use virsh create <xml config file>.
When I was using virt-install before, the VM rebooted itself at the end of installation and virt-install would notice and shut down ("destroy") the VM instead of allowing it to reboot, leaving me with a nice clean installed disk image. However, now when the VM reboots after completing installation, it actually boots up again instead of shutting down cleanly, so I can't programmatically tell when the installation has completed. After the reboot it looks like the same qemu-system-x86_64 process is being used, so I also can't use it to tell when the installation has completed.
How can I force libvirt to shut down ("destroy") the VM instead of rebooting the way virt-install did? Alternatively, is there some other indicator I can use to tell that a VM reboot has occurred?
Although there doesn't seem to be a way to automatically destroy a libvirt VM on reboot through a special incantation of virsh create or by changing options in the domain XML file, I stumbled across the very useful virsh event command:
$ virsh help event
NAME
event - (null)
SYNOPSIS
event [<domain>] [<event>] [--all] [--loop] [--timeout <number>] [--list]
DESCRIPTION
List event types, or wait for domain events to occur
OPTIONS
[--domain] <string> filter by domain name, id, or uuid
[--event] <string> which event type to wait for
--all wait for all events instead of just one type
--loop loop until timeout or interrupt, rather than one-shot
--timeout <number> timeout seconds
--list list valid event types
The command blocks until an event of the specified type occurs for the specified domain. This allowed me to achieve my goal of emulating the behavior in virt-install by doing:
$ virsh event domain1 --event restart
event 'reboot' for domain -
events received: 1
$ virsh destroy domain1
And it even gives me a built-in timeout mechanism!

Running perl script on AWS EC2 linux AMI continually

I have used various snippets of code to build a system which
listens to a port for incoming TCP data (using a perl script), writes this data to a log file.
calls and runs a PHP script to consume the log file and write it to an RDS MySQL DB
I have a GPS device configured to send the data to the elastic IP of my AWS EC2 Server
It works fine, and when i run via SSH
perl portlistener.pl
it does it's job fine, happily working away.
The only way I can stop the script running is by closing the terminal window, ending my SSH session. What I need to do, is keep it running at all times, and to implement a start, stop and restart facility. Do i need to create a daemon?
I know PHP, but until now have never worked with Perl. I'm also not that familiar with command line, other than installing updates, navigating and editing single files etc.
Thanks in advance for any help, or for pointing me in the right direction.
Solved it I think!
installed CPAN http://www.thegeekstuff.com/2008/09/how-to-install-perl-modules-manually-and-using-cpan-command/
Using CPAN, installed Deamon::Control
Then created a new program as below (portlistener_launcher.pl), and ran it as SU.
#!/usr/bin/perl
use strict;
use warnings;
use Daemon::Control;
$ENV{PHP_FCGI_CHILDREN} = 10;
$ENV{PHP_FCGI_MAX_REQUESTS} = 1000;
Daemon::Control->new({
name => 'portlistener',
program => 'perl /home/ec2-user/portlistener/portlistener.pl',
fork => 2,
pid_file => '/var/run/portlistener.pid',
stdout_file => '/var/log/portlistener.log',
stderr_file => '/var/log/portlistener.log',
})->run;
There's probably a neater way of doing it, but it seems to work, and I can stop/start it like so:
perl portlistener_launcher.pl start
If the terminal window is the only task, you can use the nohup command, e.g.
http://linux.101hacks.com/unix/nohup-command/
To terminate the listener you can kill an appropriate running process or processes.
An implementation of daemon does not ensure its permanent running. It can crash or might be killed from someone. To guarantee the permanent daemon running you must implement a 24x7 monitoring of this daemon and automatic restarting of it.

Resources