Crontab not running python script, no errors no nothing
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 videos on Youtube
kkonrad
Updated on September 18, 2022Comments
-
kkonrad over 1 year
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'
-
AFH over 6 yearsIs
/root/anaconda3/bin/python
the python script file? This is confusing, sincepython
is the handler you want to run. Does it start with#!/usr/bin/python
or similar? Does it and all its directory components have execute-all permissions? This is important, as thecrontab
environment is not the same as for your terminal. If the.txt
files were deleted beforehand, what permissions and user / group names were they created with? -
kkonrad over 6 years"/root/anaconda3/bin/python" is path for python interpreter. It was just "python" before but I though it cannot find the right env path or sth. Anyway with just "python -V > pv.txt" does not work either. Also there is no error messages , file pv.txt ic reated empty ... Should it be like that? "python -V should simply give python version and ">pv.txt" should store that message in that file, which comes empty in the end ...
-
AFH over 6 yearsSorry, I was confused because
/root/anaconda3/bin/
is an unlikely location for the interpreter. An empty log file will be produced if an error prevents the interpreter from running. Add2>&1
to the end of thepython
call to see any errors. -
kkonrad over 6 yearsNo problem got the fix finally! Seems the missing MTA on ubuntu server was messing around. After adding "2>&1" to the python line it works!!!!
-
AFH over 6 yearsI didn't realise that the version was written to
stderr
: like you, I had assumed it would be onstdout
, but I've just confirmed this on my own system. Glad you're in business. I think I'll submit an answer, for the benefit of other users of the site. -
kkonrad over 6 yearsI'm still not sure it is, but somehow it works after adding the "2>&1" part. This is more like solution when MTA is not installed I think.
-
Kamil Maciorowski over 6 yearsYou shouldn't place an answer inside a question. If you think the existing answer is not enough then add your own answer. Use this link if you want to copy-paste your "answer" to an actual answer.
-