简体   繁体   中英

run script on mysql server every n hours

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM