Anton
Anton

Reputation: 4815

cronjob can't find file on EC2

I have a script, called script.py that is on an EC2 instance. The script reads a file in the same folder (code: open(data.csv, 'r')) called data.csv. Here is the cron job:

* * * * * /home/ubuntu/anaconda3/bin/python /home/ubuntu/project/script.py > /var/log/myjob.log 2&>1

When I run the script myself python script.py is works perfectly. However, when cron runs the script, a python error is printed (as I intended) in the myjob.log file:

[Errno 2] No such file or directory: 'data.csv'

I suspect cronjob doesn't run the script in the same directory as when I run it myself, however, I do not know how to write the crontab line to tell cron to run the script from the directory that the script is in.

Upvotes: 1

Views: 307

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179084

Assuming /home/ubuntu...

* * * * * cd /home/ubuntu/ && ./anaconda3/bin/python ./project/script.py > /var/log/myjob.log 2>&1

Adding 2>&1 to the end is unrelated to your question, but captures the STDERR output into your log, which you probably also want.

Upvotes: 2

Related Questions