Interview › Scripting (Bash, Groovy)
How would you make a long-running script run safely in the background? [Advanced]
Answer
For a long-running script, I prefer running it under systemd, supervisord, Kubernetes, or another process manager. If I must run it manually in the background, I use nohup or a terminal multiplexer, redirect logs, store the PID, and handle signals cleanly.
Technical explanation
A process manager provides restart policy, logs, lifecycle control, and dependency ordering.
Manual backgrounding with & is fragile because the process can die when the session closes unless detached correctly.
The script should trap TERM/INT and clean up child processes or lock files.
Hands-on example
# Better: systemd service
systemctl start my-worker.service
journalctl -u my-worker.service -f
# Manual fallback
nohup /usr/local/bin/worker.sh >> /var/log/worker.log 2>&1 &
echo $! > /run/worker.pidPreparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the difference between a cron job and a systemd timer? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- How would you write a health-check script that polls a URL until it returns 200? [Advanced]
- How do you define and call a function (or step) in a Jenkins Groovy pipeline? [Advanced]
- How do you handle errors and retries in a Groovy pipeline (try/catch, retry, timeout)? [Advanced]
- What is the difference between sed -i and sed without -i? [Advanced]
- How do you run stages in parallel in a Jenkins Groovy pipeline? [Advanced]