It is simple enough to edit and run files while in a remote environment; however, when dealing with big data or computationally intensive procedures you may want to work on something else or even turn off your computer to go to bed. The worst is when you're running a program and you disconnect due to connection issues.
To solve this problem, this tutorial will cover how to run your jobs in the background using
Running long jobs on your machine of choice can be cumbersome since it requires maintaining an active session (staying logged on, keeping laptop powered on, disabling hibernation, etc.). Fortunately, this can be alleviated by running jobs on a remote server using tmux (terminal multiplexer). tmux allows you to run a job while being able to disconnect from a container and from the server. You power off your computer, come back, and reattach to your running job.
Although tmux is used for more than job management, we will discuss the basic usage in regards to job management. For more advanced features please view the tmux wiki.
tmuxand press enter. This will open up a tmux session which is no different than your regular terminal but with additional features.
tmux detachor the keyboard shortcut
d(2 steps: "control" and "b" together followed by "d" by itself). Detaching means that we are leaving the tmux session but leaving any jobs/programs running in the background within that session.
tmux ls. We only have 1 session called "0" with 1 window open:
-tflag for the following command:
tmux attach -t <name/number>where name/number can be found on the left-most output of
tmux kill-session -t <name/number>:
tmux kill-server. This will end all tmux sessions.
tmux new -s <name>:
Let's start fresh by running
tmux kill-server and running
tmux to create a new session:
This will attach us to a new tmux session as discussed. As an example, we will use the
simulate_job.R file to run a long job within the tmux session. This will create an empty file in a folder every 10 seconds:
Now, let's detach from the tmux session using our keyboard shortcut
d as shown in step 3 in the previous page. This will detach our session back to our main terminal. From here, we are free to detach from the dev container and from our remote server!
We are free to shut down our computer and come back to it whenever we please. In order to retrieve our job(s), we need to:
tmux attach(for the most recent session) or
tmux attach -t <name/number>(to find a specific session and attach to it):
tmux is much more than a way to run and recover jobs in the background. There are many keyboard shortcuts and customizability features that are available. Specifically, tmux can:
This means that instead of needing more than one tmux session, we can add additional windows or additional panes as needed. Below is an example of 3 panes in a single window of a tmux session:
These features, as well as additional configuration and customization can be found in the tmux wiki.