django-sspanel前后端那些坑

定时任务(宝塔安装)

用宝塔安装前端,定时任务不生效

解决起来很简单,既然涉及定时任务,就在这里详细说明一下(从头捋一遍),如果你只想看解决办法,那请看最后的章节就可以了。

部署前端时,会涉及定时任务的设置,当时保持默认没有修改.

定时任务配置文件

站点根目录/configs/default/cron.py

from .common import BASE_DIR

# 定时任务相关
CRONJOBS = [
    ('* 1 * * *', 'commands.croncmds.check_user_state',
     '>>' + BASE_DIR + '/logs/userstate.log'),    #应改为1 1 * * *,每天凌晨1:01分
    ('0 0 1 * *', 'commands.croncmds.auto_reset_traffic',
     '>>' + BASE_DIR + '/logs/trafficrest.log'),  # 每月月初重置免费用户流量,日志写入logs
    ('15 12 * * 1', 'commands.croncmds.clean_traffic_log',
     '>>' + BASE_DIR + '/logs/trafficrest.log'),  # 每周一清空流量记录,日志写入logs
    ('30 2 * * *', 'commands.croncmds.clean_online_log',
     '>>' + BASE_DIR + '/logs/node_online.log'),  # 每天凌晨2:30删除节点在线记录,日志写入logs
    ('0 3 1 * *', 'commands.croncmds.clean_node_log',
     '>>' + BASE_DIR + '/logs/node_info.log'),  # 每月第一天凌晨3点删除所有节点负载,日志写入logs
    ('0 4 1 * *', 'commands.croncmds.reset_node_traffic',
     '>>' + BASE_DIR + '/logs/node_reset.log'),  # 每月第一天凌晨4点重置节点流量,日志写入logs
    ('30 1 * * *', 'commands.croncmds.clean_online_ip_log',
     '>>' + BASE_DIR + '/logs/onlineip_reset.log'),  # 每天凌晨1点半清空ip记录
]

添加定时任务

站点根目录下运行(部署前端时已添加)

python manage.py crontab add

查看添加情况

python manage.py crontab show

反馈

