bash script & cron probs.
I've written a small bash script to use spcaview to capture an image from my USB webcam. I want to be able to have this script run as a cron job every 5 minutes. The script is responsible for capturing the webcam image and moving it to a specific directory within my webserver.
When I run the script on it's own (ie: from the bash shell commandline) the script works perfectly. But when it runs from cron, it seems as though spcaview isn't writing the output file at all. Nothing is different in the script, I even checked to see what the script was running as to make sure it wasn't a permissions issue. But the script runs as root, and runs FROM the directory where I want the webcam image to be saved to. I've double-checked the permissions for this directory, and it's fine (755 root root). I'm completely baffled as to why it won't save this image. Any thoughts or ideas? If you'd like me to post the script itself, let me know. Thanks, - skubik. |
Whats the working directory?
When you run the script, its no where the script actually is that counts, its what the current working directory is of cron. so rather than doing somthing like this in the crontab 0 5 * * 4 /home/username/scriptdirectory/yourscript Try 0 5 * * 4 cd /home/username/scriptdirectory && yourscript |
Thanks leon, but this didn't seem to work. Here's my crontab entry:
*/5 * * * * cd /usr/local/scripts/ && ./getwebcamimg 1> /dev/null The script itself writes to a log file, including the timestamp for when the script was run. So in doing this, I can check the log file and know that it *is* running every 5 minutes like I want, but for some reason spcaview just won't write the output file. Here's the script: Code:
#!/bin/bash - skubik. |
Check if the environment is the same. Run 'env' both from the command line and from the script file, and compare the result.
Another idea to check: Some programs act differently when they know they're being run from a terminal. BTW, I run my cron jobs as a normal user unless root privileges are necessary. But this is not really related to your problem. |
Don't throw away potentially helpful info!
One other suggestion: change your "1> /dev/null" to "1> /tmp/tmplog.txt 2>&1" (at least until you get the problem figured out).
|
Thanks paul, I checked what you suggested and the two environments are definitely not the same. When run as root in a console there are (obviously) many more environment variables set than root in a cron job.
I have, however, done a little more playing with it and have narrowed the problem potentially to a problem with initializing SDL. Tried running it in a different vterm altogether, logged in as root, and it gave me: "Could not initialize SDL: No available video device" I also tried it in a fresh console logged in as myself, and contrary to what I said in a previous post, it failed, giving me: "Fatal signal: Segmentation Fault (SDL Parachute Deployed)" ... yet it works fine running from a console as root (su'ed into from my user account)??? I'm thinking it's either a permissions issue, or an environment variable issue as suggested. Any suggestions? - skubik. |
dunno about yours but cron may start a /bin/sh shell not what you expect. check it.
also you have a cut down env. It doesn't run your .profile or anything. maybe put . ~/.profile or whatever at the top of the script. |
Thanks billy, I checked the shell from the script as well, and when run from cron, it definitely *is* /bin/sh like you said. But I'm not 100% convinced that's the problem since it won't run when I login as root on a dedicated vterm, which is definitely /bin/bash (as shown in my script log).
Anyone have any insight into the SDL errors I mentioned in a previous post? Thanks! - skubik. BTW, I changed my cron entry to: */5 * * * * cd /usr/local/scripts/ && ./getwebcamimg 1> /data/tmp/.webcam/errors.log 2>&1 ... and examined the contents of errors.log, and found a familiar entry.... Spcaview version:1.0.4 date: 01:05:2005 (C) mxhaard@magic.fr Initializing SDL. Could not initialize SDL: No available video device. ??? |
Try running xhost + before running the cron job if that works then its definatley a permission error with X, i.e. Its locked out.
|
Nope, xhost + doesn't seem to be the solution. Unless I'm doing it wrong (I entered 'xhost +my.host.name' (checked by echo $HOSTNAME) on the command-line in my console window su'ed to root. Also tried it in the dedicated vterm logged in directly as root) Still having the same problem. How frustrating. :^/
- skubik. |
Hi skubik,
No, I don't know what SDL or a "dedicated vterm" is. But (yesterday 5:07am) you established that (A) the environment is different, and (B) it fails when you log in as root. If it fails as root, (local) permissions seem unlikely. (Presumably permissions stop you from running it as yourself; that's where you get the SDL parachute deployed.) I'd track down environment. E.g., start removing variables from the rich environment till it stops working. Or add to the small environment till it starts working. First, I'd try to figure out if you need X11, i.e., if the DISPLAY environment variable is necessary, or if it will run without (which I hope). BTW, there is only one console; referring to a console doesn't sound right. |
Sorry Quigi, sometimes I get ahead of myself with terminology. :)
Just to clarify: I'm usually logged in under my own user account (skubik). If I open a terminal window in X and run the script, I get the SDL Parachute error. If I then (in that same window) su into the root account, then the script works fine. If I then 'Alt+F2' to another 'terminal', and login as root directly, the script fails here, citing the Initialization of SDL failure error. I will try your suggestion of moving environment variables over until it works and post my results later tonight. Thanks! - skubik. |
will it run without X?
|
Do you su to root with the "-" flag to source root's profile? If not, you're obviously setting something from your own profile that's making things work. Get your script to source your .profile when it starts running, to get your environment set properly.
Code:
#!/bin/sh |
AHA! You might be onto something eddie. I have *NOT* been using '-' when I su, which might explain something. Still seems trange that it only works when logged in as root and not as myself. Haven't had time to play around with env vars like quigi suggested, but I'll give it a whirl tonight. :)
- skubik. *Edit* BTW, I just tried to login to root using 'su -' instead of just 'su', and now I'm getting ye olde 'Could not Initialize SDL: No available video device' error. I also checked the 'env' by logging in that way, and it looks like most of the same environment variables are still there, but haven't done a thorough examination, yet. |
All times are GMT -5. The time now is 04:34 AM. |