AWS not working working from Cronjob

2019-02-21 09:52发布

So I have a script to download a file from AWS daily and append it to a spread sheet. To do this I have set up a cronjob.

The Script works fine when I run it manually, but does not work when running from the cronjob.

The code has a line:

aws s3 cp s3://My/files/backup/ ~/home/AnPoc/ --recursive --exclude "*.tgz" --include "*results.tgz"

And in the email I recieve from the cronjob execution, I see the following error message:

./AnPoc/DayProcessing.sh: line 14: aws: command not found

I don't know why the command is not being found. Any help would be great.

4条回答
Root(大扎)
2楼-- · 2019-02-21 10:00

The only thing that worked for me was to specify the path explicitly in the script:

ROOTDIR=/home/myusername
LOGDIR=$ROOTDIR/logs
DUMPDIR=$ROOTDIR/db_backup
LOGFILE=$LOGDIR/db_backup.log

$ROOTDIR/.local/bin/aws s3 cp $DUMPDIR/myappname-`date +"%Y-%m-%d"` s3://my-bucket-name/backups/myappname-`date +"%Y-%m-%d"` --recursive >> $LOGFILE 2>&1

As a previous poster said, use which aws to find the location of aws.

查看更多
欢心
3楼-- · 2019-02-21 10:07

Put this code before your command line to be executed into crontab -e

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
查看更多
淡お忘
4楼-- · 2019-02-21 10:14

You should use the full path for the "aws" command. For example, /usr/local/bin/aws

查看更多
该账号已被封号
5楼-- · 2019-02-21 10:23

First: check where on your system the executable aws is stored. Use this command:

$ which aws
/usr/bin/aws # example output, can differ in your system

Now, place a variable called $PATH in your crontab before the script:

PATH=/usr/bin:/usr/local/bin

Those paths separated by : define where should be search for the exectable. In the example above it's /usr/bin. You have to check all executables in your cron job that they are available.

Another thing: try to avoid path with a tilde (~) in cronjobs. Use /home/user instead.

查看更多
登录 后发表回答