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.pid
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 purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]