bash - How to test existence of a program in crontab? -
i made bash script test if utility named 'myutility' exists:
#!/bin/bash #if hash myutility >/dev/null 2>&1; if hash myutility 2>/dev/null; echo "branch true" exit 0 else echo "branch false" exit 1 fi
on command line,
$ ./test.sh branch true
-- seems work. if add crontab:
$ crontab -l ## test * * * * * /users/meng/bin/test.sh > "/users/meng/log.txt"
i got
$ more /users/meng/log.txt branch false
so, somehow when script run crontab, hash myutility >/dev/null 2>&1
part not work. idea?
knowing when, , why, cronjobs fail can difficult. solution use wrapper script archive successful cron job runs time , send email admin staff when fails.
both successful , failed runs leave trace file disk can read understand job did , when. @ failure trace sent admin staff, not need login server initial idea should done if anything. notice when things fail best read notification email bottom top.
#!/bin/bash # # description of script. # # <begin not touch header> # default settings. # trap err , pipefail bashisms, not change shebang. job_name="${0##*/}" set -e trap 'echo "${job_name}: exit on error"; exit 1' err set -u set -o pipefail # report errors email when shell not interactive. if [ -t 1 ]; # terminal exists, user running script manually. # not initiate error reporting. trap 'echo "${0}: exit on error"; exit 1' err else outputfile=$(mktemp "/tmp/${job_name}.xxxxxxxxx") exec > "${outputfile}" 2>&1 set -x mailto="system-admin-maillist@example.com" cronjoblogdir="/tmp/${job_name}" server=$(hostname -s) timestamp=$(date --iso=ns) trap "cat "${outputfile}" | mailx -s \"${server}: ${job_name} failed\" ${mailto}" err trap "mv \"${outputfile}\" \"${cronjoblogdir}/${job_name}.${timestamp}\"" 0 if [ ! -d "${cronjoblogdir}" ]; mkdir -p "${cronjoblogdir}"; fi find "${cronjoblogdir}" -name "${job_name}.*" -type f -mtime +7 -delete fi # </end not touch header> # # write script here # exit 0 # eof
Comments
Post a Comment