Quick Poll: Do you want a Vagrant suspend on shutdown script?

Bash Resources

Hi Everyone,

This is just a quick poll for a consensus of opinion. Every time I run vagrant up I normally forget that it is running and when it comes time to shutdown or reboot I have forgotten that I have vagrant running. How many of you also suffer from this problem?

If there’s enough call for one then I shall write a shell script to suspend all instances of vagrant on shutdown or reboot. Let me know what you think.

Please leave your comments below or reply on Twitter

Create a cheap CCTV system that uploads to the cloud

Create a cheap CCTV system that uploads to the cloud


In this post I will show you how to create a cheap CCTV system that uploads to the cloud.

After moving my office into my garage I found I needed such a system so I built one & thought I’d share my newly found knowledge with you all.

My new CCTV system takes images from 2 cameras, 1 mounted outside the garage and a webcam inside the garage. It then records both images and videos and immediately uploads them to the cloud in case the computer which is collecting the information is itself stolen.

Equipment needed

External CCTV camera
This has to be weatherproof ideally with IR lighting built in.
A Webcam
I would buy the best webcam you can justify as you want the images of the intruders to be a clear as possible
A Cloud Account
Ideally this will be with something like Google Drive or Dropbox, you can use FTP but there may be limitations as the number of connections etc
A Video Card for your Computer
You will need some sort of video card that has the right connections from your camera
CCTV Warning Signs
Here in the UK the ICO who is in charge of peoples privacy rights has deemed that if you are going to use images or people taken from CCTV then you must inform them that you have CCTV equipment in use.
An Operating System
Finally you will need an operating system capable of running this software and for that I recommend a linux system such as Ubuntu

Hooking it all together

Step 1: The Video Capture Card

I would suggest that if you do not already have a card installed you choose one that is compatible with one of the various TV/PVR software libraries such as MythTV. MythTV has a good guide when it comes to choosing a card and is a good starting point if you are a beginner. The video card that use is a Compro S350 that accepts RCA video connections. This is the video connection that my camera has so I was in luck with this stage of the process.

Step 2: The Camera

First of all you need to put the camera in the correct place for the area you want to monitor. For me this was just above my garage door. I set it up so that the view-point of the camera can monitor the shared double drive that our neighbours and us share. The view extends over the road and onto the opposite houses (luckily they are town houses and the bottom floor is a front-door and a garage only).

Picture showing CCTV camera mounted on the outside of my garage

My CCTV camera mounted on the outside of my garage

The wires from this camera go inside the garage and are routed to the back of my Computer where they go into the video card (that I already had installed for Freesat).

Step 3: The Webcam

Next is the webcam. The webcam’s job is to grab any images of intruders that actually get past the first line of defence, and then upload as many images of the intruders to the cloud for later use if needed.

Picture showing webcam angled towards door of garage/office

This picture shows that my office webcam is not pointing at me but in fact at the garage/office door

With the webcam set up so that it faces the garage/office door I have maximised the chances of capturing images of any intruders and uploading them to the cloud before the computer is possibly unplugged and stolen.

Step 4: Test the cameras

Before we install the camera monitoring software we need to make sure that the cameras themselves are working and we know which camera relates to which input.

The best way to do this is by either installing VLC or Cheese Webcam. This example shows using VLC to find out which video input is assigned to which camera.

Picture showing the Open Capture Device option in VLC

This option allows us to view the video inputs on our system

In VLC, right click on the blank area of the window and select “Open capture device”, you will then be presented with the below box. I am typing this post on my laptop so I only have one input but if all is well you should see at least two inputs.

Picture showing the video capture device selection box

You should see multiple inputs here but this clip is showing just one

All you have to do is cycle through the video inputs and take a note of which video input is assigned to which camera.

Step 5: Install Motion

The ideal software for this project is Motion. This is a brilliant, not to mention free piece of software that allows you to control not only the cameras but the detection status as well as the quality and the interval etc.

For Ubuntu users like me this is simple. You just install it via Software Centre. For other users you will need to follow the instructions that are given on the Motion website.

Step 6: Configure Motion

Once it’s installed you will need to configure it. It is pretty much configured for one camera already.

By default it is located at /etc/motion/ If you have a camera and a webcam you will need three files

  1. motion.conf
  2. thread1.conf
  3. thread2.conf

All the generic options are placed in motion.conf and all camera specific options go in the particular thread files. The Motion website has good documentation on all of the config options.

Step 7: Upload to the Cloud

In this example I shall be using the Google Doc CLI. We will be using this facility along with the on_picture_save config option. This will allow us to upload images to our Google Docs/Drive accounts as soon as they are taken by motion. This way, if an intruder gets past your other lines of defence and steals your computer you should have plenty of images already in the cloud and safe for future use.

