closing code brace

How to change Swappiness in Ubuntu

It’s likely that if you don’t know how to change the Swappiness value, you don’t know what you should be changing it to, and maybe don’t know what Swap is at all, so this is actually a few questions

What is Swappiness?

Swappiness is the setting relating to how aggressively your linux system will use the Swap Memory.

What is Swap Memory?

Swap memory is a type of overflow memory that is used to prevent your system crashing when the memory required approaches or passes the size of RAM on a linux system. It is a partition of your Hard Drive or SSD that is dedicated to “act like” RAM. Remember that Hard Disk Drives are physical drives that need to mechanically spin to retrieve data. You should not use Swap as a permanent replacement for RAM. Solid State Drives are faster than HDDs, but they’re not as fast as RAM. A 128GB SSD shouldn’t be viewed as 128GB of cheap RAM.

How much Swap Memory do I need?

How long is a piece of string? The answer varies depending on a number of circumstances that your environment has.  The minimum you should aim for is at least the same size as the RAM you have available. If you’ve got a simple 512MiB RAM installation, then you should have at least 512MiB HDD space.  If HDD space is at a premium as well as your RAM size, then you’ll have to make considerations. For example if you’ve got 4GB RAM and a 20GB HDD (for some insane reason) then you’re not going to want to spend 4GB of your HDD on Swap.  If HDD space is plentiful then go for at least the size of your RAM, and see how that works. If you’re reaching 100% of your swap memory on a regular basis, consider increasing your RAM alongside the size of your swap.

If HDD space is sparse compared to your RAM then make a reasonable swap partition, monitor it for a few days, particularly under high load, and see how the server runs.

So, what should my Swappiness be set to?

Annoyingly for anyone looking for a quick-fix it’s not as simple as just saying “60″. You need to take a few things into consideration:

Swapiness can be configured to anything between 0-100. The 0 and 60 aren’t %ges of Swap that will be used, they’re an aggression variable. A low aggression means that your system will prefer to steal pages form cache, a high aggression means that it prefers move to swap memory. Your mileage may (will) vary from system to system.

Adding to swap and taking from swap is in itself quite expensive on the system. Having said that, it’s also generally less expensive than grabbing from a (potentially external) Hard Drive.

A good number to start with is 50. Then work from there. If you find that your system is slowing, or locking up more often than you’d like, then check the status of your system in #top or #free. If the RAM usage is too high, then increase the swappiness, if the swap is too high then decrease swappiness. If you’re getting as low as 10-15 and still not seeing an improvement, then you’ll want to start looking into other issues. Equally, if you’re getting as high as 80-90 and not seeing an improvement then you’ve likely got issues elsewhere (consider checking MySQL buffers, and your actual RAM size).

How to Change Swappiness

You’ll need to have super user privileges for most of the following, and you’ll need to be able to turn off the system to reboot.

First let’s check swappiness.

cat /proc/sys/vm/swappiness

So mine was 60 (it’s actually the default, because I spun up the server for this tutorial). The server is an 8GB one, and it’s got a 4GB database running on it, I expect that Apache is going to take up a lot of memory, and then the actual site running will take up quite a bit too. Since in this instance I’ve got a big DB that I want kept mainly in RAM I’ll set the Swappiness quite low.

sudo nano /etc/sysctl.conf

then either find or change this line:

vm.swappiness = 60

to whatever you choose. I made it 20 as a start.

Next you’ll want to reboot the system. Once it’s back online run

cat /proc/sys/vm/swappiness

again and make sure it returns the number you input.

closing code brace

How to kill a process by its name in Ubuntu

Sometimes you want to end a process in Ubuntu, but have no idea what the process number is – alternatively you may be writing a script where the process ID is obviously going to be dynamic. Here’s a quick guide to ending a process by its name via command line:

Some services has slightly obscure names, for instance if you’re running a test server with skip-grant-tables version of MySQL it’ll be called mysqld. You’ll want to check and make sure you’re killing the correct process

