Category Archives: CodeUnit

CodeUnit houses software and programming tips, snippets and tutorials, complemented with the occasional hardware review.

Changing the Hostname in Ubuntu Server 16.04: Unit hostname.service is masked Tips, Tricks and Tutorials 03 OCT 2017

For older versions of Ubuntu, many of the tutorials online show you how to edit your server’s hostname by simply editing /etc/hostname and then restarting the service with “service hostname restart”.

However, attempting this on a 16.04 Ubuntu Server build will most likely fail with the following error message:

Failed to restart hostname.service: Unit hostname.service is masked.

As it turns out, the new way of changing your hostname on systems running systemd (i.e. Ubuntu 16.04) requires you to use the hostnamectl command, meaning that to set your new hostname you need to run:

sudo hostnamectl set-hostname NEWNAME

(Where obviously NEWNAME is the new/desired hostname that you want to use).

You can check that the change has been affected by running:


Worth jotting down here for future reference.

FPDF: How to use a Degree Symbol in a Generated PDF Tips, Tricks and Tutorials 29 SEP 2017

I’ve been using the FPDF PDF generator library for years now as the de facto method for my PHP projects to produce PDF reports. However, one minor annoyance is that the generated PDF files often falter when it comes to the inclusion of certain special characters – like the degree symbol (°) as an example.  (Basically, something like °C becomes °C in the final document)

The reason for this happening is that Arial, the default used/included font, is of type ISO-8859-1 while the degree symbol is UTF-8 encoded. So in order for us to include special symbols or characters from other languages, we need to either try and convert them into our font compatible ISO-8859-1 format, or perhaps switch to using a different TrueType or Type1 font (which then would contain the desired character set).

Now while UTF-8 support is available via a modified class, the easiest way to fix the degree symbol issue without having doing any real work is to simply make use of the PHP utf8_decode function, which convert UTF-8 encoded strings to their ISO-8859-1 equivalents.

In other words outputting utf8_decode(“°C”) to your PDF should result in the expected °C

Related Link: FPDF PDF Generator Library

Ubuntu Server: How to change or set the Timezone Tips, Tricks and Tutorials 29 SEP 2017

Dropping this as a quick reminder to myself. When confronted with a new Ubuntu server instance, I can check the currently set time zone by simply asking for the current time with


Now if I need to change the currently set time zone to something else, the easiest would be to just run

sudo dpkg-reconfigure tzdata

This should bring up an easy to use graphical interface that will allow you to select the correct geographical region/city time zone that you wish to have your server reflect.

Note: Because your cron jobs all depends on this information, it is a VERY good idea to restart the cron service after making your timezone change:

sudo service cron restart

Also worth pointing out, if you are not particularly sure on which city time zone to choose, you could always select the geographic area “Etc” and then choose from the more generic options like GMT or GMT+2.

(UPDATE: Oh. Turns out that that I’ve left this note for myself before. I may need to rethink my note system.)

Related Link: Ubuntu Server

MacBook Air: How to Update Node.js Tips, Tricks and Tutorials 14 MAR 2017

Ugh. I needed to fire up the loan MacBook Air to do an iOS compile of my recently completed Appcelerator app. Naturally, I started off on the wrong foot by not recalling the MacBook’s password (luckily I found that I had mailed it to myself some time ago, so I did eventually get in). Then Appcelerator moaned that it needed an update, which failed because it needed a newer version of Node.js to be installed.

So off to Google I once more marched, the result of which I am now jotting down here as a future reminder: How to Update Node.js on a MacBook Air.

As it turns out, updating node.js is a pretty quick affair: First, fire up a terminal (which again I needed to march over to Google in order to learn how to do), and then run the following commands:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Note the use of sudo to run these commands. Essentially we are first force clearing the npm cache, then installing and upgrading node to the latest version.

Related Link: MacBook Air | Node.js

Appcelerator and Android Bug: Invalid “–device-id” value “XYZ” Tips, Tricks and Tutorials 10 JAN 2017

Working in Appcelerator Studio, I was trying to compile and run the Android app that I’m developing on a Lenovo A7600-H tablet hooked up to my Windows 10 laptop via USB.

Developer Options’ USB debugging checkbox was turned on (remember, if it isn’t already showing – and it shouldn’t be – you can open up Developer Options as a Settings menu option by clicking 7 times on the Build Number item tucked away behind the About Phone/Tablet settings menu option), and the USB computer connection was set to MTP media device/files option.

