0

I'm trying to schedule this script to run every day by using crontb :

#/bin/bash
cd /home/banana/gdrive
DATE=`date +%Y-%m-%d`
mongodump  --gzip --archive=$DATE
./gdrive upload ./$DATE

olddate="$(date "+%Y-%m-%d" -d "9 days ago")"
old2=$(date -d $olddate +%s)
#echo $old2
list="$(./gdrive list)"
#echo $list
ADDR=(${list// / })
ii=0
for i in "${ADDR[@]:5}"; do
    if [ $(($ii % 7)) -eq 0 ]; then
       id=$i
    fi
    if [ $(($ii % 7)) -eq 1 ]; then
       name=$i
       dat=$(date -d $name +%s)
       #echo $dat
       if [ $old2 -ge $dat ]; then
          echo "delete"
          echo $id
          touch $id
          ./gdrive delete $id
       fi
       #echo "name"
       #echo $name
       #echo $id
    fi
    #echo $ii
    #echo $i
    let ii=ii+1
done

crontab entry :

0 2 * * * /home/banana/gdrive/upload.sh

the script runs fine if lunched in a bash console, if it's run by crontab it doesn't reach the end even if i'm not able to find any logged error anywhere.

By simulating the use of the crontab environment as suggested here : https://serverfault.com/questions/85893/running-a-cron-job-manually-and-immediately/85906

i found out that the execution result in an error:

gdrive/upload.sh: 12: gdrive/upload.sh: Syntax error: "(" unexpected

given the fact that the SHELL variable in the crontab environment was set to SHELL=/bin/sh and that the syntax there is bash this sound quite reasonable (even if i'm still confusing on the role of the #/bin/bash on the first line of the script).

The problem is that the same outcome still even after putting SHELL=/bin/bash in the crontab file.

So the question is, what's wrong in this setup ? and how can i schedule this script by using crontab ?

3

The shebang that needs to be written includes an exclamation mark.

Try to write #!/bin/bash at the first line of your shell script.

  • yes, replacing #/bin/bash with #!/bin/bash solve the error – bn89 Oct 3 '16 at 10:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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