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.

2
  • 3
    Add the full path to the out.txt file. Commented Dec 16, 2020 at 11:50
  • that how it already is..my mistake for not including it in the question Commented Dec 16, 2020 at 12:07

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this. Still no output
The file will now contain an actual error message you can examine. See the duplicate for (lots) more.
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.
My mistake this actually does return an error inside the out.txt. cron does not have permission to access python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.