I am using Motion to record videos.

I have a Python script which sends the video to me when it ends (sendvideo.py).

I have a bash script that clears the folder (clearmedia.sh)

I have tried the following:

on_movie_end sudo python /home/pi/Motion/sendvideo.py && bash clearmedia.sh

The clearmedia.sh file is simple:

#! /bin/bash/
sleep 50s
rm -f /home/pi/Motion/media/*

The reason for the sleep is because the video is created using the images that are collected by Motion.

When I use sleep the script fails to run.

When I remove sleep the script runs but the video wont work because the contents of my media folder is cleared before it has a chance to email me the video.

Is there a way to getting this to work? I could stick the bash script in the on_event_end section of the motion.conf file but I would like it to work after the movie ends and the email is sent.

This is the final step in getting my pi home security system to work. I just need to avoid my Pi filling up with loads of files. I don't mind my email filling up as that is the point of this experiment!

Any ideas?


  • Why not add the delete step to the end of the Python script - so it completes the transfer first? Or run the bash script via cron and change it to delete files older than an hour (something longer than it takes to create and transfer the file). Oct 19, 2016 at 20:26
  • A crude workaround would be to rename the folder based on the time stamp and recreate another in its place. the original folder (which was renamed) can be deleted at leisure after said post processing is done (possibly through cron jobs as @Steve suggested) . This is assuming the objective is to get manageable size videos and there is atleast sufficient available free disk space while the different tasks are running. Oct 19, 2016 at 21:30

1 Answer 1


Ok I solved the problem.

I came across the python module 'subprocess' and used this.

import subprocess

This is essentially called after the email is sent. Works brilliantly!

I now have my alert system set up and don't have to worry about my Pi filling up with nonsense files.

Thanks for the advice @Steve and @Shreyas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.