Ensure a cron job can find commands in /usr/local/bin even though cron's environment differs from an interactive shell.
Which crontab setting is appropriate?
Set environment variables in a crontab with NAME=value, or use absolute command paths.
Detailed explanation
PATH=/usr/local/bin:/usr/bin:/binCorrect. This sets PATH in the cron environment.
Correct. This sets PATH in the cron environment.
export PATH=/usr/local/bin:/usr/bin:/binIncorrect. A crontab environment line is not a shell export statement.
Incorrect. A crontab environment line is not a shell export statement.
path /usr/local/bin:/usr/bin:/binIncorrect. path is not a crontab variable assignment.
Incorrect. path is not a crontab variable assignment.
SHELL=/usr/local/bin:/usr/bin:/binIncorrect. SHELL selects the interpreter, not the command search path.
Incorrect. SHELL selects the interpreter, not the command search path.
Try it yourself
An example you can run in a temporary verification environment.
line='PATH=/usr/local/bin:/usr/bin:/bin'; name=${line%%=*}; value=${line#*=}; printf '%s=%s\n' "$name" "$value"Expected result
PATH=/usr/local/bin:/usr/bin:/binKey points
- NAME=value syntax
- Do not assume interactive shell
- Absolute paths are another option
Notes
- Environment: POSIXシェル / 合成crontab環境行
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.