Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to grab the same image (from a website) every x minutes to eventually make a timelapse video.

My issues are:

  • setting up the schedule to grab the image and
  • saving the image with the time/date as part of the file name.

Would it be best to use something like Python?

share|improve this question
    
Do you necessarily need Python? This can easily be done with normal Bash commands and wget. Have you tried anything or did you get stuck somewhere? –  slhck Aug 22 '13 at 8:36
    
I'm reasonably competent with DOS batch files, but have little experience with Bash (beyond ls and shutdown!). I assumed that Python would probably be the answer, but I'm happy with Bash. I'll be trying this out tonight. Many thanks. –  Nigel Aug 23 '13 at 15:34
    
slhck - thanks for the assistance, it worked! I want to save the image files to a subdirectory of my home directory called pdm-5min. I have tried to modify the output statement but can't get it to work. I have tried -O ~/pdm-5min/"${timestamp}.png" and "~/pdm-5min/"${timestamp}.png" but no joy. Further assistance appreciated! Nigel –  Nigel Sep 2 '13 at 19:19
    
Could you post this comment under the answer below? @justbrowsing will then get a notification. Perhaps you can edit your question to include what your current status is or what exactly doesn't work? Thanks! –  slhck Sep 3 '13 at 6:14
add comment

1 Answer

up vote 5 down vote accepted

Let's use your avatar for example (save to /opt/download-avatar)

#!/bin/bash
URL="https://www.gravatar.com/avatar/554f043fa658cba9bbc2ee4c6d4e052d?d=identicon"
timestamp=$(date +%F_%T);
wget "$URL" -O "${timestamp}.png"

Note: it may be better to save the filenames as seconds from epoch timestamp=$(date +%s)

Create a cron job to run it every 5 mintues (save to /etc/cron.d/timelapse-avatar)

#!/bin/sh
*/5 * * * * /opt/download-avatar

Make the scripts executable

chmod +x /opt/download-avatar
sudo chmod 755 /etc/cron.d/timelapse-avatar

If you don't want to use cron then simply (save to ~/start-timelapse)

#!/bin/bash
input="$1"
URL="https://www.gravatar.com/avatar/554f043fa658cba9bbc2ee4c6d4e052d?d=identicon"

if [ -z "$input" ]; then
  echo "USAGE: $0 [interval in minutes]" 
  exit 1
else
  interval=$((60*input));
fi

while [ 0 = 0 ]; do
  timestamp=$(date +%s)
  wget "$URL" -O "${timestamp}.png"
  echo "Sleeping for $input minutes"
  sleep "$interval"
done;

Make executable and run at 7 minute intervals

chmod +x ~/start-timelapse
cd ~
./start-timelapse 7

UPDATE:

Thanks slhck.

Nigel, by default cron runs as root user. Not pertinent to this question but technically it runs as a "special user", non-interactively, without environmental variables such as $PATH, without the dbus session id, without the X session id, etc.

~/ is shorthand for a user's home directory (also see the environmental variable $HOME). By default for users, this would be for example /home/nigel but for root, this is /root therefore probably cron has been saving them to /root/pdm-5min. You could use:

wget "$URL" -O "/home/nigel/pdm-5min/${timestamp}.png"

BUT, the files would be owned by root, so nigel wouldn't be able to move, delete, edit them unless you give yourself permission as root (or through sudo)

Therefore you might choose to run cron as your user nigel.

#!/bin/sh
*/5 * * * * nigel /opt/download-avatar

Make sure the directory you are saving to exists, might want to add directly before the wget line:

mkdir -p /home/nigel/pdm-5min/

Lastly, be careful with your " (quotes). Use them so that variables retain whitespace (spaces, tabs, etc.) but make sure each single or double-quote has a matching one, it's not optional.

share|improve this answer
1  
Note that in the latter variant, the download would eventually stop if the shell was quit or the terminal was exited, etc. –  slhck Aug 22 '13 at 10:38
    
@Justbrowsing: thanks for the assistance, it worked! I want to save the image files to a subdirectory of my home directory called pdm-5min. I have tried to modify the output statement but can't get it to work. Nothing is saving. I have tried -O ~/pdm-5min/"${timestamp}.png" and "~/pdm-5min/"${timestamp}.png" but no joy. Further assistance appreciated! Nigel – Nigel –  Nigel Sep 3 '13 at 6:21
1  
@justbrowsing - many thanks for the rapid and concise reply. All points raised very useful,and I am now successfully running cron as me, rather than as root, and saving my grabbed images to the correct folders. Thanks again. Appreciated. –  Nigel Sep 3 '13 at 19:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.