A cleanup logged `cd: /srv/app/current/cache: No such file or directory` and then deleted the contents of the deploy user's home directory. The code was `cd $CACHE_DIR; rm -rf *`.
BeginnerTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer
Likely causes → The cd failed (directory missing after a release switch, or CACHE_DIR empty, making cd go to $HOME), the script did not stop, and rm -rf * ran in whatever the current directory was.
Diagnose → The log line order shows cd failing immediately before the deletions; shellcheck reports SC2164 ("Use cd ... || exit in case cd fails").
Fix → Never combine a relative wildcard delete with an unchecked cd. Delete by absolute path with guards:
: "${CACHE_DIR:?CACHE_DIR not set}"
[[ -d $CACHE_DIR && $CACHE_DIR == /srv/app/* ]] || die "refusing to clean '$CACHE_DIR'"
find "$CACHE_DIR" -mindepth 1 -delete
# or: cd -- "$CACHE_DIR" || exit 1
Prevent → set -e plus ShellCheck in CI, ${var:?} on every path used in a delete, and an allow-list prefix check before destructive operations.
More on Troubleshooting: tools, portability, permissions and environment
On a Mac, `sed -i 's/replicas: 2/replicas: 3/' deploy.yaml` fails with `sed: 1: "deploy.yaml": extra characters at the end of d command`.BeginnerA cleanup script fails on a developer's Mac with `date: illegal option -- d`.BeginnerA script that works on Ubuntu fails in an Alpine-based CI image with `grep: unrecognized option: P`.IntermediateAn `awk` one-liner works on your RHEL box but fails on Ubuntu with `awk: line 1: function gensub never defined`.Intermediate`comm -23 expected.txt installed.txt` prints `comm: file 1 is not in sorted order`, even though both files were produced with `sort`.Intermediate`grep ERROR /var/log/app/app.log` prints only `grep: /var/log/app/app.log: binary file matches`.Beginner