0

Is it possible to achieve this from the command line?

Syntax: I can capture an image with the following command:

raspistill -o image.jpg

But I want to be able to run this command via cron each minute and to get something like this

image1.jpg, image2.jpg, image3.jpg, etc...

2 Answers 2

3

If you don't mind the suffix value as long as images are in ascending numerical order you could use the date and time

raspistill -o "$(date --utc +'image%Y%m%d%H%M.jpg')"

Extend with %S for seconds if needed.

If you do plan to add this as a one-liner remember that % is a special character in a crontab entry. Either sidestep the issue and put this in a (very short) script and call the script from cron, or precede each % with a backslash \. See man 5 crontab for the details (search for %).

5
  • Thanks! but I really wanted to be able to use the format in my question without the suffix
    – Dante
    Oct 7, 2018 at 7:08
  • I appreciate your answer but I asked for a number at the end, not a datetime value.
    – Dante
    Oct 7, 2018 at 7:27
  • 1
    Note that cron interprets % in a command specially.
    – Kusalananda
    Oct 7, 2018 at 9:28
  • @Kusalananda what do you mean by that?
    – Dante
    Oct 8, 2018 at 9:13
  • @Dante See the end of the answer and read man 5 crontab about how % are treated in commands.
    – Kusalananda
    Oct 8, 2018 at 9:15
1
#!/bin/sh

num=1
outfile="image$num.jpg"

while [ -e "$outfile" ]; do
    num=$(( num + 1 ))
    outfile="image$num.jpg"
done

raspistill -o "$outfile"

The loop in the above script will increment the num variable by one and create a new output filename in outfile using this number, until a filename that is no taken is found.

This potentially fails if multiple copies of the script are run simultaneously, as there is a race condition between testing for the existence of the filename and creating the new file. Running the script with a one minute interval should not be a problem unless it takes a minute or longer for raspistill to create the new file. In this case, add touch "$outname" after the loop to "claim" the filename as soon as possible (simultaneous invocations of the script may still fail though).

By "failing" I mean two or more invocations of the script using the same filename with raspistill.

4
  • @Kusalanada thanks a lot! I'm a newbie in Linux and didn't know you can use a script in the command line, but how would that work? do I need to save it in a file, make it executable and run it with cron?
    – Dante
    Oct 7, 2018 at 7:06
  • @Dante Yes exactly. If you save it in ~/script.sh and make that executable, you can then schedule that script with cron. This is simpler than trying to execute a complex command directly in the crontab file.
    – Kusalananda
    Oct 7, 2018 at 9:27
  • great, will give it a go!
    – Dante
    Oct 7, 2018 at 11:08
  • @Dante If you want the script to create the files in a particular directory, you can cd to that directory at the start of the script, somewhere before the while loop.
    – Kusalananda
    Oct 7, 2018 at 12:43

You must log in to answer this question.

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