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

2024/9/25 10:25:25

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, strftimedef 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?

Answer

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.

https://en.xdnf.cn/q/71589.html

Related Q&A

concatenating arrays in python like matlab without knowing the size of the output array

I am trying to concatenate arrays in python similar to matlab array1= zeros(3,500); array2=ones(3,700); array=[array1, array2];I did the following in python:array1=np.zeros((3,500)) array2=np.ones((3,7…

How to save the result of a comparison using Djangos with template tag?

I would like to create new variable in django template, which will have a value of comparison obj.site.profile.default_role == objUnfortunately none of this code works: {% with obj.site.profile.default…

How to merge two list of dictionaries based on a value

I have two lists of dictionaries, lets say: a = [{id: 1, name: a}] b = [{id: 1, city: b}]I want to have a list that merges every dictionary in both lists with the same ID. In this example i expect to h…

How can I format strings to query with mysqldb in Python?

How do I do this correctly:I want to do a query like this:query = """SELECT * FROM sometable order by %s %s limit %s, %s;""" conn = app_globals.pool.connection() cur = con…

Doing many iterations of curve_fit in one go for piecewise function

Im trying to perform what are many iterations of Scipys curve_fit at once in order to avoid loops and therefore increase speed.This is very similar to this problem, which was solved. However, the fact …

python - dictionary iterator for pool map

I am handling set of frozensets. I am trying to find minimal sets for each frozenset in the dictionary output. I have 70k frozensets, so i am making chunk of this frozenset dictionary and parallelizing…

How to get SciPy.integrate.odeint to stop when path is closed?

edit: Its been five years, has SciPy.integrate.odeint learned to stop yet?The script below integrates magnetic field lines around closed paths and stops when it returns to original value within some t…

High-dimensional data structure in Python

What is best way to store and analyze high-dimensional date in python? I like Pandas DataFrame and Panel where I can easily manipulate the axis. Now I have a hyper-cube (dim >=4) of data. I have be…

How to access top five Google result links using Beautifulsoup

I want to access the top five(or any specified number) of links of results from Google. Through research, I found and modified the following code.import requests from bs4 import BeautifulSoup import re…

Logging in a Framework

Imagine there is a framework which provides a method called logutils.set_up() which sets up the logging according to some config.Setting up the logging should be done as early as possible since warning…