In the django project, the database needs to be backed up regularly. I see that django-crontab is used on the Internet, but it is not executed, so the script is loaded directly at the project startup to execute the scheduled task.
django-crontab
crontab
How to execute a script at project startup
1. Directly in manage.py
#add your fun
execute_from_command_line(sys.argv)
2. In the app's __init__.py, the app is required to be registered, such as
import os
default_app_config = "monitor.apps.MonitorConfig"
os.system('crontab -u root -r')
os.system('crontab corn.sh')
corn.sh:
a task that is executed once a day, note that an absolute path is required
0 0 * * * sh /opt/xxx/do_job.sh
do_job.sh:
#!/bin/bash
#定时任务
cp /opt/backup.sql /tmp/$(date -d "today" +"%Y%m%d").sql
sqlite3 /opt/touch/xxx.db .dump > /opt/backup.sql