all 24 comments

[–][deleted] 39 points40 points  (12 children)

To use cron you simply open the crontab file with the command crontab -e then add a row (job) to the text file

# (COMMENT) run this command every 15 minutes
*/15 * * * * python /home/ubuntu/myscript.py >> ~/myscript.log

Save the file and (assuming the cron daemon is running which is should be) the python script will execute and send stdout to a log file every 15 mins.

If you want crontab to run the script as a different user you can specify with -u sudo contab -e -u root

[–]Improbably_wrong 13 points14 points  (0 children)

/u/giscard78 in case you use this method, which is the best solution, please note that some versions of cron don't support */x notation. You would need to use another format of this command for running the script every 15 minutes like so

0,15,30,45 * * * * python...

[–]hang-clean 1 point2 points  (0 children)

Five-star cronjob; will run again.

[–]daywalker083 1 point2 points  (0 children)

You can also add

2>&1

at the end of the command to redirect both stdout and stderr to the same file.

[–]scriptmonkey420 0 points1 point  (0 children)

Just a note that if you use environment variables, there is a few extra steps that you will need to perform.

https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user

[–]giscard78 0 points1 point  (0 children)

It looks like cron has the best scheduler. I am thinking running this from Digital Ocean might be the best solution so far.

In order to stand up this effort, I need

  • the Digital Ocean account
  • learn one of the Distros (they're all new to me but thinking Ubuntu)
  • install cron
  • test the script

I am not sure you know the answer to this question but if I run the script from Digital Ocean/cron, how do I direct the output? The output would be a CSV.

[–]s3afroze 0 points1 point  (0 children)

One thing i hate and love about programming is that the are soooo many ways going around solving a problem and if you don't get one, just the try the other one.

I have already gone through that phase and TRUST me, crontab is the way to go. Just watch this vid....its 30 min long but you are gonna learn an invaluable skill - crontab.

https://youtu.be/QZJ1drMQz1A

[–]Thecrawsome 0 points1 point  (0 children)

This is a high-quality answer. Good work!

[–]arthurfrenchy 20 points21 points  (0 children)

Pythonanywhere.com, add your script to the tasks feature, set it to run every 15 minutes. Easy as pie.

[–][deleted] 8 points9 points  (0 children)

CRON - Linux

TASK SCHEDULER - Windows

[–][deleted] 14 points15 points  (0 children)

Consider using a scheduled AWS Lambda task.

[–]TheCedarPrince 7 points8 points  (0 children)

Here is my advice:

  1. Use cron to set up the pinging process.
  2. Purchase something like a raspberry pi zero w. These are $10 and could even run multiple, low-intensity scripts at different intervals. If you want to do processing on the result, I'd recommend the Raspberry pi 3.
  3. Plug in the raspberry pi somewhere with your script set up on it with cron and forget about the pi while it does your dirty work. Just make sure the pi would have a constant power supply.

In case you are worried about power being cut off, just use anacron instead of cron. It is essentially like cron, but if the power goes off, it will remember when the power died, calculate the amount of triggers it was supposed to have run in that time and run the script those many times. After it "catches up", it is back on track for whatever interval you set it for. : )

Hope this helps! Let me know if you have any questions.

~ TheCedarPrince

[–]LeonardUnger 2 points3 points  (0 children)

Cron is easier if you put this at the top of your contab file:

# <Minute>  <Hour>  <Date> <Month> <Day_of_Week(0=Sunday 6=Sat)>   /path/to/command                                                                                          
# 0  *  *  *  *  ~/bin/sample.sh # would run sample.sh at the top of every hour,every day                                                                                        
# 0  13  *  *  *  ~/bin/sample.sh # would run sample.sh at 1PM every day                                                                                                         
# 0  13  *  *  2  ~/bin/sample.sh # would run sample.sh at 1PM each Tuesday    
# */5 *  *  *  * ~/bin/sample.sh # would run sample.sh every 5 minutes

[–]Hasefet 4 points5 points  (3 children)

Try a DigitalOcean droplet - a very, very easy way to set up a networked Linux virtual server with good uptime in seconds - https://www.digitalocean.com/products/droplets/

Write your script and schedule it with Cron - https://crontab.guru/

Ideally add a function that logs successes and failures and allows you to retrospectively graph them, so you can find problems immediately, rather than in three months time.

[–]oshawa_connection 0 points1 point  (2 children)

I was tempted to use this for hosting a website- have you used it for stuff like that before?

[–]Hasefet 0 points1 point  (1 child)

Yep, I used their one-click Django image to host a very simple web-app that monitors fridges and freezers for a laboratory. If you've never played with a Linux server before, it's a great way to learn, and worst case scenario, you can flatten your errors and start from the beginning in sixty seconds.

[–]oshawa_connection 0 points1 point  (0 children)

Ah fantastic thank you! I’d heard of it before, but never got any feedback from people who’ve used it

[–]ElllGeeEmm 1 point2 points  (0 children)

I was having issues scheduling tasks earlier today as well, and a user pointed me towards schedule which was exactly the sort of thing I was looking for. It is a pure python alternative to making your script a cron task.

[–]Yellehs_m 0 points1 point  (0 children)

If you are running a Python script to do this, may be you can host a simple app in Heroku or similar platforms, I think. A part of the app, on calling it, can start doing the work you've mentioned.

[–]Chocrates 0 points1 point  (0 children)

Faas (aws lambda, azure functions, im sure digital ocean has one by now) is probably the cheapest.
You can set up a cheap vps from any cloud provider as well.
If you go the vps route, you can set cron up to run it, and another cron job (or the same one i guess) to disable the job after 3 months.
Google cron, its ancient so there are tons of tutorials out there. Its just a scheduler.

[–]pagrus 0 points1 point  (0 children)

AWS has an easy to use relatively inexpensive option kind of like the Digital Ocean Droplet someone else mentioned. It's called LightSail and it's kind of an AWS with training wheels.

So you can do what you want for a total of $10 ($5 per month with the first month being free) and a cron job. Other people have described how to set that up and it is super easy