In AWS, how to keep running processes for ssh connections that keeps breaking?
Strategies to Ensure Your AWS Processes Keep Running Even if Your SSH Connection Breaks
Hey folks, I started my AWS cloud journey last year, and then learnt about launching the AWS instances and creating the key pairs. In this article, basically we'll be solving an issue which I faced frequently while connecting to the instances.
If you're an AWS user, you might have experienced the frustration of having your SSH connection break unexpectedly. This can be particularly troublesome when you have long-running processes that you need to keep active. In this blog post, we'll discuss some strategies for keeping your processes running even when your SSH connection drops.
Use screen or tmux:
One way to keep your processes running is to use a terminal multiplexer like
tmux. These tools allow you to create multiple virtual terminals within a single SSH session, so you can keep your processes running even if your connection drops. Simply start a screen or tmux session, start your process, and then detach from the session. Your process will continue running in the background, even if you disconnect from SSH.
To start a new tmux session, use the following command:
tmux new -s mysession
To detach from the session, press
CTRL+b, followed by
To reattach to a detached tmux session, use the following command:
tmux attach -t mysession
Another option is to use the nohup or disown commands to detach your process from your SSH session. Nohup stands for "no hang up," and it allows you to run a command that will continue running even if you close your terminal or disconnect from SSH. To use nohup, simply prefix your command with "nohup." For example, "
nohup mylongrunningprocess &". The "&" at the end tells the shell to run the command in the background. Alternatively, you can use the disown command to detach a process that is already running. Simply find the process ID (PID) using the "
ps" command and then run "
disown PID" to detach it.
To use disown, first start your long-running process in the foreground. Then, use the
CTRL+zkey combination to suspend the process. Next, use the
bgcommand to resume the process in the background. Finally, use the
disowncommand to detach the process from your current shell:
mylongrunningprocess >> CTRL+z >> bg >> disown
Use a persistent session:
If you're using AWS EC2 instances, you can create a persistent SSH session using tools like Mosh or Tmate. These tools allow you to keep your SSH connection alive even if you switch networks or lose connectivity temporarily. This is particularly useful if you're working on a mobile device or laptop and frequently switch between networks.
Use an SSH tunnel:
If your SSH connection is being blocked by a firewall or network security rules, you can use an SSH tunnel to bypass these restrictions. An SSH tunnel allows you to create a secure connection between your local machine and your AWS instance, even if the port you're trying to connect to is blocked. To create an SSH tunnel, use the "
-L" option with the SSH command. For example, "
ssh -L 8080:
myuser@myinstance" would create a tunnel that forwards traffic from port 8080 on your local machine to port 80 on your AWS instance.
Keeping your processes running on AWS can be a challenge, but with the right tools and strategies, it's definitely possible. By using a terminal multiplexer, nohup or disown, persistent SSH sessions, or SSH tunnels, you can ensure that your processes keep running even if your SSH connection drops. Remember to always test your setup thoroughly to make sure that everything is working as expected.