When I ask someone to run a script on their server to understand an outage, that script enters their house. It must change nothing, leave nothing world-readable behind, and above all must not stop at the first missing tool: a partial snapshot is better than none. Here are the rules I follow, and the script that goes with them.
The script was run on Ubuntu 26.04 (bash 5.3) and checked with ShellCheck, with no warnings. It only uses commands available on RHEL, Oracle Linux and Debian. The names in the script are in French (releve = snapshot, disques = disks); the log lines read absent for a missing tool and echec for a failure.
The script
#!/usr/bin/env bash
# collecte.sh: read-only snapshot, to run on a server under diagnosis
set -Eeuo pipefail
umask 077
STAMP=$(date -u +%Y%m%dT%H%M%SZ)
OUT=${OUT_DIR:-/tmp}/collecte-$(hostname -s)-$STAMP
mkdir -p "$OUT"
LOG=$OUT/collecte.log
trap 'echo "ERREUR ligne $LINENO : $BASH_COMMAND" >>"$LOG"' ERR
trap 'tar -C "${OUT%/*}" -czf "$OUT.tar.gz" "${OUT##*/}" && echo "Archive : $OUT.tar.gz"' EXIT
# Run a command, keep its output, never stop the collection
releve() {
local nom=$1; shift
if ! command -v "$1" >/dev/null 2>&1; then
echo "absent : $1" >>"$LOG"; return 0
fi
if timeout 20 "$@" >"$OUT/$nom.txt" 2>&1; then
echo "ok : $nom" >>"$LOG"
else
echo "echec : $nom (code $?)" >>"$LOG"
fi
}
releve systeme uname -a
releve os cat /etc/os-release
releve uptime uptime
releve disques df -hT
releve inodes df -i
releve memoire free -m
releve ports ss -tulpn
releve echecs systemctl --failed --no-pager
releve journal journalctl -p err -b --no-pager -n 200
releve inexistant commande-qui-nexiste-pas
releve fichier cat /etc/fichier-absent1. Read-only, no exceptions
Each releve line reads the state of the system: df, free, ss, systemctl --failed, journalctl. None of them restarts, cleans or "fixes" anything. If a fix is needed, it is decided after reading the snapshot, with the person in charge of the server, not inside the script.
2. set -Eeuo pipefail, but no stop when a command fails
-u: a misspelled variable stops the script instead of becoming an empty string (with a typo,rm -rf "$DIRECTRY/"becomesrm -rf /).-o pipefail: a failure in the middle of a pipe is not hidden by the last command.-eand-E: an unexpected error stops the script, and theERRtrap is inherited by functions.
For a snapshot, stopping at the first failure would be a bad idea: ss can be missing on an old machine, journalctl in a container. The releve function therefore runs each command inside an if: a failure inside an if triggers neither -e nor the ERR trap. It is written to the log, and the collection goes on.
3. timeout on every command
A command that reads a dead NFS mount can wait forever. timeout 20 bounds each step: at worst the snapshot takes a few minutes, never a whole night.
4. Dated, private outputs, and a log of what happened
- The folder carries the short host name and the UTC time: two snapshots never overwrite each other, and you know where an archive received by e-mail comes from.
umask 077: files are readable only by whoever ran the script. A snapshot contains ports, service names, log excerpts; it has no business being world-readable in/tmp.collecte.logsays what worked, what was missing and what failed.
Here is the log I got, with two lines added on purpose to test the failure cases (a command that does not exist, a missing file):
ok : systeme
ok : os
ok : uptime
ok : disques
ok : inodes
ok : memoire
ok : ports
ok : echecs
ok : journal
absent : commande-qui-nexiste-pas
echec : fichier (code 1)
And the permissions actually set:
600 /tmp/t1/collecte-JEM-20261204T213858Z.tar.gz
700 /tmp/t1/collecte-JEM-20261204T213858Z/
600 /tmp/t1/collecte-JEM-20261204T213858Z/disques.txt
The script exited with code 0 despite the two failures: that is the intended behaviour.
5. The archive is built on exit, whatever happens
The EXIT trap creates collecte-<host>-<time>.tar.gz at the end, even if the script stops on an unexpected error. There is a single file to send back, and the person does not have to choose what to attach.
Before sending it to anyone
bash -n collecte.sh # syntax shellcheck collecte.sh # common mistakes OUT_DIR=/tmp/test bash collecte.sh
Without ShellCheck installed: docker run --rm -v "$PWD:/mnt" koalaman/shellcheck:stable /mnt/collecte.sh.
And I read it one last time with a single question in mind: if this script ran on my own production, would I sleep well?