Skip to Content
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

readonly LOG=/var/log/myjob.log
log() { printf '%s %s\n' "$(date '+%F %T')" "$*" | tee -a "$LOG"; }
die() { log "ERROR: $*"; exit 1; }
trap 'die "failed at line $LINENO"' ERR

main() {
  [[ $EUID -eq 0 ]] || die "run as root"
  log "start"
  # work here
  log "done"
}
main "$@"
  • -e stop on error, -u error on unset variables, -o pipefail catch failures inside pipes
  • Check scripts with shellcheck — it finds most bugs before they run
Rating
0 0

There are no comments for now.

to be the first to leave a comment.