简单的Python脚本
/home/debian/project/del_video.py
工作正常:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import time
today = time.strftime("%w",time.localtime())
video_dir = '/home/ftpuser/ftp_dir/upload/6J078DEGAG56788'
if today == '5':
for item in os.listdir(video_dir):
if item != 'DVRWorkDirectory':
del_dir = os.path.join(video_dir,item)
os.system('sudo /usr/bin/rm -rf {} '.format(del_dir))
今天是星期五(当我发帖时),
/usr/bin/python3 /home/debian/project/del_video.py
删除/home/ftpuser/ftp_dir/upload/6J078DEGAG56788
中的文件。我登录我的ipcamera webui设置存储,并在/home/ftpuser/ftp_dir/upload/6J078DEGAG56788
中创建一个新文件。将其添加到crontab中。
crontab -e
@reboot /usr/bin/python3 /home/debian/project/del_video.py
重新启动,没有删除文件,
cron.log
中没有错误信息:
cat /var/log/cron.log
Jul 28 09:46:29 debian cron[756]: (CRON) INFO (Running @reboot jobs)
Jul 28 09:46:31 debian CRON[825]: (debian) CMD (/usr/bin/python3 /home/debian/project/del_video.py)
Jul 28 09:49:06 debian crontab[2218]: (debian) BEGIN EDIT (debian)
Jul 28 09:50:38 debian crontab[2218]: (debian) END EDIT (debian)
Jul 28 09:53:35 debian crontab[2694]: (debian) BEGIN EDIT (debian)
Jul 28 09:55:01 debian CRON[2739]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jul 28 09:57:56 debian crontab[2694]: (debian) END EDIT (debian)
那该如何解决呢? 我可以用 bash 编写相同的函数:
vim project/del_video.sh
dw=$(date +%w)
video_dir='/home/ftpuser/ftp_dir/upload/6J078DEGAG56788'
if [[ $dw = 5 ]]; then
for item in $(ls "$video_dir");do
if [[ $item != DVRWorkDirectory ]];then
sudo /usr/bin/rm -rf "$video_dir"/"$item"
fi
done
fi
已验证
del_video.sh
工作正常。我只是想知道如何修复Python脚本。
已为用户
debian
设置权限:
sudo cat /etc/sudoers
debian ALL=(ALL:ALL) NOPASSWD:ALL
尝试时请根据实际值替换该值(星期几)。