Linux – Crontab not running python script, no errors no nothing

bashcrontablinuxpythonscript

Already spent over 1h to make that easy thing and totally failed :/

Cant find why that python not working on crontab while it works perfect in commandline…

Script is (bash):

#!/bin/bash

touch before_zzz_text.txt # to check if cron works at all 
ls > "before_zzz_text.txt" # just to check if I'm in the correct directory

/root/anaconda3/bin/python -V > pv.txt # this is empty! or a white char

touch after_zzz_text.txt # this works new file every minute

This way I know it runs in cron (files .txt both created every minute – like cron runs every minute).

However pv.txt is empty … so looks like bash script not working ?

In the end I want more complicated script to run in the bash script but I tried to dig why it's not working so to simplify it is now "/root/anaconda3/bin/python -V'

Best Answer

After a bit of discussion (see the comments above), it seems that the basic problem is that python writes its version text to stderr, not the expected stdout, where nothing is written, hence the empty file.

In general, when diagnosing crontab problems, it is a good idea to log errors as well as output, to the same or a different file. By adding 2>&1 to the end of the python invocation line, the version text appeared in pv.txt:

/root/anaconda3/bin/python -V > pv.txt 2>&1
Related Question