如果我希望 shell 脚本在/etc/cron.hourly 中自动运行,我总是在 lunux 中编写 shell 脚本。我有以下 python 脚本(为 python 3.6 设计,但我的服务器上的默认 python 版本是 2),我希望在/etc/cron.hourly 而不是 crontab 中执行。我怎样才能实现这个目标?
"Perform automated database backups using xtrabackup"
import os
location='/opt/mysql/backups/daily'
with open('/authentication/account.txt') as f:
mylist = f.read().splitlines()
username = mylist[0]
password = mylist[1]
hostname = mylist[2]
os.system(f"innobackupex --user={username} --password={password} --
host=localhost {location} >/dev/null 2>&1")
# Delete backup folders older than 3 days
os.system(f"find {location} -type d -ctime +7 | xargs rm -rf")
如果这是可能的,一旦我安装此版本而不是默认的 python 2,如何确保 cron.hourly 使用 python3.6?
最佳答案
创建一个新的 shell 脚本并将其放置在 /etc/cron.hourly
文件夹中。
Shell 脚本应该如下所示。
#!/bin/bash
python3.6 complete_path_of_your_script.py
python3.6
命令确保您的脚本以 3.6 版本运行(如果您的服务器中已安装 python 3.6)
供您引用 - https://askubuntu.com/questions/7676/function-of-etc-cron-hourly
关于python - 如何在/etc/cron.hourly中执行python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56033109/