Bash is an acronymous for “Bourne-again-shell”, a common and widely used UNIX shell.
Fiddling with a Linux server and exploring the power of Bash scripting.
I wanted to automate image captures from a webcam and create a timelapse every day.
Having never done this before, I startet out by breaking down the steps I figured was needed:
1. Pull the image from the remote webcam
2. Sequentially rename and sort the imagefiles
3. Create the movie
4. Create a backup and clear the captures for a new day
So the scripts I wrote for achieving this turned out as follows:
1.
grab.sh
wget "URL-to-capture" -O /var/www/files/`date +%Y-%m-%d-%H%M%S`.jpg
Cron schedule every 5 minutes:
*/5 * * * * /usr/local/bin/grab.sh
2.
ren.sh
#delete tmp symlinksrm /var/www/files/tmp/*.jpg # create a symbolic link in tmp folder x=1; for i in $(ls -r -t /var/www/files/*jpg); do counter=$(printf %03d $x); ln -s "$i" /var/www/files/tmp/img"$counter".jpg; x=$(($x+1)); done
3.
makemovie.sh
# create a variable with todays date today=$(date +%Y%m%d) #use avconv to create the timelapse movie avconv -y -f image2 -r 25 -i /var/www/files/tmp/img%03d.jpg -vcodec libx264 /var/www/timelapse/"$today"timelapse.mp4
#Cron schedule for Step 2. and Step 3. every 10 minutes: */10 * * * * /usr/local/bin/ren.sh && /usr/local/bin/makemovie.sh
4.
newday.sh
#create a variable with todays date today="$(date +%Y-%m-%d)" #compress and backup the images captured today tar -cvf /var/www/archive/backup"$today".tar /var/www/files/*.jpg #remove the images from working dir to be ready for new day rm /var/www/files/*.jpg
Cron scheduled at midnight:
0 0 * * * /usr/local/bin/newday.sh
What I find particularly sweet (and new to me) is the function of symbolic links(in step 2) and being able to process the links and not the actual files(step 3). Also making the links sorted (ls -r -t /var/www/files/*jpg) and named sequentially in chronological order based on modified date is very nice to achieve with the “for” statement in step 2.
As for the sorting and renaming processing I was also checking out “gawk” but had some issues so I left it alone.
I find Bash and the extremly extensive packages library for Linux shell very powerful, stable and well documented.
I am sure this can be done in many different ways and I am not a programmer/scripter – so go gentle on me:)
I am allways happy for any suggestions, so please leave a comment under the heading of this post.
One day in life of a pier, displayed over 8 seconds (buildt up by 208 images)