0

I have a python script, which works when I run:

python my_script.py --start_time 2020-09-18-09-00 --end_time 2020-09-18-19-00 >> log_file.log 2>&1

I am trying to run in crontab:

14 16 * * * python my_script.py --start_time 2020-09-18-09-00 --end_time 2020-09-18-19-00 >> log_file.log 2>&1

but it fails - It says that I have a SyntaxError in the python script (which didn`t happen while I ran the python script without crontab).

I guess that I have a mistake in the syntax of the crontab command.

I would appreciate any help.

theletz
  • 1,713
  • 2
  • 16
  • 22
  • it is likely that your python version is different from the python in system default path. please provide the output of `python --version` and `whereis python` – minhhn2910 Oct 01 '20 at 13:44
  • modify and use full path (use the real locations): `14 16 * * * /usr/bin/python /path/to_my_script/my_script.py --start_time 2020-09-18-09-00 --end_time 2020-09-18-19-00 >> log_file.log 2>&1 ` – balderman Oct 01 '20 at 13:45
  • also read this: https://unix.stackexchange.com/questions/176226/passing-a-command-with-arguments-to-a-script . make sure your argument is read correctly in the python script launched by crontab by printing it out. – minhhn2910 Oct 01 '20 at 13:47
  • It works! thanks!! can you add this as an answer and I will accept it? such that it will help others as well – theletz Oct 01 '20 at 13:56

1 Answers1

0

The solution was:

my python version was different from the python in system default path.

for example, if the relevant python version for this script is /usr/bin/python, you have to modify the command to be:

14 16 * * * /usr/bin/python /path/to_my_script/my_script.py --start_time 2020-09-18-09-00 --end_time 2020-09-18-19-00 >> log_file.log 2>&1
theletz
  • 1,713
  • 2
  • 16
  • 22