pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Security system cron jobs

Sun May 04, 2014 5:53 pm

Hi Folks,

I have a security camera system on a pi that works great. I am trying to automate it a bit with cron and having some issues.

The system uses Motion and has worked very well. My issue is with two things related to cron... I think.

The following three commands work just fine in terminal and do exactly what I want them to do (after I remove the scheduling bits). However, once I add the second two of them to cron, they do not work (the one I use to start motion LD_PRELOAD works fine).

I checked the status of cron, and I think it is asking all commands to execute, but I don't know how to determine why they are second two are not working.

The intention of the third command is just to monitor the CPU temperature and write it to a text file.

Thoughts?

Thanks
pumpkinpi

Code: Select all

0 8 * * * LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion.conf
15 15 * * * /etc/init.d/motion stop
* 1 * * * /opt/vc/bin/vcgencmd measure_temp &>> /home/pi/SecurityCam/temperature.txt | date &>> /home/pi/SecurityCam/temperature.txt

User avatar
jojopi
Posts: 3087
Joined: Tue Oct 11, 2011 8:38 pm

Re: Security system cron jobs

Sun May 04, 2014 9:56 pm

pumpkinpi wrote:* 1 * * * /opt/vc/bin/vcgencmd measure_temp &>> /home/pi/SecurityCam/temperature.txt | date &>> /home/pi/SecurityCam/temperature.txt
"&>>FILE" is a bash shorthand for ">>FILE 2>&1". cron jobs normally run in /bin/sh, which in Raspbian is dash, so you must write this out in full.

You should separate the two commands with ";" rather than "|", because the order of execution may vary in a pipeline where the right-hand command is not actually reading from the left. You may want to use a subshell to avoid repeating the redirection:

Code: Select all

(/opt/vc/bin/vcgencmd measure_temp; date) >>/home/pi/SecurityCam/temperature.txt 2>&1
Finally, I doubt that "* 1 * * *" (every minute from 01:00 to 01:59) is what you intended.

I do not see the problem with the other command. Typically, issues are due to having a different environment from cron than from an interactive shell, but I would expect the init scripts to be tolerant of that.

Anyway, try adding something like ">>/tmp/cron 2>&1" to capture the output from the command and look for clues.

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Security system cron jobs

Mon May 05, 2014 1:15 am

Thanks!

Tried this:

Code: Select all

*/10 * * * * (date; /opt/vc/bin/vcgencmd measure_temp) >>/home/pi/SecurityCam/temperature.txt
It works now. I did not know I could use brackets like that. Nifty.

I want to write a temperature every ten minutes. I had messed up the schedule in the original post.

As for the other command, I tried:

Code: Select all

50 20 * * * /etc/init.d/motion stop >>/tmp/cron 2>&1
This is what I get in that cron file. Not sure why it is not working.

Code: Select all

error pid:25251, boost::interprocess::bad_alloc
error pid:25254, boost::interprocess::bad_alloc
Stopping motion detection daemon: motion/etc/init.d/motion: 70: /etc/init.d/motion: start-stop-daemon: not found
error pid:25255, boost::interprocess::bad_alloc
 failed!
When I run the command:

Code: Select all

sudo /etc/init.d/motion stop
I get:

Code: Select all

[ ok ] Stopping motion detection daemon: motion.
I have no idea why I am getting that

Code: Select all

error pid:25255, boost::interprocess::bad_alloc
It showed up the other day and keeps appearing at occasional times and in error logs.

Any other advice on stopping motion from cron?

Thanks,
pumpkinpi

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Security system cron jobs

Mon May 05, 2014 3:30 am

Solved my own issue another way. Instead of stopping the motion daemon, I just created two config files. In one config file, the daemon is set up to detect motion. In the second config file, the daemon is set up to simply stream the output.

In this way, with cron, I can just toggle between the two with a scheduler.

Code: Select all

30 23 * * * LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion.conf
01 06 * * * LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion34.conf
pumpkinpi

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Security system cron jobs

Mon May 05, 2014 3:35 am

Darn. I spoke too soon. I does not seem to work...

Pondering...

User avatar
rpdom
Posts: 15600
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Security system cron jobs

Mon May 05, 2014 6:24 am

pumpkinpi wrote:As for the other command, I tried:

Code: Select all

50 20 * * * /etc/init.d/motion stop >>/tmp/cron 2>&1
This is what I get in that cron file. Not sure why it is not working.

Code: Select all

error pid:25251, boost::interprocess::bad_alloc
error pid:25254, boost::interprocess::bad_alloc
Stopping motion detection daemon: motion/etc/init.d/motion: 70: /etc/init.d/motion: start-stop-daemon: not found
error pid:25255, boost::interprocess::bad_alloc
 failed!
When I run the command:

Code: Select all

sudo /etc/init.d/motion stop
I get:

Code: Select all

[ ok ] Stopping motion detection daemon: motion.
I have no idea why I am getting that

Code: Select all

error pid:25255, boost::interprocess::bad_alloc
It showed up the other day and keeps appearing at occasional times and in error logs.

Any other advice on stopping motion from cron?
Because you're not running it with sudo in the crontab, so it doesn't have permissions?
Try:

Code: Select all

50 20 * * * /usr/bin/sudo /etc/init.d/motion stop >>/tmp/cron 2>&1
I'm assuming you're using the "pi" crontab, rather than the "root" crontab.

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Security system cron jobs

Tue May 06, 2014 12:09 am

I just did a quick test with that and it seemed to work. I set up my schedule for tomorrow so we will see if it reliably stops motion tomorrow. Thanks for the tip!

pumpkinpi

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Security system cron jobs

Wed May 07, 2014 12:02 am

It is working well with this code:

Code: Select all

59 07 * * * LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion.conf
30 23 * * * LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion.conf
15 15 * * * /usr/bin/sudo /etc/init.d/motion stop >>/tmp/cron 2>&1
15 06 * * * /usr/bin/sudo /etc/init.d/motion stop >>/tmp/cron 2>&1
*/30 * * * * (date; /opt/vc/bin/vcgencmd measure_temp) >>/home/pi/SecurityCam/temperature.txt
Thanks so much for the help everybody.

pumpkinpi

Return to “Automation, sensing and robotics”