The simple Python script /home/debian/project/del_video.py
works fine:
#!/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))
Today is Friday (when I post), /usr/bin/python3 /home/debian/project/del_video.py
delete files in /home/ftpuser/ftp_dir/upload/6J078DEGAG56788
. I log in to my IP camera's web UI to set the storage, and create a new file in /home/ftpuser/ftp_dir/upload/6J078DEGAG56788
. I add it in crontab:
crontab -e
Output:
@reboot /usr/bin/python3 /home/debian/project/del_video.py
Reboot, no file deleted, no error information in 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)
How can I fix it then?
I can write the same function with Bash. In file 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
I verified that del_video.sh
works fine. How can I fix the Python script?
The permissions are already set for user debian
in file /etc/sudoers:
debian ALL=(ALL:ALL) NOPASSWD:ALL
Please replace the value (day of the week) according to the real value when you try.