1

I would like to get help regarding Crontab on Centos 7.

I Need to create the scheduler to run python script (which have 2-5 addition import in the same folder) by using Crontabs

Currently, my crontab looks like this I have

PATH = /srv/git/XXX
30 * * * * cd $PATH/XXX/XXXX  python $PATH/XXX/XXXX/upload.py

I have tried to use a path to the python folder and so on, nothing works. Then I found that the best way is to use .sh files.

WHAT I NEED: Currently, I'm looking to for the best way to run crontab scheduler, for python script(which have an extra import in the same folder) with PROD and DEV variables for future testings. Any better way instead of .sh files? and is .sh is the best way, what if the clean way to write them?

1 Answers1

0

I would create a *.sh (e.g. dum.sh) file like this:

#!/bin/bash
export PATH = /srv/git/XXX
python $PATH/XXX/XXXX/upload.py

and in my Crontab file use:

30 * * * * /.../dum.sh
msi_gerva
  • 2,021
  • 3
  • 22
  • 28
  • Sorry for not mention the issue, I have specified that each python script has an extra import, and usually when I run the script directly, imports do not pick up automatically –  Sep 26 '18 at 09:02
  • You should be able to set the directory-tree that the Python sees. Basically, the folders where to seek, when importing a module. For example, `import sys;sys.path.append(path_1);sys.path.append(path_2)`; Afterwards your Python session should be able to see the modules in path_1 or path_2. – msi_gerva Sep 26 '18 at 09:15
  • there is no cleaner way to set configuration, but keep python script in the format (import extra_module) ??? –  Sep 26 '18 at 09:17
  • I do not quite understand you, but in Python 2, there was module `user`, which basically ran script `.pythonrc` where-ever it was located in the system (I had it under $HOME/ dierctory). Nevertheless, I have a feeling your question/problem is growing outside the scope of your initial question - how to run the python script with crontab. – msi_gerva Sep 26 '18 at 09:26
  • Currently, all what I need is to run python script (with extra imports) with PROD and DEV variables which allow running different script based on the variable. –  Sep 26 '18 at 09:30
  • FIxed it by using cronwrap –  Sep 26 '18 at 09:58