Crontab not running python script, no errors no nothing

5,305

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
Share:
5,305

Related videos on Youtube

kkonrad
Author by

kkonrad

Updated on September 18, 2022

Comments

  • kkonrad
    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
      AFH over 6 years
      Is /root/anaconda3/bin/python the python script file? This is confusing, since python 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 the crontab 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
      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
      AFH over 6 years
      Sorry, 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. Add 2>&1 to the end of the python call to see any errors.
    • kkonrad
      kkonrad over 6 years
      No 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
      AFH over 6 years
      I didn't realise that the version was written to stderr: like you, I had assumed it would be on stdout, 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
      kkonrad over 6 years
      I'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
      Kamil Maciorowski over 6 years
      You 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.