Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crontab: python script being run but does not execute OS Commands

I have this crontab configuration setup and the following script.

MAILTO="[email protected]"
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 as this

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 has begun running but other parts of the script are not finished. I am not sure if its OS fault or cron fault or python. The script by itself runs fine when I invoke it from command line.

Does anyone know why is this happening?

like image 208
Nick Avatar asked Aug 27 '13 22:08

Nick


1 Answers

You need shell to run cd command. 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="[email protected]" 
# m h 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 shell as subprocess in python.

like image 115
eri Avatar answered Sep 18 '22 16:09

eri