Bash scripts start, monitor, and stop processes. Know signals, PIDs, and foreground/background control before managing servers.
Listing processes
ps aux | head
pgrep -l bash
pid=$$
echo "this shell pid=$pid"$$ is current shell PID—useful for unique temp files.
Signals
# SIGTERM (15) polite stop; SIGKILL (9) force
kill 12345
kill -9 12345Try SIGTERM first; SIGKILL cannot be caught—last resort for stuck processes.
nohup
nohup long-task.sh > task.log 2>&1 &
disownnohup keeps running after logout—still prefer systemd for production services.
Important interview questions and answers
- Q: SIGTERM vs SIGKILL?
A: TERM allows cleanup; KILL forces immediate termination. - Q: What is $$?
A: Current shell process ID.
Self-check
- Which signal number is SIGKILL?
- What does nohup help with?
Tip: Prefer systemd units over nohup for production daemons.
Interview prep
- $$?
PID of current shell.
- SIGTERM vs SIGKILL?
TERM allows graceful stop; KILL forces immediate termination.