Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
10-08-2005, 07:01 PM
|
#1
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Rep:
|
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.
|
|
|
10-08-2005, 07:31 PM
|
#2
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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
|
|
|
10-08-2005, 11:11 PM
|
#3
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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
# ++++ 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.
- skubik.
|
|
|
10-09-2005, 11:13 AM
|
#4
|
Member
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377
Rep:
|
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.
Last edited by Quigi; 10-09-2005 at 06:05 PM.
|
|
|
10-09-2005, 05:32 PM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
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).
|
|
|
10-10-2005, 12:07 AM
|
#6
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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.
|
|
|
10-10-2005, 05:46 AM
|
#7
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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.
|
|
|
10-10-2005, 02:49 PM
|
#8
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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.
???
Last edited by skubik; 10-10-2005 at 03:20 PM.
|
|
|
10-10-2005, 05:00 PM
|
#9
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
Try running xhost + before running the cron job if that works then its definatley a permission error with X, i.e. Its locked out.
|
|
|
10-11-2005, 02:17 AM
|
#10
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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.
|
|
|
10-11-2005, 10:35 AM
|
#11
|
Member
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377
Rep:
|
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.
|
|
|
10-11-2005, 12:17 PM
|
#12
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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.
|
|
|
10-12-2005, 03:32 AM
|
#13
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
will it run without X?
|
|
|
10-15-2005, 09:56 AM
|
#14
|
Member
Registered: May 2005
Posts: 378
Rep:
|
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
. $HOME/.profile
.. rest of script
....
|
|
|
10-15-2005, 06:20 PM
|
#15
|
Member
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 161
Original Poster
Rep:
|
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.
Last edited by skubik; 10-15-2005 at 08:21 PM.
|
|
|
All times are GMT -5. The time now is 09:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|