Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Here's my code for runLucene.py:

#!/usr/bin/env python

import os
from extras.download_datos_desambiguar import news_Lucene

x=datetime.today()
y=x.replace(day=x.day, hour=09, minute=00, second=0, microsecond=0)
delta_t=y-x

secs=delta_t.seconds+1

def fourdlife():
    print "checking function"
    os.system("~/code/4dlife_repo/4dbatch/src/engines/extras/download_datos_desambiguar/news_Lucene.py")



t = Timer(secs, fourdlife)
t.start()
print "timer started"

I am running this code in my ProcesosContinuous.py file like this:

while True:
            os.system("./runl.sh")
            #some other processes

where runl.sh is

python ~/code/4dlife_repo/4dbatch/src/engines/extras/download_datos_desambiguar/news_Lucene.py

This python code is always running on my apache2 server.

However, this is working at any given hour and not just the specified hour. What am I doing wrong? Also, I feel there is a much better way to do this. I looked at the cron task but that's not what I am looking for. I don't want my program to go to sleep() because I need the other processes to run after the runl.sh process. What is the best way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The command crontab -e will open up an interactive editor within which you can define a cron job.

Within that editor's buffer, you can then enter a line such as the following (and then save it):

00 00 * * * /path/to/script.sh

This command will run script.sh every midnight.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...