Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to capture a picture with my webcam and I need to log the output to check for errors, but cron is failing at this. I am able to do this from terminal, but cron running it doesn't work.

Here's the script:

#!/bin/bash
script -ac "fswebcam -c /home/webcam/webcam.conf" /data/webcam/logscript.txt

Crontab:

@reboot mv /home/webcam/tmp/* /home/webcam/tmp_lost/
* * * * * sh /data/webcam/script.sh > /data/webcam/log.txt 2>&1

Terminal output:

[webcam@raspberry-pi webcam]$ sh /data/webcam/script.sh 
Script started, file is /data/webcam/logscript.txt
--- Opening V4L2:/dev/video0...
/dev/video0 opened.
Setting Brightness to 0 (50%).
Setting Contrast to 19 (20%).
--- Capturing frame...
Skipping 50 frames...
Timed out waiting for frame!
Capturing 1 frames...
Timed out waiting for frame!
Captured frame in 0.00 seconds.
No frames captured.
Script done, file is /data/webcam/logscript.txt

logscript.txt (generated by script command) when ran from terminal:

[webcam@raspberry-pi webcam]$ cat logscript.txt 
Script started on ke 21. elokuuta 2013 21.10.52
--- Opening V4L2:/dev/video0...
/dev/video0 opened.
Setting Brightness to 0 (50%).
Setting Contrast to 19 (20%).
--- Capturing frame...
Skipping 50 frames...
Capturing 1 frames...
gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0xba 0x28
Captured 51 frames in 3.91 seconds. (13 fps)
--- Processing captured image...
Flipping image horizontally.
Flipping image vertically.
Putting banner at the bottom.
Setting banner background colour to #FF000000.
Setting banner line colour to #FF000000.
Setting banner text colour to #FFFF00.
Setting font to luxisr:12.
Setting timestamp "%d.%m.%Y %H:%M".
Writing JPEG image to '/home/webcam/tmp.jpg'.

Script done on ke 21. elokuuta 2013 21.10.57

Running it through cron. Cron output -> log.txt

[webcam@raspberry-pi webcam]$ cat log.txt
Script started, file is /data/webcam/logscript.txt
Script done, file is /data/webcam/logscript.txt
--- Opening V4L2:/dev/video0...
/dev/video0 opened.
Setting Brightness to 0 (50%).
Setting Contrast to 19 (20%).
--- Capturing frame...
Skipping 50 frames...
Capturing 1 frames...
gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x8e 0x80
Captured 51 frames in 3.38 seconds. (15 fps)
--- Processing captured image...
Flipping image horizontally.
Flipping image vertically.
Putting banner at the bottom.
Setting banner background colour to #FF000000.
Setting banner line colour to #FF000000.
Setting banner text colour to #FFFF00.
Setting font to luxisr:12.
Setting timestamp "%d.%m.%Y %H:%M".
Writing JPEG image to '/home/webcam/tmp.jpg'.
Script started, file is /data/webcam/logscript.txt

logscript.txt file is empty after cron and that is the problem. As you can see: when running from cron, the script-command is started and done'd before any output happens. When in terminal it is fine. Anyone know why this is happening?

share|improve this question
up vote 0 down vote accepted

When invoked as sh /data/webcam/script.sh > /data/webcam/log.txt 2>&1, sh will interpret /data/webcam/script.sh as a command file and everything else as arguments to it -- including, possibly, the redirection operators.

Force sh to interpret '/data/webcam/script.sh > /data/webcam/log.txt 2>&1' as a command instead:

sh -c '/data/webcam/script.sh > /data/webcam/log.txt 2>&1'

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .