1

I have shell script i've written that deletes the oldest logfile in a directory when the mount point reaches 90% capacity. When I run the script manually it works fine but when I attempt to use crontab to run it cannot seem to execute the actual rm command but it executes everything else in the script. See my crontab and script below.

0 * * * *  /acsmgmt/iselogs/iselogcleanup.sh

#!/bin/bash
df -H | grep /acsmgmt | awk '{ print $4 " " $5 }' | while read output;
do
    #!echo $output
    usep=$(echo $output | awk '{ print $1 }' | cut -d '%' -f1)
    #!echo $usep

    if [ $usep -ge 90 ]; then
            echo $(date) "Logs cleaned up" >> /tmp/isecleanup.log
            rm -v `ls /acsmgmt/iselogs -rt | grep "iselog-" | head -1` >> /tmp/isecleanup.log
    else
            echo $(date) "No logs to clean up" >> /tmp/isecleanup.log
    fi
done
6
  • 1
    Is the script producing any errors when running under cron? You may want to redirect stdout/stderr for the script to a file using something like 0 * * * * /acsmgmt/iselogs/iselogcleanup.sh > /tmp/errors 2>&1, and then look at that file after the script runs.
    – larsks
    Apr 21, 2015 at 14:27
  • Just tried that. No output is showing up in the errors file. Cron can execute everything in the script except this statement rm -v ls /acsmgmt/iselogs -rt | grep "iselog-" | head -1 >> /tmp/isecleanup.log. All my echos are showing up
    – jedatt01
    Apr 21, 2015 at 14:32
  • Perhaps try iselog=$(ls /acsmgmt/iselogs -rt | grep "iselog-" | head -1) then do rm -v $iselog >> /tmp/isecleanup.log
    – l'L'l
    Apr 21, 2015 at 14:35
  • If have that ls pipeline output to the file do you see the correct output in the log? Apr 21, 2015 at 14:54
  • Got it working using I'L'I's idea with a few modifications. Cron couldn't find head so I head to use absolute path. Also I had to use append absolute path to the $iselog variable so rm knew were it was.
    – jedatt01
    Apr 21, 2015 at 15:12

1 Answer 1

1

So, the answer is indeed, as I suspected, to always make sure you specify a correct and complete PATH variable in any script called by cron.

(I keep making this same mistake myself, even after years of writing cron scripts -- some versions of cron allow you to specify a default PATH (and other environment variables) for all your scripts, and this can help, but it also needs careful maintenance.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.