升级Python导致bt面板中的计划任务执行失败
微wx笑 2022-03-03【网页网站】 8 0关键字: bt面板 宝塔面板 Python 计划任务
bt面板升级Python导致 RuntimeError: Bad magic number in .pyc file,解决了之后以为就没有问题了,后来发现设置的计划任务全都执行失败了,根据失败的错误日志判断,还是升级Python版本导致的。
错误日志
File "/www/server/panel/script/backup.py", line 23 print "★["+endDate+"] "+log ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("★["+endDate+"] "+log)? File "/www/server/panel/script/backup.py", line 23 print "★["+endDate+"] "+log ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("★["+endDate+"] "+log)? File "/www/server/panel/script/backup.py", line 23 print "★["+endDate+"] "+log ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("★["+endDate+"] "+log)?
解决方法
参考:bt面板升级Python导致 RuntimeError: Bad magic number in .pyc file
在解决BT面板不能启动的问题时,只将部分位置的“python”修改为“python2.6”,导致一些功能不能用。
比如“bt”文件中的
ps aux |grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'
如果程序是通过“python2.6”启动,那么可能查询到进程ID,如果不是,那就查不到。
1、将“bt”文件中的所有“python”都修改为“python2.6”
完整文件内容如下:
#!/bin/bash # chkconfig: 2345 55 25 # description: bt Cloud Service ### BEGIN INIT INFO # Provides: bt # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts bt # Description: starts the bt ### END INIT INFO panel_path=/www/server/panel cd $panel_path panel_start() { isStart=`ps aux |grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` if [ "$isStart" == '' ];then echo -e "Starting Bt-Panel... \c" if [ -f 'main.py' ];then python2.6 -m py_compile main.py fi nohup python2.6 main.pyc `cat data/port.pl` > /tmp/panelBoot.pl 2>&1 & sleep 0.2 isStart=`ps aux |grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` if [ "$isStart" == '' ];then echo -e "\033[31mfailed\033[0m" echo '------------------------------------------------------' cat /tmp/panelBoot.pl echo '------------------------------------------------------' echo -e "\033[31mError: BT-Panel service startup failed.\033[0m" return; fi echo -e "\033[32mdone\033[0m" else echo "Starting Bt-Panel... Bt-Panel (pid $isStart) already running" fi isStart=`ps aux |grep 'python2.6 task.pyc$'|awk '{print $2}'` if [ "$isStart" == '' ];then echo -e "Starting Bt-Tasks... \c" if [ -f 'task.py' ];then python2.6 -m py_compile task.py fi nohup python2.6 task.pyc > /tmp/panelTask.pl 2>&1 & sleep 0.2 isStart=`ps aux |grep 'python2.6 task.pyc$'|awk '{print $2}'` if [ "$isStart" == '' ];then echo -e "\033[31mfailed\033[0m" echo '------------------------------------------------------' cat /tmp/panelTask.pl echo '------------------------------------------------------' echo -e "\033[31mError: BT-Task service startup failed.\033[0m" return; fi echo -e "\033[32mdone\033[0m" else echo "Starting Bt-Tasks... Bt-Tasks (pid $isStart) already running" fi } panel_stop() { echo -e "Stopping Bt-Tasks... \c"; pids=`ps aux | grep 'python2.6 task.pyc$'|awk '{print $2}'` arr=($pids) for p in ${arr[@]} do kill -9 $p done echo -e "\033[32mdone\033[0m" echo -e "Stopping Bt-Panel... \c"; pids=`ps aux | grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` arr=($pids) for p in ${arr[@]} do kill -9 $p done echo -e "\033[32mdone\033[0m" } panel_status() { isStart=`ps aux |grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` if [ "$isStart" != '' ];then echo -e "\033[32mBt-Panel (pid $isStart) already running\033[0m" else echo -e "\033[31mBt-Panel not running\033[0m" fi isStart=`ps aux |grep 'python2.6 task.pyc$'|awk '{print $2}'` if [ "$isStart" != '' ];then echo -e "\033[32mBt-Task (pid $isStart) already running\033[0m" else echo -e "\033[31mBt-Task not running\033[0m" fi } panel_reload() { isStart=`ps aux |grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` if [ "$isStart" != '' ];then echo -e "Reload service Bt-Panel... \c" pids=`ps aux | grep 'python2.6 main.pyc'|grep -v grep|awk '{print $2}'` arr=($pids) for p in ${arr[@]} do kill -9 $p done nohup python2.6 main.pyc `cat data/port.pl` >> /tmp/panelBoot.pl 2>&1 & if [ "$isStart" == '' ];then echo -e "\033[31mfailed\033[0m" echo '------------------------------------------------------' cat /tmp/panelBoot.pl echo '------------------------------------------------------' echo -e "\033[31mError: BT-Panel service startup failed.\033[0m" return; fi echo -e "\033[32mdone\033[0m" else echo -e "\033[31mBt-Panel not running\033[0m" fi } install_used() { if [ ! -f /www/server/panel/aliyun.pl ];then return; fi password=`cat /dev/urandom | head -n 16 | md5sum | head -c 12` username=`python2.6 /www/server/panel/tools.py panel $password` echo "$password" > /www/server/panel/default.pl rm -f /www/server/panel/aliyun.pl } case "$1" in 'start') install_used panel_start ;; 'stop') panel_stop ;; 'restart') panel_stop sleep 0.2 panel_start ;; 'reload') panel_reload ;; 'status') panel_status ;; 'default') port=`cat /www/server/panel/data/port.pl` password=`cat /www/server/panel/default.pl` echo -e "==================================================================" echo -e "\033[32mBT-Panel default info!\033[0m" echo -e "==================================================================" echo "Bt-Panel: http://IP:$port" echo -e `python2.6 /www/server/panel/tools.py username` echo -e "password: $password" echo -e "\033[33mWarning:\033[0m" echo -e "\033[33mIf you cannot access the panel, \033[0m" echo -e "\033[33mrelease the following port (8888|888|80|443|20|21) in the security group\033[0m" echo -e "==================================================================" ;; *) echo "Usage: /etc/init.d/bt {start|stop|restart|reload|default}" ;; esac
我的系统中最初安装的是python2.6,后来又安装了python 3.7.5
执行“python -V”,得到的结果是 python 3.7.5,具体安装过程参考:CentOS release 6.10 下 Python 3.7.5 安装记录
2、修改/www/server/panel/class/crontab.py文件
在管理计划任务的时候,可以看到地址栏访问的是/crontab,于是找到/www/server/panel/class/crontab.py文件,把用到“python”的地方都修改为“python2.6”
已经创建的任务可以删除重新创建,也可以直接在线编辑脚本,将“python”都修改为“python2.6”就可以了。
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/web/2022-03-03/1079.html