However, the run process kept aborting with the following error message: Invalid “–device-id” value “XYZ” where XYZ was a label associated with the hooked up device.

As it turns out, the problem is actually one of authentication or rather authorization – essentially the device doesn’t trust the PC that it is currently plugged into. So to resolve, I turned off the “USB debugging” setting, clicked on the “Revoke USB debugging authorizations” menu option below that, and then turned USB debugging back on. The tablet asked me if it was okay to allow the computer connection, and after confirming on the device and then returning to Appcelerator Studio to compile and run, the Invalid –device-id issue was gone.

So, worth jotting down for future reference then.

Bored? Enter Vibescout
[Partner Content] 02 DEC 2016

With the weekend looming large, are you still scratching your head trying to come up with some ideas on what to do with your precious free time this coming Saturday and Sunday? Sure, Netflix or DSTV is always a fallback, but come on, Summer is here!

Which is exactly the point at which Vibescout saunters into the conversation!

Vibescout Cape Town Events

Vibescout is a cool local startup that brings to life the local events guide concept, packaging it in a clever, slick, mobile optimized website containing well… loads of categorized and geographically tagged events.

With a wide selection of events listed, the obvious cool trick is the ability to narrow down the list of events to either a specified place or your current surrounds – I mean, just check this perfect events list for my current location (with each event’s distance from me noted) when I ran a check from home!


The site is clear and well laid out, simple to use, well formatted and super pleasingly, not in an app form. (Seriously this is a plus – not everything needs to be a standalone app on your phone!)

As I mentioned, this is a completely homegrown South African project, meaning that there is plenty of friendly, local help if needed, all of which then basically means that you should definitely be checking out and adding Vibescout to your brower’s bookmarks as soon as possible! ;)


After all, what’s on in Cape Town? Well Vibescout should know…

Related Link: Vibescout | Twitter | Facebook

PHP: Figure out which Function called Your Function Programming 03 JUL 2016

When troubleshooting code, it is often quite handy to know what function called the function that you are busy working your way through. To do this you need a backtrace, and PHP makes getting one pretty easy thanks to its debug_backtrace function.

If you are interested in only the calling function name itself, then this neat little function should work quite nicely:

function GetCallingFunctionName($debug = false) {
//Returns the calling function through a backtrace
        $r = '';
        $debugString = '';
        $caller = debug_backtrace();
        if ($debug === true) {
            $debugString.= ' <p>START: debug_backtrace result:</p>';
            $debugString.= '<pre>';
            $debugString.= print_r($caller, true);
            $debugString.= '</pre>';
            $debugString.= '<p>END: debug_backtrace result:</p>';
        $caller = $caller[2];
        $r .= $caller['function'] . '()';
        if (isset($caller['class'])) {
            $r .= ' in ' . $caller['class'];
        if (isset($caller['object'])) {
            $r .= ' (' . get_class($caller['object']) . ')';
        return $r . $debugString;

Worth jotting down for the next time a function is giving me a headache for no apparent reason!

one-handed backhand tennis shot

Related Link: debug_backtrace | Source 1

Solved: F3 Framework Route is running Twice Programming 02 JUL 2016

I’ve inherited a project written using the Fat-Free Framework (or F3 Framework), which is billed as being a lightweight PHP micro-framework that is intended to be easy-to-use and fast, with a gentle learning curve thrown in for good measure. (In fairness, all three of these points seem to be holding relatively true thus far…)

fat-free framework php

Anyway, the project was written by an outsourced Polish developer, and now that he is no longer available, I’m bringing it in-house and expanding on it. Of course, this means that I’m refactoring it a little bit to better suit my own coding style. However, I hit an annoying snag which embarrassingly held me up for far longer than what it should have.

After making a class/controller name change, I was testing my routing when it became apparent that all of a sudden, one of my routes was running it’s function call twice.

An annoying issue to say the least.

So just a heads up, before you tear your hair out – be sure that your target function does in fact not share the same name of your newly renamed class in which said function resides.

If it does, then the framework will call the function through the run() routing as expected, and following that, the function will be called again… because as you might have noticed by now, the fact that your function is the same name as the class means that it is of course a constructor function.

So yes. I’ve jotted this down as a reminder to myself, that sometimes I’m an idiot.

Related Link: Fat-Free Framework