简体   繁体   English

每 n 小时在 mysql 服务器上运行脚本

[英]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.我在 python 中编写了一个脚本,它对 MySQL 服务器中的一些数据进行了一些处理。 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.目前,我需要它每 24 小时运行一次,我通过每天手动运行它来实现这一点。 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.我知道如何通过安排任务(Windows)在我的计算机上执行此操作,但无论我的计算机是打开还是关闭,我都希望它运行。

Yes, you can use a cron job to run your script.是的,您可以使用 cron 作业来运行您的脚本。

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这是一个很好的教程,但我会把重要的部分放在这里https://gavinwiener.medium.com/how-to-schedule-a-python-script-cron-job-dea6cbf69f4e

On the command line enter crontab -e在命令行输入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.阅读上面的链接,或谷歌搜索其他一些 cron 作业教程,以根据您的需要进行调整。

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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