Crontab: python script executes but does not execute OS commands

I have this crontab configuration setting and the following script.

MAILTO=" abc@avc.com " 41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py > /home/atweb/Documents/opengrok/restart_log.txt 2&>1 

And the python script is like

 import subprocess import os from time import gmtime, strftime def main(): print(strftime("%a, %d %b %Y %X +0000", gmtime())) print('Running opengrok index..') subprocess.call(["cd", "/home/atweb/Documents/opengrok"]) subprocess.call(["./stop_website"]) print('Stopped website...') subprocess.call(["./index_opengrok"]) print('finished indexing...') subprocess.call(["./setup_opengrok"]) print('setup finished...') subprocess.call(["./start_website"]) print('Finished opengrok index..') if __name__ =='__main__':main() 

And this is the output log

 Tue, 27 Aug 2013 22:41:01 +0000 Running opengrok index.. 

For some reason, the script started working, but the other parts of the script are not finished. I am not sure if its an OS error or a cron or python error. The script itself works fine when I call it from the command line.

Does anyone know why this is happening?

+4
source share
2 answers

You need a shell to run the cd . In your crontab, define sh or bash as SHELL.

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin MAILTO=" abc@avc.com " # mh dom mon dow command 41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py > /home/atweb/Documents/opengrok/restart_log.txt 2&>1 

Or open a shell as a subprocess in python.

+4
source

Two things: your cd set the directory for this subprocess, which exits immediately:

 subprocess.call(["cd", "/home/atweb/Documents/opengrok"]) 

In other words, this is an empty step.

The following subprocess knows nothing about the previous environment:

 subprocess.call(["./stop_website"]) 

... therefore he cannot work. If you want all your programs to run in this directory, use:

 os.chdir("/home/atweb/Documents/opengrok") 

before any of the lines subprocess.call() .

+1
source

Source: https://habr.com/ru/post/1499255/


All Articles