Interview Prep/Shell Scripting/Shells, scripts and execution

What does `exec` do in a script? Give two uses.

AdvancedConceptShells, scripts and execution
Answer

exec cmd replaces the current shell process with cmd: same PID, no return. exec with only redirections changes the shell's own file descriptors.

# 1. Container entrypoint: the app becomes PID 1 and receives SIGTERM directly
exec gunicorn app:app --bind 0.0.0.0:8000

# 2. Send all further output of the script to a log file and the terminal
exec > >(tee -a /var/log/deploy.log) 2>&1

Without exec in an entrypoint, the shell stays PID 1 and does not forward signals, so docker stop waits 10 seconds and kills it.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Shells, scripts and execution