Reputation: 570
I wrote a script in python that does some stuff to some data in a MySQL server. Mostly just reducing unnecessary data. Currently, I need this to run once every 24 hours and I am achieving this by running it manually every day. Is there an easy way to automate this process on the actual server? I know how to do it on my computer by scheduling a task (windows) but I want this to run regardless of whether my computer is on or off.
Upvotes: 1
Views: 310
Reputation: 9227
Yes, you can use a cron job to run your script.
Here's a good tutorial but I'll put the important parts here https://gavinwiener.medium.com/how-to-schedule-a-python-script-cron-job-dea6cbf69f4e
On the command line enter crontab -e
Then add a line similar to this:
0 0 * * * /usr/bin/python3 /full/path/to/yourfile.py >> ~/cron.log 2>&1
Have a read of the link above, or google for some other cron job tutorials, to tweak this to your needs.
If your python script is mostly just running mysql statements, you might want to have a look at the mysql event scheduler too: https://dev.mysql.com/doc/refman/8.0/en/events-overview.html
Upvotes: 1