1

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'

7
  • 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?
    – AFH
    Commented Nov 6, 2017 at 23:03
  • "/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 ...
    – kkonrad
    Commented Nov 6, 2017 at 23:08
  • 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.
    – AFH
    Commented Nov 6, 2017 at 23:29
  • 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!!!!
    – kkonrad
    Commented Nov 6, 2017 at 23:40
  • 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.
    – AFH
    Commented Nov 6, 2017 at 23:54

1 Answer 1

3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .