-3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
showkey
  • 482
  • 42
  • 140
  • 295
  • Did you try to verify *from within the script* that it is is being run? Did you try to verify what strings it is passing to `os.system`? – Karl Knechtel Jul 28 '23 at 02:19
  • The `sudo` has a higher potential to make troubles. – Klaus D. Jul 28 '23 at 03:44
  • It woks fine with `python3 /home/debian/project/del_video.py`,can't work when to add in the cron task. – showkey Jul 28 '23 at 05:43
  • 1
    A lot of things can go wrong. For example no permission to access the directory, no permission to run sudo without specifying a password, spaces in filenames, etc. Write to a log file from your script to see exactly what it's doing and where it failed. – interjay Jul 31 '23 at 09:49
  • have you tried `crontab -u root -e`? – Ter Aug 02 '23 at 09:48

1 Answers1

1

Try this two things:

  1. os.system() function doesn't use the user's environment variables by default, so sudo might not be in the path.
  2. scripts run with sudo will ask for a password, which isn't possible in this situation. Since you've set up passwordless sudo for the debian user, you can run the script with sudo from the crontab:

Crontab

@reboot sudo /usr/bin/python3 /home/debian/project/del_video.py

And the python code should look like something like this:


#!/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 /bin/rm -rf {} '.format(del_dir))
Kr1sz
  • 134
  • 3