0

I use motion to detect motions with a webcam. I try to run a command when a motion is detected.

In /etc/motion/motion.conf, I set the option on_picture_save like this:

on_picture_save date | mail -s "Webcam alert" myaccount@gmail.com

This sends me a mail when a picture is saved. It works perfectly. Now I would like to run a command when a motion starts. For example:

on_event_start touch /home/pi/test

But it does not work. I don't understand why... How to use the on_event_start option?

5
  • 1
    I don't use motion but if it runs as a particular non-root user that is not pi, it will not have access to /home/pi/test.
    – goldilocks
    Sep 23 '15 at 13:53
  • 1
    @goldilocks Actually, I would like to call a script (which contains curl and mpg123 calls). How could I give correct permissions to do that? I don't even know how can I log the permission errors...
    – clemtoy
    Sep 23 '15 at 16:48
  • There are two factors WRT to permissions, the UID of the process, and those set on the file it is trying to access (and the directories leading to that file -- if the process cannot get to the directory, it doesn't matter what permissions are on the file). If motion (I'm guessing that's the name of the process) starts a script, it will probably do it with the same UID it has; you can find out with ps -o user,comm -C motion while it is running (actually that's the username, not the uid, but more useful)...
    – goldilocks
    Sep 23 '15 at 17:22
  • 1
    ...If it's root (UID 0), permissions are not the issue. If it's not, you have to take permissions into account. WRT logging errors, I'd hope there's some documentation for that, but you could also try grep motion /var/log/syslog.
    – goldilocks
    Sep 23 '15 at 17:23
  • Have you tried changing the access permissions? I'm experiencing a similar issue and can't get on_event_start to work! No matter what is the requested task is. Any thoughts?
    – Rotail
    Jan 21 '20 at 17:41
3

If you try to use on_event_start in motion to do something with GPIO, you will discover that user motion has no access to /dev/mem. So a Python script that wants to activate a relay through a GPIO pin will fail.

You can verify this when logged in as user pi: python relaytest.py works but sudo -u motion python relaytest.py gives

Traceback (most recent call last): File "relaytest.py", line 9, in GPIO.setup(22, GPIO.OUT) RuntimeError: No access to /dev/mem. Try running as root!

user pi has access to memory. But the script will run as user "motion".

The solution to this is to let user access GPIO. Use this command: sudo usermod -a -G gpio motion This adds user "motion" to group "gpio" and on_event_start can activate relays, etc. through the GPIO.

0

To add to this thread, I was having an issue with:

on_motion_detected /usr/bin/python3 /home/pi/mlai/movementDetected.py

And as noted above, when I tested this as:

sudo /usr/bin/python3 /home/pi/mlai/movementDetected.py

from the command line it failed because the Python 'import pika' failed as SU had no access to pika... So 'pip3 install pika' fixed this.

1
  • I would not use the on_motion_detected for any script. it gets called for EACH motion aplay a short sound and you will see what I mean
    – Arrow22
    Mar 23 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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