jedatt01
jedatt01

Reputation: 43

Crontab can't execute command in script

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

Upvotes: 1

Views: 831

Answers (1)

Greg A. Woods
Greg A. Woods

Reputation: 2792

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.)

Upvotes: 1

Related Questions