Cron jobs not running



  • I have the following command entered under 'cron'. It's designed to download an ad-blocking host file, and update over the previous version if it's found to be newer. It works flawlessly when run as a block command in the shell, but doesn't seem to be running as a cron job i.e. my file "/etc/extra-blocks" isn't updating as it should.

    Under 'cron', the command is as follows:

    /bin/sh -c 'FILE="/etc/extra-blocks" ; /usr/bin/fetch -q -o ${FILE}.new http://someonewhocares.org/hosts/hosts && if [ -s ${FILE}.new ]; then if [ -f ${FILE} ]; then /bin/mv ${FILE} ${FILE}.old ; fi && /bin/mv ${FILE}.new ${FILE} && /usr/local/sbin/pfSctl -c "service reload dns" && if [ -f ${FILE}.old ]; then /bin/rm -f ${FILE}.old ; fi ; fi'

    The cron is scheduled as:
    5  0  *  *  6  root

    Which I think indicates minutes past midnight, every saturday night.


  • Moderator

    You should check out pfBlockerNG which has DNSBL integrated into the GUI…

    https://forum.pfsense.org/index.php?topic=102470.0



  • @BBcan177:

    You should check out pfBlockerNG which has DNSBL integrated into the GUI…

    https://forum.pfsense.org/index.php?topic=102470.0

    Thanks for the pointer - but hosts files are so resource light :)

    How might I convert the code to a shell script ("update-adblock.sh") which I can then ask cron to run directly?



  • I finally got the above scripts running as shell scripts, then using cron to call the script itself. So easy and so basic, it's embarrassing to be posting it to such a learned forum, especially after all these years. :)

    The main motivation to finally learn how to script this came via adapting scripts to automate ACME's Let's Encrypt SSL certs into my UniFi controller, which I have running on my pfSense box (https://community.ubnt.com/t5/UniFi-Wireless/Unifi-Cloud-key-certificate-installation/m-p/2437833#M312944)

    Anyway, the basic script to automate adblock hosts file updates is:

    #!/bin/sh
    
    FILE="/etc/adblock"
    /usr/bin/fetch -q -o ${FILE}.new http://winhelp2002.mvps.org/hosts.txt &&
    if [ -s ${FILE}.new ]; then if [ -f ${FILE} ] ; then /bin/mv ${FILE} ${FILE}.old ; fi &&
    /bin/mv ${FILE}.new ${FILE} &&
    /usr/local/sbin/pfSctl -c "service reload dns" &&
    if [ -f ${FILE}.old ]; then /bin/rm -f ${FILE}.old ; fi ; fi
    

    Obviously the file name (FILE) and download source can be easily varied according to need. Simply make the script executable (chmod +x) and then point cron to run it whenever you want.


Log in to reply