Managing linux shell sessions with tmux

If you run shell scripts for data analysis which have a long execution time on a server, you probably feel the frustration when your network connection drops out momentarily and terminates the job. Sure, we can use the 'nohup' command to force the command to proceed despite loss of connection, but this is not ideal when for example we are doing an interactive analysis or trying to fix issues in a large job. In those cases, I like to use tmux to save my session. It enables me to rejoin that analysis if I get interrupted with other work and continue working interactively later.

Here I will give you a guide to use tmux to save server sessions and rejoin them later.

 First check whether tmux is already installed on your server:
tmux ls
If tmux is installed it will list the current sessions. If it is not installed, then it will say something along the lines of "tmux cannot be found".

If you need to install it, you can ask your sysadmin to run:
sudo apt update && sudo apt install tmux
To start a new session, use the following syntax:
tmux new -s <new_session_name> 
For example:
tmux new -s pipeline1
You will now find yourself inside the session, and all the jobs you run will persist even if your ssh connection drops out. You can detach from the active session by using the command:
tmux detach
When detached, the session is still active, it's just running in the background. The alternative to detach is to close the terminal window.

On the server, you can then run "tmux ls" to list the current sessions.
To connect to a session run the following:
tmux attach -t <existing_session_name>
For example:
tmux attach -t pipeline1
That's all there is to it. You can develop more advanced used of tmux as covered elsewhere in a multitude of blogs and youtube videos.

Popular posts from this blog

Mass download from google drive using R

Data analysis step 8: Pathway analysis with GSEA

Extract properly paired reads from a BAM file using SamTools