This week’s article is about how we spend the time while code is compiling, a web app is being deployed, a neural network is learning or a job sent to an HPC facility is being executed. Some of these tasks might take quite some time, but in the day-to-day life of a programmer, there are a lot of tasks that take something between 20 seconds and 5 minutes. One can stare idly at the blinking console cursor and wait for the prompt to reappear, or follow Randall Munroe’s suggestion:
Usually, I divert my attention to other, small tasks like checking my emails. The issue is, that one small task quick leads to another (let me quickly try this…), and the initial job I was waiting for gets forgotten. While browsing for a solution, I discovered that my issue is not to stay focused (although they used the same xkcd comic). My issue was a missing notification when the initial task was done. The solution is a program called universal notifications. Let’s look at it in more detail with four real-life examples:
- Receive a notification when a new process finishes,
- Receive a notification when an already running process on a remote machine finishes,
- Receive a notification when the CPU usage of a running process drops below a threshold and
- Receive a notification when a GitLab CI pipeline is done.
Use-case 1: New process
Let’s assume we want to compile our latest and greatest program using
It takes about 2 minutes to compile it.
To prevent the issue mentioned above,
$ un make -j4
instead of just
make -j4. The code will compile as expected—or it won’t. In
any case, you will receive a GUI notification once the process terminates. Under
the hood, universal notifications uses
Use-case 2: Running process on a remote machine
Assume you’ve started an extremely involved build a remote machine with more
resources than your laptop could offer. One could assume, that sending
notifications over SSH channels is complicated. Well, it might be, but with
it’s doable. All you need is the id of the parent
build process. For the sake of this example, assume the id is
$ un -s email@example.com 7098
This line will use SSH to log on to
remote.host and send a notification to
laptop once process 7098 terminates.
Use-case 3: CPU usage drops below threshold
Assume you are working in a jupyter notebook. Let’s say you are training a neural
network. It’s not too large, neither is the training set, so it should be
trained within a few minutes. Jupyter is running in a Firefox tab while
you switch to another tab, and the happily learning network completely disappears
from your mind. How can you receive a notification once the training is done?
One method is to receive a notification once the CPU usage of the process in
question drops below a certain threshold. This can be done with universal
notifications and it’s plugin
$ un uwait -t 10 7098
The line can be dissected into two independent programs. By now, you know already
uwait watches the process with id 7098 and
terminates once the process’ CPU usage drops below 10%. Since
un waits for its
argument to terminate, you receive the notification once the neural network is
Use-case 4: Notification once GitLab CI is done
With yet another plugin, you can use
un to scream at you once a GitLab CI
pipeline or job is done. The
ciwait plugin must be installed
independently. You will need an API token to give the plugin access to the GitLab
server. Assume the id of the pipeline is 53124, the id of the GitLab project is
$ un ciwait frank gitlab.sauerburger.com 245 53124
Once all jobs are done (either success, canceled, skipped or failed), you’ll receive a notification.
Obviously, there are other use-cases such as waiting for a local process already
running in the background
or a new remote process. Furthermore,
may other tools may can be combined with
un. You could, for
example, extend it with a plugin to query the queue of an HPC facility to receive
a notification once all your jobs are done.
This very short command-line tool greatly improved my productivity and keeps me focused on the main task. On the other hand, when I needed to wait 5 minutes or so, I can now shift my attention to other tasks with the certainty that I will not forget to go back to the main task.