Category Archives: Tips, Tricks and Tutorials

Handy how to snippets grabbed from the computing world.

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:

hostname

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

date

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.

Solved: Appcelerator Build Error: “EPERM, operation not permitted” Tips, Tricks and Tutorials 27 MAY 2016

Sigh, I really do not enjoy building Apps. Anyway, the latest roadblock to hit me after being asked to quickly unlock the previously locked orientation on one of our Appcelerator-built mobile apps, required me to boot up Appcelerator Studio, make the code tweak and then run an Android build – only to be stopped in my tracks by a glaring compiler error that read:

“EPERM, operation not permitted”.

After a bit of tearing my hair out, Googling, and just general despair, the solution to this problem revealed itself… the damn antivirus was interfering here!

So a quick trip to the Avast! antivirus control panel in order to turn off all the active monitoring, a restart of the build process, and voilà… my app change was done.

So just jotting this down here in case I run into this issue again. (Also, don’t forget to turn you Antivirus back on again when you are done!)

Avast Active Protection settings window above appcelerator studio

Related Link: Appcelerator | Avast!

Simple way to Backup Files from an Ubuntu Server to Amazon S3 Cloud Tips, Tricks and Tutorials 17 MAY 2016

You can never have too many backups, and this is a simple way of backing up files from an Ubuntu server to the Amazon S3 cloud storage system.

Backup-three-laptops-plugged-into-word

For this, you will obviously need an Ubuntu server, an Internet connection, and of course, an Amazon AWS account.

First things first, you’ll need to generate Amazon AWS access keys, which you do from the AWS Security Credentials page (Access Keys section) in the AWS console.

Write these (both the Access Key ID and Secret Access Key) down somewhere safe, because you definitely don’t want to be losing them unnecessarily. (Maybe a Google Doc might be a good idea here?)

Now head over to the S3 Management page in the AWS console, where you will need to create the new bucket (or folder in an existing bucket) where you want to store your backed up files in.

With your bucket created, and your access details at hand, head into your Ubuntu server and install the super useful Amazon S3 targeted s3cmd package:

sudo apt-get install s3cmd

Next configure it by entering the requested information (your Access Key details will be needed here). Note, you do have the option to encrypt the files in transit, and if you choose to do so, it is probably worth your while to jot down the password in that previously mentioned Google Docs file of yours!

s3cmd --configure

Run the connection test and if everything passes, you should be good to go. You can check your current buckets by doing a directory listing with s3cmd:

s3cmd ls

You are pretty much just about there now. To do the file backup, we’ll use s3cmd’s built in sync command. To push files to Amazon S3, we declare the parameters in the order of local files then target directory. So for example, if we have a S3 bucket called server-backup, and want to back up our user account’s home directory to S3, the sync call would look like this:

s3cmd sync ~/* s3://server-backup

You can of course get all clever and target specific folders, exclude or include files and folders using wildcard characters, etc. (See the documentation for more). For example, here I exclude .svn folder files using:

s3cmd sync --exclude '*.svn' ~/* s3://server-backup

If you are happy with the sync result, then all that is left is to throw the command into a short bash file, give it execute rights and add it to the cron scheduled tasks system. So for example, create the file cron_s3_backup.sh:

nano /home/craiglotter/cron_s3_backup.sh

Add this text:

#!/bin/bash
s3cmd sync /home/craiglotter/* s3://server-backup/craiglotter/

Save, and make the file executable:

chmod +x ~/cron_s3_backup.sh

Finally, add it to the cron in the usual manner. Open the crontab for editing:

crontab -e

Add the following line for a daily backup at 07:00 in the morning.

0 7 * * * bash /home/craiglotter/cron_s3_backup.sh >/dev/null 2>&1

Done.

Facebook (Android): How to Cancel Stuck, Failed Uploads Tips, Tricks and Tutorials 02 MAY 2016

I wanted to upload a quick pic of the indoor braai that I had started to ring in the end of the long weekend to Facebook, but for some or other reason, the image upload failed, or rather got stuck in its attempt to upload to the social network’s servers. This means now that every time you open the Facebook app, you watch as it futilely tries to reassure you that your post is STILL being created, even though deep down inside you know that it is a lost cause.

Actually, this happens relatively frequently, and with seemingly no method for cancelling the infinitely in progress upload, it is quite a frustrating problem to sit with.

IMG_20160502_174345 country mews indoor braai fire

As I mentioned, opening and closing the app won’t solve the problem. You could go extreme of course, and uninstall, but that’s a little bit on the overkill side. Instead, probably the easiest way to resolve the stuck upload issue is to simply log out of the app via the menu. Log back in, and by magic, your stuck upload should be nowhere in sight.

I have no idea why this works. It shouldn’t, but it does.

Maybe one day Facebook will fix their Android app and allow us to cancel stuck in progress uploads in a more natural fashion!