简体   繁体   中英

cron job - ack: command not found

I have a bash script that I've written to identify if a certain string exists in a file or not and then output the file name to "hasString.txt" of "noString.txt". I'm using ack -i 'mystring' 'searchDir' to find what I'm looking for.

The weird thing is that when I manually call the script through terminal it works perfectly, but when cron calls the script I get the following error message output:

~/sourceDir/script.sh: line 30: ack: command not found .

Why would it work when manually called but not be able to find the ack command when called by cron?


edit: adding relevant code and cron file

Lookout script - Determines if there are any files to process.

    if [[ $(ls -A ${PWD}/*.zip) ]]; then
        while [ $different -eq 1 ]; do
            du -h 1> $compare1
            ls -laR >> $compare1 
            sleep 25s
            du -h 1> $compare2
            ls -laR >> $compare2

            if cmp $compare1 $compare2 ; then   
                mkdir -p $LOGAREA
                mkdir -p $workarea/zip/unzip
                touch $LOG
                touch $ERRORLOG
                sleep 2s
                source ~/Desktop/Scripts/readfolderfiles.sh $drop
                mv *epub $workarea
                rm $compare1
                rm $compare2
                bash ~/Desktop/Scripts/Page_Label/script/searchString.sh
                different=0
            else
                echo
            fi
        done 1> $LOG 2> $ERRORLOG
    else
    rm ~/Desktop/page_labels.running
    rm $drop/page_labels.running
    fi 

Identify and report script - Generates the output mentioned at top

for files in *.zip; do
    #move and unzip the files
    mv $workarea/$files $workarea/zip/unzip/${files%.epub}.zip
    sleep 2s
    unzip zip/unzip/*.zip -d zip/unzip/${files%.epub}
    mv zip/unzip/*.zip zip
    sleep 2s
    cd $workarea/zip/unzip
    for dir in *; do
        # Search the files for the searchString
        if ack --ignore-case 'searchString' $dir; then
            echo $dir >> $drop/Has_searchString.txt
            echo
            rm -r $dir
            sleep 5s
        else
            echo $dir >> $drop/No_searchString.txt
            echo
            rm -r $dir
            sleep 5s
        fi
    done
    cd $workarea
done

Cron doc

#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

#### Backup Files
* * * * 1,2,3,4,5 bash  ~/Desktop/Scripts/searchScript.sh &> /dev/null;

It is because search path is not defined.

Use either full path in your script or define path

Option 1:

/path/to/ack -i 'mystring' 'searchdir'

Option 2:

PATH=/path/to/ackdir:$PATH
export PATH

ack -i 'mystring' 'searchdir'

You need to set PATH enviroment variable in your cron job. So add this on the top of the cron file:

 #!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

Note: also add #!/bin/bash, so you don't have to add shell enviroment variable too.

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