ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
# ++++ SCRIPT CONFIGURATION ++++ #
SCRIPTNAME=$0
SRCDEVICE="/dev/video0"
TRGDRIVE="/data/www/apache2:80/gfx/" # rwxrwxr-x 1 root www
TMPDRIVE="/data/tmp/.webcam/" # rwxr-xr-x 1 root root
OUTFILEEXT="jpg"
OUTFILENAME="webcam"
IMGCAPTUREAPP="/usr/local/bin/spcaview"
IMGCAPTURERES="640x480"
IMGCAPTUREFLAGS="-l ${IMGCAPTURERES} -j -b -N 1"
LOGDRIVE=${TMPDRIVE}
LOGFILE="getwebcamimg.log"
# ++++ SCRIPT FUNCTION DEFINITIONS ++++ #
function log()
{
echo "$1" >> ${LOGDRIVE}${LOGFILE}
}
init_script()
{
log "============================================================"
log "Running '${SCRIPTNAME}'"
log "Date: `date`"
log "Running As: `whoami`"
# ---- Ensure Temporary Environment is sane ---- #
if [ -d ${TMPDRIVE} ]; then
log "Temporary Drive Exists at '${TMPDRIVE}'"
if [ -f ${TMPDRIVE}*.jpg ]; then
rm -f ${TMPDRIVE}*.jpg
fi
else
mkdir ${TMPDRIVE}
log "Created Temporary Drive '${TMPDRIVE}'"
fi
# ---- Change to the Temporary Drive ---- #
cd ${TMPDRIVE}
log "Changed to Temporary Drive '${PWD}'"
}
quit_script()
{
# ---- Perform temporary file cleanup (not implemented here) ---- #
echo ""
echo -n " Cleaning Up... "
echo "ok"
log "Ending Webcam Image Capture Session"
log ""
}
capture_img()
{
# ---- Capture the Webcam Image ---- #
log "${IMGCAPTUREAPP} ${IMGCAPTUREFLAGS}"
${IMGCAPTUREAPP} ${IMGCAPTUREFLAGS}
log "Attempt to Capture Web Image"
log "| Temporary Drive Directory Output:"
log "| `ls -l ${TMPDRIVE}`"
log "|__________________________________________________"
}
publish_img()
{
# ---- Verify that a captured image exists, then move it ---- #
if [ -f *.jpg ]; then
cp *.jpg arch/
else
log "No Image File Found in Temporary Drive"
fi
# ---- Move Captured Image To Target Directory ---- #
if [ -f *.jpg ]; then
mv *.jpg ${TRGDRIVE}${OUTFILENAME}.${OUTFILEEXT}
else
log "No Image File to Move to Production Target Drive"
fi
log "Publishing Captured Web Image"
}
main()
{
# ---- Perform Script Initialization ---- #
init_script
# ---- Capture Webcam Image ---- #
capture_img
# ---- Publish Webcam Image To Web Server ---- #
publish_img
# ---- Quit the Script Gracefully ---- #
quit_script
}
############# MAIN SCRIPT ##############
# ---- Call the 'main' function ---- #
main
Works fine when I run as root on the cmd line, but with cron it still has problems. Problem with cron and spcaview? I'm stumped.
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:
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?
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. :^/
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.