Forums

How do I run a python script from Schedule?

Need to run a little script hourly, exact instructions for a fool please!

  1. Save the script somewhere in your home directory. Make sure you know its path. Let's say it's called ~/scheduled-scripts/hourly.py for now (i.e. a script called hourly.py in a directory scheduled-scripts in your home directory).
  2. Click the Dashboard link in the top-right and click on the Schedule tab.
  3. Select Hourly from the drop-down list and in the text box enter the minutes past the hour you want it to run each hour. If you don't care, I suggest leaving it at the current time rather than just setting it to 00 because that's likely to be a popular choice and your script may take a little longer to run if the system is busy.
  4. Enter the path of your script in the text box on the right of the row. In this example, you can enter scheduled-scripts/hourly.py.
  5. Click the Add button on the right. You should see a new entry in the list below. To the right of each entry there's a link called log, which shows you the log file of your job - this lets you check that's it's being invoked correctly. There's also a remove link which you can use to unschedule the task when you no longer need it (but doing so won't remove the script itself, you'll need to also delete the file if you don't want it any more).

Note: As far as I know there's no way to edit a scheduled job once you've added it. In other words change the times it runs - you can edit the script itself any time you like and the scheduled job will always execute the current version of the script file. If you need to change the times at which it runs, simply delete it (with the remove link) and re-add it in the same way.

Does that help?

Thanks Cartroo, I think it now works, I got a return code of 2, is that good?

Any return code that's not zero is normally a sign that something went wrong in the script -- that is, PythonAnywhere ran your script happily, but the script itself reported an error. Is the script something you wrote yourself? Or something someone gave you?

I will run it a few times as one completed fine with a 0 returned, yet the other identical script ran at another time and returned 2.

No panic as I will play about with it but appreciate the feedback. Thanks.

No problem! Just let us know if you have any further questions.