LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script & cron probs. (https://www.linuxquestions.org/questions/programming-9/bash-script-and-cron-probs-371069/)

skubik 10-08-2005 07:01 PM

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.

leonscape 10-08-2005 07:31 PM

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

skubik 10-08-2005 11:11 PM

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.

Quigi 10-09-2005 11:13 AM

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.

paulsm4 10-09-2005 05:32 PM

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).

skubik 10-10-2005 12:07 AM

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.

bigearsbilly 10-10-2005 05:46 AM

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.

skubik 10-10-2005 02:49 PM

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.

???


leonscape 10-10-2005 05:00 PM

Try running xhost + before running the cron job if that works then its definatley a permission error with X, i.e. Its locked out.

skubik 10-11-2005 02:17 AM

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.

Quigi 10-11-2005 10:35 AM

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.

skubik 10-11-2005 12:17 PM

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.

bigearsbilly 10-12-2005 03:32 AM

will it run without X?

eddiebaby1023 10-15-2005 09:56 AM

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
....


skubik 10-15-2005 06:20 PM

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.