Step 7a: Install the CLI

The Google CLI Downloads page has loads of options and as an Ubuntu user I’m very pleased to see a .deb file for each release.

Step 7b: Configure the CLI

The first time you use the service you will have to authorise the computer you are on to use your Google Account. It makes sense therefore that we do this early. The format of the CLI input is
Google <Service> <command>
. So to authorise your computer just try to list the contents of your Google Drive with
Google docs list
You will then be asked to enter your Google username. Make sure you enter the full email address that you are registered with. Once you enter your username you will be presented with an interface that will give you an authorisation code to enter back into the command line. Enter the code in the command line and Bob’s your Uncle; Fannie’s your Aunt (that good).

We also need to make sure that we upload to the correct folder. The CLI does not take a hierarchical folder structure; it simply takes the name of the directory that you wish to upload to. That means that you need to make sure that you have a directory with a unique name to upload your media content to.

Now enter
sudo gedit /etc/motion/motion.conf
Find the line that says
; on_image_save
Now change it to
on_image_save Google docs upload %f <upload_directory>
for example to upload to the CCTV folder I use
on_image_save Google docs upload %f CCTV
The %f bit is a motion shortcut for the filename. you can change the format of the filename in the motion.conf file

Step 7c: My setup

You may be wondering how I have my motion configured.
I have my video files set at 25 frames per second on both cameras with 100% image quality (I’m looking at reducing this though).

I have the external camera set to take 1 shot per video. This is the option that detects the most pixels changed and uses this as the image/thumbnail. On the webcam I want as many shots as I can get of any intruders so as well as the video file each and every frame is saved to a separate file/image (and uploaded).

That’s it, now it’s time to test it all.

If you’ve configured motion properly you should be able to fire up http://localhost:8081 and get one camera…

Screenshot of the working outside CCTV images

Taken not long after dawn so not the best shot but it shows the sort of picture you get. This one isn’t at full resolution either, it is just to show and test that the camera is working properly

…and change to http://localhost:8082 and get the next camera (your camera order may not be the same as mine).

What Next?

There are a few things that we still need to consider:


Unless you have massive amounts of [hard drive] space to use you will be needing some sort of system to manage how much media you store. There are several options:

  1. Automatic deletion of media over a certain amount of days eg over 3 days
  2. A purge on media when storage gets down to a certain threshold eg purge media when storage gets down to 10GB
  3. Do nothing and simply have a manual approach: assuming you are able to check on your system at regular intervals this is a viable if not time consuming option

I am currently in the process of creating a CMS for my CCTV system which will allow such options and once the system is complete I will probably be releasing it for beta testing.

Automatically Start Motion Detection

I have found that one of the biggest pains of the current system is having to manually start and stop detection. Again there are several options.

  1. Have cron jobs start and stop your detection system
  2. Use the Motion plugin that starts and stops detection based on whether a registered bluetooth device is in range or not
  3. Simply having shortcuts or keystrokes set to start and stop detection


That’s it: I have left out the configuration details for motion as every system is different and you may not want your system set up the way I have but with a cup full of technical know-how & a bag full of enthusiasm it isn’t too difficult to create a cheap CCTV system that uploads to the cloud. You can make it quite configurable and suit it to your own needs. There’s nothing saying that you cannot extend this to a whole array of uses included live webcam streaming etc (which is one of the features I have already implemented in the CMS that I am creating).

Show Git branch & untracked/modified files in terminal

Show Git branch & untracked/modified files in terminal

Bash Git Resources

Many people know that you can add the Git branch name you are currently working in to the end of your command prompt but did you know that you can also have the command prompt notify you if you have any untracked of modified files?

Screen clip of the final result

Clip of the final result showing the Git branch name and one or more untracked files represented by the “U” after the branch name

I have created this post with linux in mind but I’m sure you can Google what to do if you have a different OS

First place the following in ~/.bashrc so that .bashrc can read my script.

# Include my very own git parse type function 🙂
source ~/.GitParseStatus.sh

Then I update the PS1 variable (the command prompt text) to output the result of the script

export PS1=”\n${debian_chroot:+($debian_chroot)}${RED}\u@\H ${YELLOW}\A ${BLUE}\w \$(gitParseStatus)${NORMAL}\n$ ”

It’s the \$(gitParseStatus) bit that works the magic we want.

