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.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat is the difference between a login, interactive and non-interactive shell, and which startup files run?Intermediate