[ norboten ]
bash · lab bash-05

The Export That Leaves Its Mess Behind

· about 35 minutes · runs on ubuntu-26.04-devops · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

export-orders joins the day's order files from /srv/orders/*.csv into one compressed CSV, /srv/exports/orders-<YYYY-MM-DD>.csv.gz. The warehouse importer picks up any file in /srv/exports whose name matches orders-*.csv.gz, as soon as it appears.

On Tuesday the export was stopped half way by a deploy that restarted the box's jobs, and the warehouse imported half a day of orders. On Thursday one order file could not be read; the export said exported, and the file was short again. Someone also found a stale /tmp/export.tmp owned by a colleague that made their own run fail.

What is expected, and graded — the grader runs export-orders itself, as an ordinary user, through the EXPORT_SRC and EXPORT_DEST variables the script already reads:

  1. A good run writes the complete export — one header line id,sku,qty, then every row of every file — and exits 0.
  2. When an order file cannot be read, the script exits non-zero and nothing new appears in the destination: no export, no temporary file.
  3. When the script is stopped with SIGTERM in the middle of an export, it exits non-zero and leaves nothing behind — not in the destination, not in the temporary directory.
  4. Two exports running at the same time, from different sources to different destinations, both produce their own complete file.

You have root through sudo. It must all still hold after a reboot.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_a_good_export_is_completePublish a file only when it is complete, in one step
02_a_failed_export_leaves_nothingPublish a file only when it is complete, in one step
03_an_interrupted_export_leaves_nothingClean up after a script however it ends: success, failure or a signal
04_two_exports_at_onceKeep two runs of the same script from sharing a temporary file

Start it

  1. 2Labs
  2. bash-05select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

4 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Three of them, to answer here:

Question 1

What does this print?

out=$(bash -c 'trap "echo cleaned" EXIT; kill -TERM $$; echo after')
echo "$out $?"
Question 2

A bash script runs `trap 'echo stopping' TERM` and then a command that takes a minute. Ten seconds in, it receives SIGTERM. What happens?

Question 3

An importer takes any `orders-*.csv.gz` in `/srv/exports` as soon as it appears. Which way of writing the export can never hand it a partial file?