I scheduled following cron job:
root@alexus:~# crontab -l | grep ^\@hourly
@hourly OUT=`/usr/lib/nagios/plugins/check_disk --include-type=ext4 --warning=10% --critical=5%` ; if [ $? != 0 ] ; then echo $OUT ; fi
root@alexus:~# OUT=`/usr/lib/nagios/plugins/check_disk --include-type=ext4 --warning=10% --critical=5%` ; if [ $? != 0 ] ; then echo $OUT ; fi
root@alexus:~# cat /etc/issue.net
Ubuntu 14.04.3 LTS
root@alexus:~# and even though I'm able to run EXACTLY same one liner within shell, whenever job gets executed via cron, I get following email:
Subject: Cron OUT=`/usr/lib/nagios/plugins/check_disk --include-type=ext4 --warning=10 /bin/sh: 1: Syntax error: EOF in backquote substitution
per subject line, it seems like everything after % sign is missing.
How do I properly escape it without breaking my script?
11 Answer
You'll need to check your crontab(5) man page. Some implementations of cron will use % as a newline in the command field, so you can pass data to the command on stdin
* * * * * >$HOME/cron.cat.out cat%hello%worldThen "cron.cat.out" contains 2 lines:
hello
worldYou'll need
- to escape your percent signs
- I recommend using
$()instead of backticks - Quote the
"$OUT"-- always quote your variables unless you need the specific side-effects of leaving them unquoted.
@hourly OUT=$(/usr/lib/nagios/plugins/check_disk --include-type=ext4 --warning=10\% --critical=5\%) || echo "$OUT"
# ...................................................................................^..............^