0

I have a script which I desire to run once a day, now i can achieve this with simpletime.sleep(60*60*24), then usingnohup python ...

Now i am not sure, what constraints time.sleep function would have on CPU ?

Other approach would using cron job ?

2 Answers 2

1

"sleep" has no impact on the cpu.

But cron job is a better approach for many reasons :

  • if your computer restarts, you don't have to relaunch the script manually
  • a long life process will more likely reach a border case making it crash (such as memory leak)
  • while sleeping, process is still consuming resources, especially RAM, but also file descriptors
Sign up to request clarification or add additional context in comments.

Comments

0

Cron is a much better solution as it becomes responsible for starting and stopping the script. Your other choice is to have your script working 24 hours a day plus some mechanism to re-start it if it locks up and start at reboot, etc.

Cron is way simpler and more dependable.

3 Comments

Hi tried the crobtab approach 00 00 * * * /Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/xx/xx/python/db.py But for some reason it didn't do anything
@ADFriend ... and you took the sleep out? How do you know it didn't do anything... it may have hit an error in the more restrictive cron enviornment. You should be able to grep CRON /var/log/syslog (but sometimes cron is in a different file under /var/log so you have to search). You could also do 00 00 * * * /Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/xx/xx/python/db.py >>/var/log/db.py.log 2>&1 to see if you are getting an error. It would be a bit different if you are doing it in the user cron instead of the system cron.
@tdelaney its actually working on my server, which is linux machine.. its not working locally on my mac though.. but again locally i was just testing the corntab.. what matters is server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.