This is a minimal example of how you would run a script “detached” on a remote server, so you can disconnect SSH, and free up your Terminal to do other things, or even close your Terminal and turn off your computer altogether.
Let’s say we want to run some script on a server, say
fix-stuff.sh, that would run for 1 hour. We wish to have the script running without needing to be connected to it on our computer, and waiting for it to finish.
The script is quite plain and simple, so we are confident that it won’t take down the server, thus we can leave it running. (Perhaps the server is not even important enough for us to worry about it anyway, like a testing server.)
We use tmux (terminal multiplexer) to start a “session” on the server. Inside the session, we can run scripts as we normally do. The tmux session is just a standard command line interface with extra stuff. When we detach the session, the script will continue to run inside the session on the server. When we log off, it won’t terminate the script, which is still running in the tmux session. At a later time, we can come back and “reattach” the session.
First, we will need to log into the server.
I am assuming our server is running Linux or macOS (or another nix OS) and we can access it via SSH.
check and install tmux
We need to ensure that tmux is installed on the server. We only need to do this once.
tmux -V (note: uppercase ‘V’) on the remote server.
It should display the version number if tmux is installed:
$ tmux -V tmux 1.8
Otherwise, it should display “command not found” if tmux is not installed:
$ tmux -V bash: tmux: command not found...
To install tmux if it is not already installed on your server:
# Fedora distros. sudo dnf install tmux -y # RHEL/CentOS distros. sudo yum install tmux -y # macOS / Homebrew. brew install tmux # Debian/Ubuntu distros. sudo apt install tmux -y
Pick the command applicable to the operating system of your server.
To start a new session, simply run
tmux on the server.
We would see a green bar on the bottom of the tmux session.
Now we can run any command as we normally would.
Let’s run our script, then detach the session.
To detach, use the following keyboard shortcuts:
- Press both keys:
- Release both keys.
CTRL-b as a way of telling tmux that a command is coming, and
d is for “d” in “detach”.
We would see that tmux’s green bar has disappeared. The script would continue to run in the tmux session. We can now disconnect from SSH and the script will continue to run.
We can re-attach the session any time we like to check the script and see if it has finished or is still running.
To do this we would ssh back to the server, then run:
You should now see tmux’s green bar at the bottom again. The ‘a’ in the
tmux a command stands for “attach”, which will just attach a detached session.
At this point, we will see if the script is still running. We can detach the session again if the script is still running, or if the script has finished, type
exit to end the session.
If you have multiple sessions, you can use
tmux ls to list them. If you run multiple sessions, you would give each session a name, so when you want to reattach them, you can specify the exact session by its name.
I would use 1 session for the purpose of leaving a script running on a server if my situation allows, so it is easy to detach and reattach sessions without worry about names. There are only 2 commands that I would need,
tmux can do a lot more
You can certainly do a lot more with tmux: tabs, splitting panes, etc.
For all other powerful features of tmux, I would recommend the following guide: http://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 😄