0

I have a python file with this content:

from datetime import datetime

print(datetime.today())

If I run this from terminal with python3 file.py > out.txt I will get the out.txt file with todays date.

I want to replicate this with cron and schedule this script to run every minute. So the crontab looks like:

* * * * * /path/to/python3 /path/to/file.py >> path/to/out.txt

Now this works and out.txt does get created but the file is empty and never contains any date. Why is that?

The duplicate mentioned here does not solve my issue. I have tried this with just a simple echo command scheduled every minute with cron tab and it worked.

Somehow this is a python issue. Python is hiding the output. Or there are some other permission issues.

bcsta
  • 1,963
  • 3
  • 22
  • 61

1 Answers1

1

You need to include 2>&1 at the end of the crontab specified in order to append the printed value to the file, so your crontab should look like:

* * * * * /path/to/python3 /path/to/file.py >> path/to/out.txt 2>&1
magnusnn
  • 137
  • 1
  • 13
  • Tried this. Still no output – bcsta Dec 16 '20 at 12:09
  • The file will now contain an actual error message you can examine. See the duplicate for (lots) more. – tripleee Dec 16 '20 at 12:10
  • The mentioned duplicate applies to cron jobs that are not running. I know mine is running because I see the file being created on the first minute and I have tried other tests that work (echoing every minute to file) its this specific python case that is not working. – bcsta Dec 16 '20 at 12:23
  • My mistake this actually does return an error inside the out.txt. cron does not have permission to access python script. – bcsta Dec 16 '20 at 12:56