简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM