Chapter 5 BASH

On this section we will write some hints on the Bourne Again Shell (BASH), a command interpreter used in several *nix systems, such as Linux, Unix, MacOS and recently incorporated into Windows through PowerShell. This will be more of a collection of hints, and not substituting a full knowledge of BASH and the Operating System in which is running.

5.1 Screen

Screen is a tool you can use to keep a ssh session run through BASH alive. Why it is useful(?):

  1. When you have unreliable internet connections connections (ex.: Wifi, cellphone);
  2. Power outages;
  3. Your local machine freezes, or have any other issue, and your connection is killed;
  4. Any random reason that blocks your local terminal to access the remote ssh session.

5.1.1 How to use it

Some useful description is available in this website, and on the app documentation. But you can proceed as follows:

5.1.2 Creating a session

You create a new session, named session_01 as:

screen -S session_01

5.1.3 See all available sessions

screen -ls

You probably will see something like this:

There are screens on:
        6617.session_00      (09/26/2019 04:35:30 PM)    (Detached)
        1946.session_01         (09/26/2019 02:51:50 PM)    (Detached)
2 Sockets in /run/screen/S-shs

5.1.4 Reattach to a previously established session

Let’s say you want to attach to session_00:

screen -r session_00

5.1.5 Finishing a session

Let’s say you want to finish session_01. You first enter in another session, and:

screen -XS 1946 quit

If the session you are aiming to finish is the last one, then:

  • ctrl + A;
  • k;
  • and y.

5.2 Watch

Watch is a program that runs commands using a clock. In our case, watch is kind of useful when needing to monitor an application shell output. Common cases in HPC are when you need to see updates in nvidia-smi or slurm squeue command.

The base syntax is:

watch -n XX command

In which:

  • XX is the refresh rate in seconds;
  • command is the bash command you want to refresh the output

5.2.1 Slurm queue example

The base syntax is:

watch -n 5 squeue -u username

In which:

  • username is the username in the slurm cluster;

5.2.2 NVIDIA card output

The base syntax is:

watch -n 5 nvidia-smi

It refreshes every 5 seconds the running info from the NVIDIA card in the node (this will fail on CPU-only nodes)