Linux Troubleshooting: How To Keep A Process Running After Putty Or SSH Session Closed?

Mostly, I use SSH command to connect to my remote server. Unfortunately, today the power is gone and the Ethernet hub is turned off, so my SSH session was lost. What do you do if an important task is going on after losing your SSH session? Here is the simple tool called Tmux to keep a process running  after the SSH or Putty session lost.

Tmux is a terminal multiplexer. it enables a number of terminals to be created, accessed, and controlled from a single screen.  tmux may be detached from a screen and continue running in the background, then later reattached.

First install Tmux on your remote systems.

In RPM based systems:

yum install tmux

In DEB based systems:

sudo apt-get install tmux

After tmux, SSH to your remote server, for example CentOS 7 in my case.

ssh root@ip-address

Open your terminal, and run tmux.

tmux

Then run the task you want to perform.

Let me run a task, for example, I am going to update my CentOS 7 server.

yum update

Sample output:

root@server1:~_001

Now the update process is going on. Then, press ‘CTRL+B’ followed by ‘D’ keys to detach or leave from the tmux session.

Now, you’re back to your actual Terminal, but the process will be still running in the tmux session. Log off and log in again to remote system using SSH. Type tmux attach in the terminal.

tmux attach

You see there, the same process will still be running!!

root@server1:~_002

Similarly, you run as many as tasks you want inside the tmux session. Ofcourse, you can name each session by pressing Ctrl-B and $.

You can list out the number running tmux sessions from the actual Terminal using command:

tmux list-sessions

Sample output:

0: 1 windows (created Thu Jan  8 15:13:42 2015) [80x23]
1: 1 windows (created Thu Jan  8 15:20:05 2015) [80x23]

Tmux can do much more than described here. We can start long running scripts from remote system and detach. after certain time later we can attach and view the status. Check our previous article about tmux.

Also check the man pages.

man tmux

That’s it. Stay happy!

Cheers!