Linux Bash IF conditions not processing when running as crontab

bashcronlinux

So I've made myself a little script which monitors my served directories for any PHP files which have changed. It's designed to run every 5 mins from crontab and email me any changes so that I can check them.

There are lots of websites running on the machine and I want to be able to cast an eye over any new scripts to make sure that there is nothing there which I don't like the look of, if you know what I mean.

The problem is that the script runs 100% fine when I run it from the command line, but it does not run correctly from crontab. The script is included below:

rm /root/sec/email
find /var/www/ -name '*.php' -not -name '*.tpl.*' -type f -mtime -0.005 -exec ls -al {} \;  > /root/sec/email
if [[ -s /root/sec/email ]] ; then
        mail -s "PHP Change Alert on FSE4" matt@aroxo.com muji@aroxo.com < /root/sec/email
        echo "It ran" >> /root/sec/log
else
        echo "It did not run" >> /root/sec/log
fi ;

The issue is with the IF condition. Even if the file called "email" has a non-zero size, the wrong side of the if clause triggers (the else).

Any ideas what I'm doing wrong?

Cheers,

Matt.

Best Answer

The script has a bashism in it: [[

In other words, the script is using a non-standard extension to Bourne shell syntax which breaks the script when it's run by /bin/sh (I assume your distro uses a /bin/sh that doesn't have support for all of the non-POSIX bashisms in it).

To solve this issue, either

  1. Put #!/bin/bash in as the first line of the script
  2. Make the if condition look like this: if [ -s "/root/sec/email" ] ; then

Some more information about bashisms are here: http://mywiki.wooledge.org/Bashism