PTZ is disabled when running a cron job script

Forum for questions and support relating to the 1.24.x releases only.
Locked
theforce
Posts: 129
Joined: Tue May 11, 2010 5:22 am

PTZ is disabled when running a cron job script

Post by theforce »

I have a cheap PTZ camera called a wanscam. It will reboot at random and go back to its home position. Otions in the camera dont work to tell it to ignore go to center on boot. So to get the camera to always be pointing in the right spot I wrote a command to go to a set position every so many minutes.

I had to write some code in this control file. http://stuff.jaygroh.com/zoneminder/wanscam.pm
Its a modification of the foscam script. Its kind of a mess since I was trying to get things working and I never cleaned it up. I dont think there is anything in there thats is causing this issue.

Now I wrote a simple script to reset all 3 of my cameras. http://stuff.jaygroh.com/zoneminder/test.sh
The command is

zmcontrol.pl --id 23 --command=presetGoto1

Now at the command line if I manualy type in
sudo zmcontrol.pl --id 23 --command=presetGoto1
It will work fine. The PTZ controls in zoneminder web page will continue to work.

If I run that script as a cron job under root or even just put in the command in the cron job under root the PTZ controls dont work in zoneminder. They are there but unresponsive. I dont understand what is casusing the PTZ controls to go unresponsive only when the commands are ran in a cron job.

Does anyone know how to fix this issue?
mokmeister
Posts: 1
Joined: Sat Oct 20, 2012 6:09 pm

Re: PTZ is disabled when running a cron job script

Post by mokmeister »

theforce wrote:I have a cheap PTZ camera called a wanscam. It will reboot at random and go back to its home position. Otions in the camera dont work to tell it to ignore go to center on boot. So to get the camera to always be pointing in the right spot I wrote a command to go to a set position every so many minutes.

I had to write some code in this control file, [url for location of:wanscam.pm]
Its a modification of the foscam script. Its kind of a mess since I was trying to get things working and I never cleaned it up. I dont think there is anything in there thats is causing this issue.
Just on that, I have a Wanscam PTZ camera too, and I'm not using that control file, and my camera still reboots randomly, so I don't think it's the script at all that's causing the issue.

I don't have a solution to the problem, but I'm looking into it!
theforce
Posts: 129
Joined: Tue May 11, 2010 5:22 am

Re: PTZ is disabled when running a cron job script

Post by theforce »

I think I found the solution! :D

Do not run the script under the root crontab. When you run the script under root it will create a file /tmp/zmcontrol-(monitor num).sock
This file is created by root but needs access by www-data. The file stays I'm guessing until zoneminder is restarted or the box is rebooted. To fix this issue do the following.

sudo su www-data
crontab -e

Insert your script and settings. Save.

exit

Now the script will run and create the files as user www-data and you can control the cameras from the web interface in between when the script is set to run.

So far it has worked for me. Let me know if this works for you.
KaL-El00
Posts: 1
Joined: Mon Feb 02, 2015 6:47 am

Re: PTZ is disabled when running a cron job script

Post by KaL-El00 »

I've tried to add monitor to localZoneminder using "Remote Zoneminder" in Presets, but no success. I suppose that problem is in the monitor ID. I tried to use name and monitor ID as on remote Zoneminder but still nothing.
KazUyA
Paranoid
Posts: 129
Joined: Thu Feb 05, 2009 10:40 pm

Re: PTZ is disabled when running a cron job script

Post by Paranoid »

theforce wrote:
Now I wrote a simple script to reset all 3 of my cameras. http://stuff.jaygroh.com/zoneminder/test.sh
The command is

zmcontrol.pl --id 23 --command=presetGoto1

Now at the command line if I manualy type in
sudo zmcontrol.pl --id 23 --command=presetGoto1
It will work fine. The PTZ controls in zoneminder web page will continue to work.

If I run that script as a cron job under root or even just put in the command in the cron job under root the PTZ controls dont work in zoneminder. They are there but unresponsive. I dont understand what is casusing the PTZ controls to go unresponsive only when the commands are ran in a cron job.

Does anyone know how to fix this issue?
It can be a couple of things:

cron jobs do not have the same environmental variables and PATH as you have on the command line.
It is possible that when you you run it as a cron job it cant find a script or library it depends upon. Write a simple wrapper script that set up the PATH the same as in a cronjob and then executes the zmcontrol script.

or (more likely)

If you run the zmcontrol script as anybody other than the web server user then it can block the PTZ controls from working in the web interface. This is because zmcontrol creates a socket file, as the user you run it as, that can not be accessed by less privileged users. This means your "sudo zmcontrol.pl ..." creates the socket as user root and the web server user can not access it.

On my system the webserver runs as the user "wwwrun" so in my case I would have to run something like:

Code: Select all

su - -c "/usr/bin/zmcontrol.pl --id 2 --command=presetGoto --preset=1" wwwrun
But that probably wont work. The reason for this is that for security reasons the web server user does not have a shell so you can not su to that user. Instead, what can do is write a script that runs all the zmcontrols you need (as root) and then changes the owners of the sockets to the webuser.
Locked