and then of course I have the script which I save as ~/.GitParseStatus.sh (don’t forget to make the script executable with

chmod +x ~/.GitParseStatus.sh

#function get_out() {
# log $1
# return
#function log() {
# echo '* '$1
#if [ $1 = "--debug" ]; then
# DEBUG=true
# echo
# echo "==========="
# echo "Debug is on"
# echo "==========="
# echo
# Set Variables

# What do you want to show when everything is up to date
# Your options are…

# Now set the all ok message

# What do you want to display when there git needs updating in some way?
# I have used “M” for files have been modified

# What do you want to display when there are untracked files in the repo?
# I have used “U” but you can use whatever you want

# How do you want to display the repo branch
# I have used square brackets

# What colour do you want the git section of you command line to be when
# everything is ok?
# I have it as green
# Set some colours #
# Some examples are…


function gitParseStatus {

# If we fail to get a branch name then simply return empty handed 🙁
#IN=$(git symbolic-ref HEAD 2> /dev/null) || echo “You are not in a Git repo”
IN=$(git symbolic-ref HEAD 2> /dev/null) || return
#echo $IN

BRANCH_NAME=`echo $IN | sed ‘s/refs\/heads\///’`
#log ‘Branch Name=”‘$BRANCH_NAME'”‘

# get a list of files to run through
FILES=$(git status -z 2> /dev/null)
#log ‘Git Status=”‘”$FILES”‘”‘

if [[ -z “$FILES” ]]; then

#log “All the files in your Git repo are up to date”




#log “You have discrepancies in your Git Repo”

#log ‘Clearing the $UPDATES variable’

#echo ‘About to start testing on “‘${FILES}'”‘
#log ‘Testing for untracked files (ones beginning with ??)’
UNTRACKED=$(echo ${FILES} | grep $’\x000?{2}’)

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”


UNTRACKED=$(echo ${FILES} | grep ^?{2})

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”


#log “No Untracked files were found”

#log “Testing for modified files”

modified=$(echo ${files} | grep $’\x000[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then]

#log “modified files were found”


modified=$(echo ${files} | grep ‘^[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then

#log “modified files were found”


#log “No Modified files were found”


echo $OUTPUT

I hope it is of use to some of you and if you have any problems you know where I am.


Run XBMC Fullscreen on second monitor in Ubuntu

Bash Resources

After trying to find a way to run XBMC fullscreen in second monitor I decided a bespoke solution was the way forward.

UPDATE: This post was written back in the times of Ubuntu 12 (I think). As such as as you can see from the comments at the bottom of the post there are now better ways of accomplishing this task.

Although this tutorial focuses on the XBMC Media Center it can be applied to many other programs.

I know there have been work-arounds for previous versions of Ubuntu but over the years they have fallen by the wayside.

What I’m going to show you now will allow you to run XBMC fullscreen in second monitor.

Points to Note

  1. If you want to copy the code I advise you to click on the word “bash” above the code, this will open up a new box for you to copy the correct code from
  2. This tutorial is to get XBMC running in fullscreen but the code can be used to get many other programs running in fullscreen
  3. I have structured the tutorial so that you can run XBMC fullscreen in second monitor but you can easily alter the code to have it fullscreen in your primary monitor
  4. Quiet a bit of this tutorial uses the terminal, you can open a Terminal window by pressing control (or command for MacOS), Alt & T at the same time


Step 1: Run XMBC and get the title

Run XBMC up so that we can find the title it uses.

To find the title type

wmctrl -l

into the terminal. This will bring up a list of windows that the window controller is in charge of.

The chances are that the XBMC window is called “XBMC Media Center” but it may be different on yours so look in the last column of the output and copy the XBMC title.


Step 2: compizConfig settings manager

See if you have compizConfig installed by typing

which ccsm

If you get a path to the executable such as


then you have it installed. If not type

sudo apt-get install compizconfig-settings-manager

Now you can start the program by entering


in the terminal window.


Once the program has loaded look on the right hand side and under “Effects” click “Window Decoration

Now find the field titled “Decoration windows” and click on the green plus icon on the right hand side.

The CompizConfig Settings Manger

Look for the “Window Decoration” option

In the first drop down box select “Window Title

In the second box enter the title that the previous step displayed

Click the “Invert” checkbox and click Add

Once that box disappears you can exit that program.

What this does is hide the title bar from from the XBMC window


Step 3: Get your display information

Now we need to get the display details

Run xrandr from the terminal

xrandr | grep " connected" | sed -e 's/\(.*\) connected \(.*\) (.*/\nDisplay Name: \1\nDisplay Sizes: \2 /'

You will be presented with an output similar to the following

Display Name: VGA-0
Display Sizes: 1440x900+0+0

Display Name: DVI-I-1
Display Sizes: 1680×1050+1440+0

Assuming you’re wanting the program in your second monitor then we the information from the latter of the two monitors

The display name is straight forward – take a note of this.
The next line not to straight forward. The value is made up of

display-width x display-height + start-x-pxs-from-the-left + start-x-pxs-from-the-top

The value we need here is the third value as this will be where the XBMC window will start (from the left)


Step 3: Create out actual script file

Create a file called….well, what ever you want, I called mine XBMC_fullscreen.sh

In this file type or paste the following:

# This script will run a program in fullscreen mode (no borders or
# title bar etc) on a second monitor. It can used for many programs
# and on either the primary or secondary monitor
#This is the executable file or path
# This is the output of wmctrl -l for the program that we are using
NAME='XBMC Media Center'
# This is the device name of the monitor we want to display the program on
# If you want the program to be fullscreen on your second monitor
# this variable should be the width of your primary monitor
# Run the program , then wait a bit before carrying on
# If your program doesn't load in time then try increasing this value
$PROGRAM > /dev/null 2> /dev/null & disown && sleep 3
## Set the Open GL environment variables
# Set the vertical blanking to on
# And make sure OpenGL syncs the correct monitor
# Do we want a screensaver
# Set the dimensions of the program in the monitor
wmctrl -r "$NAME" -e '0,'$PRIMARYWIDTH',-1,-1,-1'
# Maximize it
wmctrl -r "$NAME" -b toggle,fullscreen,maximized_vert


Now you need to change the NAME variable to something other than the default value **IF** your version of XBMC has a different title in the window (see last step).

You also need to change the DEVICE variable to the one returned as Display Name above.
Lastly you need to change the PRIMARYWIDTH variable to the third number in the returned Display Sizes above.

Now let’s take a look at what this does (just the important lines though)

This tells the system to use bash to execute the script
Set the program to the path we found earlier
NAME='XBMC Media Center'
This sets the title of the the window that we want to control
This is the name of the display device that we noted down earlier
This is the width of your primary monitor
$PROGRAM > /dev/null 2> /dev/null & disown && sleep 3
Ok, this line will:

  1. Start the program
  2. Redirect the output
  3. Disown the process so that the the script can carry on
  4. Wait 3 seconds so the program has a chance to start
Set the vertical line blanking to on
Make sure that we tie our XBMC window to the device we want
Make sure we don’t have the screensaver
wmctrl -r "$\$NAME" -e '0,"$PRIMARYWIDTH",-1,-1,-1'
This will set the window to start at the end of the primary monitor
wmctrl -r "$\$NAME" -b toggle,fullscreen,maximized_vert
This will put the window in full screen but we also need to maximize the window vertically


Step 5: Save the file

Now we need to save the file and make it executable.

I’m sure you know how to save a file but then once it’s saved go back into the terminal and type

chmod +x

So for me it was

chmod +x ~/xbmc_fullscreen.sh

Step 6: Create a shortcut

If you haven’t already got it, install gnome-panel

sudo apt-get install --no-install-recommends gnome-panel

Now type

gnome-desktop-item-edit ~/Desktop/ --create-new

This will bring up a box that will enable us to add a shortcut to the desktop. We can then drag and drop it on the launcher.

Attaching this shortcut to the Launcher will allow you to run XBMC fullscreen in second monitor

The Create a shortcut dialogue box

Ok, so

In the “Type” box leave the default value (Application)

In Name type anything you like, I typed “Run XBMC in Full Screen

In the 3rd box type the path to our shell file in the form of /home/user/xbmc_fullscreen.sh

If you want to type any comments in the last box then feel free.

Take a look on your Desktop and you should now have a shortcut to XBMC that works as default in full screen.

If you want, simply drag this shortcut over the launcher to the side of the screen.

That about does it for this script.

That’s it, thanks for looking, I hope you enjoyed the post and don’t forget to leave your comments below.


At some point further down the line the above script stopped working for me. It appears as if it needed a bit of a timeout so I often had to run a script containing these commands. This basically goes through the previous proceedure but as the XBMC program is already running it just sets the GUI co-ords etc.

NAME='XBMC Media Center'
wmctrl -r "$NAME" -e '0, '$PRIMARYWIDTH',-1,-1,-1'
wmctrl -r "$NAME" -b toggle,fullscreen,maximized_vert

If you have run the first part of the post but XBMC is still in your primary monitor then try this part. Hopefully your system just needed a bit of a timeout before the wmctrl commands were set.

Vimtip #1: start gvim from terminal and still use terminal

Resources Vim-tips

This tip will allow you to start gvim from the terminal and still use it after Gvim has opened.

Step 1:
Add the following to your ~/.bashrc or mac equivalent

function gvim {
/usr/bin/gvim -f $* > /dev/null 2> /dev/null & disown

Step 2:
Type gvim in your terminal and it will not hog it until you close gvim i.e

john@John-Tosh-Lin 10:31 ~
$ gvim
[1] 10381

john@John-Tosh-Lin 10:31 ~

As a bonus, if you start gvim in the directory that you are working in then NERDTree’s root will be the directory you are in. That in itself saves a lot of time 🙂
Enjoy your newly reclaimed terminal people 🙂