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

Preparing 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

← All Scripting (Bash, Groovy) questions