LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-08-2005, 07:01 PM   #1
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Rep: Reputation: 21
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.
 
Old 10-08-2005, 07:31 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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
 
Old 10-08-2005, 11:11 PM   #3
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
Old 10-09-2005, 11:13 AM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.
 
Old 10-09-2005, 05:32 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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).
 
Old 10-10-2005, 12:07 AM   #6
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
Old 10-10-2005, 05:46 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 10-10-2005, 02:49 PM   #8
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
Old 10-10-2005, 05:00 PM   #9
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Try running xhost + before running the cron job if that works then its definatley a permission error with X, i.e. Its locked out.
 
Old 10-11-2005, 02:17 AM   #10
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
Old 10-11-2005, 10:35 AM   #11
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.
 
Old 10-11-2005, 12:17 PM   #12
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
Old 10-12-2005, 03:32 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
will it run without X?
 
Old 10-15-2005, 09:56 AM   #14
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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
....
 
Old 10-15-2005, 06:20 PM   #15
skubik
Member
 
Registered: May 2003
Location: A dark corner in Canada
Distribution: Slackware64 15.0/current
Posts: 152

Original Poster
Rep: Reputation: 21
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
cron bash script troubles zzyzx Slackware 3 04-12-2005 12:27 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
bash script from cron kubla Programming 3 01-22-2004 04:30 AM
webmin,cron and bash login script colesen Linux - Software 0 08-12-2003 07:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration