Send a mail with body Failure to root only when check_job fails.
Which command list is appropriate?
Use || for failure-only notification and pipe the message into mail; -s supplies the subject.
Detailed explanation
check_job && printf '%s\n' Failure | mail -s Alert rootIncorrect. This sends mail after a successful check.
Incorrect. This sends mail after a successful check.
check_job || printf '%s\n' Failure | mail -s Alert rootCorrect. The notification pipeline is evaluated only when check_job fails.
Correct. The notification pipeline is evaluated only when check_job fails.
check_job ; printf '%s\n' Failure | mail -s Alert rootIncorrect. ; sends mail regardless of the check result.
Incorrect. ; sends mail regardless of the check result.
check_job | mail -s Alert rootIncorrect. It always pipes check_job output and does not create a Failure-only message.
Incorrect. It always pipes check_job output and does not create a Failure-only message.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'notify(){ cat >/dev/null; printf mailed; }; false || printf Failure | notify'Expected result
mailedKey points
- Failure-only with ||
- mail reads standard input
- -s sets subject
Notes
- Environment: GNU Bash 5.2 / mailは関数で安全に代替検証
- 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.