ps -aux

This shows you a list of all the services running.  If that’s a little too much, narrow it down a little. Grep will display all processes containing the string searched. The service mysqld will show up in this Grep search:

ps -aux | grep "mysql"

Now that you’ve got the correct name out of the service list you can use the killall command properly like so:

killall -v mysqld

run ps again to double check that it’s been killed properly.

closing code brace

My Cron job is running multiple times in Ubuntu

If your Cron is a wget command you’ll need to set it so that it “gets” only one time irrelevant of the http response. Wget will re-attempt a connection until it receives a 2XX response. It’s possible under some CMS that you’ll have jobs that run from a Wget’d page.

The correct syntax for using a Wget only one time is:

0 * * * * wget -t 1 ""

This tells the Wget command to only get the page one time. If your page is dynamically generated it can take longer than the minimum response from Wget, at which point the Cron will appear to run a second time (and a third, fourth, fifth, until your server is overloaded).

The first 5 characters are time in Minutes, Hours, Days, Weeks, Years. The above example runs on the hour every hour of every day, every week, every year.

closing code brace

How to copy contents of one directory into another directory in Ubuntu

This one’s quite simple, but is often forgotten. The syntax for copying from one directory to another in Ubuntu is:

cp -a /path/to/source/folder/. /path/to/destination/folder

The -a keeps the file attributes and symlinks, the period at the end of the source folder makes sure that you get hidden folders too (important for .htaccess/.htpasswd files).

If your directories have high level access privileges you might need to use the sudo command to enact root privileges.

Technical Jelly Limited

January 2014 – Present

I am currently the joint owner of Technical Jelly Ltd a software/server engineering company providing consultation and development services to SMEs and Enterprise level businesses, as well as third party support & overflow development services to software companies throughout the UK and Ireland. The company specialises in LAMP stack server & application management/optimisation, Web & Graphic Design, and general software engineering. I’m a RackSpace certified partner, but also have the ability to work in the Azure environment depending on our client requirements.

First4Websites rebrand & website relaunch

August – October 2013

Redesign and redevelopment of the First4Websites flagship website in line with the final stage of rebranding from First For Websites Ltd to First4Websites.

The site was a team effort, including input from all members of staff including the named members above, Laura Thompson, Sheila Nicholson, Reece Vail, and Corinne Spencer. Outside consultancy was offered from Mike Jobson from Oxford Innovations.

The site is designed and built using cutting edge CSS and HTML techniques in order to fully showcase the companies capabilities and rigid commitment to future development. The site uses CSS3 and HTML5 and is based on a stripped back of the WordPress CMS, and was designed to reflect that First4Websites, though being based in the countryside, has an infinite amount of inspiration and in-house design talent

Explore Berwick

September 2012 – Dec 2013

A flagship project at First4Websites to provide a unified portal for Berwick-upon-Tweed. The key aim of the project is to use the Design, Development, SEO, online marketing and sales experience within First For Websites to generate a modern user-friendly website to promote Berwick to locals and tourists alike.

Weeks Foods Website

February 2013 – April 2013

Creation of a website to promote and market Weeks Foods unique vegetarian alternatives to meat products, ready-made eggy bread, and Zambian Honey. The site uses a variety of WordPress plugins and bespoke JQuery functions to create an individual brand for an up and coming foods manufacturer.

The site is fresh, colourful, and provides a single portal for users to view the Weeks Foods brand. The site also has associated managed email accounts, and some Apache2 optimisations specifically for the large amount of image loading per page.

Farmstock Scotland

October 2012 – December 2013

Farmstock Scotland required an update to their website. The site needed to serve several functions, from an end-user editable brochure, all the way through to a cattle and sheep market. The site also contains a weekly newsletter breakdown of the current state of farming in Scotland, as well as a quarterly update on Farmstock itself.