11f3c8d90eedaeac474c6c8ef1cb43e4 -> ('* 1 * * *', 'commands.croncmds.check_user_state', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/userstate.log')
181dbdf56d7aea75df1fbcd73c0feb0e -> ('0 0 1 * *', 'commands.croncmds.auto_reset_traffic', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log')
ed765a72de47b0d552ad6cb9b4dbf1f4 -> ('15 12 * * 1', 'commands.croncmds.clean_traffic_log', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log')
f3cc77a34a6f873156217283b170f8c5 -> ('30 2 * * *', 'commands.croncmds.clean_online_log', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_online.log')
ac648108a393f2de53edaac32dfbc640 -> ('0 3 1 * *', 'commands.croncmds.clean_node_log', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_info.log')
39dd3b3cd21ea350d443621aea7d8f9d -> ('0 4 1 * *', 'commands.croncmds.reset_node_traffic', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_reset.log')
42a13f8132ea5bf9bd237a127a156703 -> ('30 1 * * *', 'commands.croncmds.clean_online_ip_log', '>>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/onlineip_reset.log')

说明定时任务已经添加上了,类似11f3c8d90eedaeac474c6c8ef1cb43e4指的是定时任务的id,>>后面的是日志。可以查看cron(后面章节)进行核实。

手动单独运行某项定时任务:

python manage.py crontab run 11f3c8d90eedaeac474c6c8ef1cb43e4

成功运行的话会反馈(每项反馈不一样)

Time: 2018-07-10 14:30:33.833909 CHECKED

手动运行所有定时任务:

python manage.py crontab run

他会报错

(venv-sspanel) [root@ssr django-sspanel]#  python manage.py crontab run
Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    execute_from_command_line(sys.argv)
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django_crontab/management/commands/crontab.py", line 29, in handle
    Crontab().run_job(options['jobhash'])
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django_crontab/crontab.py", line 126, in run_job
    job = self.__get_job_by_hash(job_hash)
  File "/root/.pyenv/versions/venv-sspanel/lib/python3.6/site-packages/django_crontab/crontab.py", line 173, in __get_job_by_hash
    'Run "python manage.py crontab add" again to resolve this issue!' % job_hash
RuntimeError: No job with hash None found. It seems the crontab is out of sync with your settings.CRONJOBS. Run "python manage.py crontab add" again to resolve this issue!
(venv-sspanel) [root@ssr django-sspanel]#

提示让你重新添加计划任务,也就是再次运行 python manage.py crontab add 尝试了并不管用。

查看cron

这步没什么实际意义,只是确认一下,是否加入到cron计划任务中

查看/编辑 使用方法同vim.

crontab -e

54 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
* 1 * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 11f3c8d90eedaeac474c6c8ef1cb43e4 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/userstate.log # django-cronjobs for configs
0 0 1 * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 181dbdf56d7aea75df1fbcd73c0feb0e >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log # django-cronjobs for configs
15 12 * * 1 /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run ed765a72de47b0d552ad6cb9b4dbf1f4 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log # django-cronjobs for configs
30 2 * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run f3cc77a34a6f873156217283b170f8c5 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_online.log # django-cronjobs for configs
0 3 1 * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run ac648108a393f2de53edaac32dfbc640 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_info.log # django-cronjobs for configs
0 4 1 * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 39dd3b3cd21ea350d443621aea7d8f9d >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_reset.log # django-cronjobs for configs
30 1 * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 42a13f8132ea5bf9bd237a127a156703 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/onlineip_reset.log # django-cronjobs for configs

第一行不是哦,剩下的就是添加的定时任务了,也可以手动执行一条,看看命令有没有问题,没报错就没问题。

/root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 42a13f8132ea5bf9bd237a127a156703 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/onlineip_reset.log # django-cronjobs for configs

如果这里的全部删掉,那就是删掉了,站点根目录下执行python manage.py crontab show,会返回空值。当然可以执行python manage.py crontab add再添加。

问题所在

手动单独运行某一项定时任务均没有问题,手动运行全部定时任务便会报错(这个无视他吧),cron手动执行也没有问题,定时任务并没有生效,所以问题来了,到底哪里的问题。

无论 python manage.py crontab show 或者是 crontab -e均显示任务执行时会输出日志,类似如下字段

/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/****.log

宝塔-文件查看站点根目录下没有logs目录,也没有相关日志文件。

最终解决办法

在宝塔面板操作,手动在站点根目录下创建logs目录,以及所有log文件包括:

userstate.log trafficrest.log node_online.log node_info.log node_reset.log onlineip_reset.log

重新测试

如果你要尝试的话请慎重,仔细对照定时任务配置文件中每项定时任务的意义再动手。
将其中一项定时任务(或者全部)改为每分钟执行一次,也就是 *,然后再查看日志文件或日志文件的修改日期,确定计划任务会执行。
因为我是测试站,无用户所以把所有的都改成了每分钟执行一次。测试完再删掉重新添加。

corntab -e

54 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
* 1 * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 11f3c8d90eedaeac474c6c8ef1cb43e4 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/userstate.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 181dbdf56d7aea75df1fbcd73c0feb0e >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run ed765a72de47b0d552ad6cb9b4dbf1f4 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/trafficrest.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run f3cc77a34a6f873156217283b170f8c5 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_online.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run ac648108a393f2de53edaac32dfbc640 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_info.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 39dd3b3cd21ea350d443621aea7d8f9d >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/node_reset.log # django-cronjobs for configs
* * * * * /root/.pyenv/versions/venv-sspanel/bin/python /www/wwwroot/free.augustdoit.bid/django-sspanel/manage.py crontab run 42a13f8132ea5bf9bd237a127a156703 >>/www/wwwroot/free.augustdoit.bid/django-sspanel/logs/onlineip_reset.log # django-cronjobs for configs

保存后查看是否生效

定时任务

确实生效了,那么在改回来

corntab -e

将相关的全部删掉

54 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

重新添加

cd /www/wwwroot/free.augustdoit.bid/django-sspanel/

python manage.py crontab add

检查

python manage.py crontab show

最终更改的方案

CRONJOBS = [
    ('1 1 * * *', 'commands.croncmds.check_user_state',
     '>>' + BASE_DIR + '/logs/userstate.log'),    #改为每天凌晨1:01分
    ('0 0 1 * *', 'commands.croncmds.auto_reset_traffic',
     '>>' + BASE_DIR + '/logs/trafficrest.log'),  # 每月月初重置免费用户流量,日志写入logs
    ('15 12 * * 1', 'commands.croncmds.clean_traffic_log',
     '>>' + BASE_DIR + '/logs/trafficrest.log'),  # 每周一清空流量记录,日志写入logs
    ('30 2 * * *', 'commands.croncmds.clean_online_log',
     '>>' + BASE_DIR + '/logs/node_online.log'),  # 每天凌晨2:30删除节点在线记录,日志写入logs
    ('0 3 1 * *', 'commands.croncmds.clean_node_log',
     '>>' + BASE_DIR + '/logs/node_info.log'),  # 每月第一天凌晨3点删除所有节点负载,日志写入logs
    ('0 4 1 * *', 'commands.croncmds.reset_node_traffic',
     '>>' + BASE_DIR + '/logs/node_reset.log'),  # 每月第一天凌晨4点重置节点流量,日志写入logs
    ('* */2 * * *', 'commands.croncmds.clean_online_ip_log',
     '>>' + BASE_DIR + '/logs/onlineip_reset.log'),  # 改为每2小时清空ip记录
]