Category Archives: Ubuntu

Saving power on Ubuntu Server

I’ve been meaning to look into this for a while. My Ubuntu server acts as a NAS drive. As such it’s on all the time, but really it needn’t be. So let’s look at what we can do to save some energy by “hibernating” late in the evening and restarting early in the morning…

Let’s start with the /usr/sbin/rtcwake command. This application will tell your Linux OS to go into a sleep/hibernate state and you supply it with a time to wake back up, for example

/usr/sbin/rtcwake -m off 

The -m switch specifies the type of suspend. Options include

  • standby this option is the least energy efficient option and is the default option if you do not set the -m switch, it’s also very fast at resuming a system from sleep.
  • mem this option suspends to RAM, this puts everything into a low powered state (except RAM) and offers significant savings in energy.
  • disk this option suspends disk operations, the contents of memory are written to disk then restored when the computer wakes up.
  • off this option turns the computer off completely
  • no this option allows you to specify the awake time but doesn’t suspect the computer. The idea is that the user would put the machine into hibernation
    and rtcwake would then wake it up again at the specified point in time.

We might wish to set the resume time using either seconds (i.e. how many seconds in the future does the system resume) using the -s options, for example to wake up 10 hours after going to sleep we would specify 36000 seconds, like this

/usr/sbin/rtcwake -m off -s 36000

or to specify a time we use the -t switch which requires the number of seconds since Unix epoch (i.e. Jan 1st 1970, 00:00:00) so we still need to calculate to seconds

/usr/sbin/rtcwake -m off -t $(date +%s -d ‘tomorrow 07:00’)

If your hardware clock is set to local time, you’ll want to use the -l switch, for example

/usr/sbin/rtcwake -m off -l -t $(date +%s -d ‘tomorrow 07:00’)

Below is a script taken from StackOverflow. Just save this script as wakeup.sh or whatever file name and chmod to executable.

#!/bin/sh

# unix time in s that we should wake at
waketime=$(date +%s -d '07:00')

# make sure the time we got is in the future
if [ $(date +%s) -gt $waketime ]; then

        # if not add a day 60s * 60s * 24h
        waketime=$(($waketime + 86400))
fi

# time in S from now till then
dif=$(($waketime - $(date +%s)))

# tell the user
echo
echo "Current Time      :  $(date +%s)"
echo "Wake Time         :  $waketime"
echo "Seconds From Now  :  $dif"
echo
echo "Ctrl-C to Cancel in 5s"
echo
sleep 5

# sleep with rtcwake
/usr/sbin/rtcwake -m off -l -s $dif

echo "Shutdown."#!/bin/sh

But wait, we can run this rtcwake command (or the above script) manually but for my scenario I’d really want want it run automatically. So we add a cronjob as sudo (as rtcwake will need to be run as sudo) i.e.

sudo crontab -e

Now add the following

0 0 * * * /home/putridparrot/wakeup.sh > /home/putridparrot/wakeup.log

As per crontab expectations the arguments are as follows

  • MIN: Minute 0-60
  • HOUR: Hour, 24 hour clock hence 0-23
  • MDAY: Day of the Month, 1-31
  • MON: Month, 1-12 or jan, feb…dec
  • DOW: Day of the week, 0-6 or sun, mon, tue…sat
  • COMMAND: The command to run

So with this knowledge, our script will run at 12:00am every day. I found it useful to create a log of the output of the script, just to ensure I could see it had run. Remember > will create a new file overwriting any existing file or you could use >> to append to an existing file if you prefer (but then you might want a cronjob to delete the file periodically).

You can also check the syslog for CRON jobs via

grep CRON /var/log/syslog

Stopping my Ubuntu server from hibernating

I’m not sure what’s happened, but after a recent upgrade to my Ubuntu server – when I tried to connect to the server (which I use as both a dev server and NAS), I couldn’t log into via ssh but when I attached a monitor and keyboard to the server I was able to use it and the ssh started working. However after a period of inactivity the server seemingly went offline, pressing the power switch briefly bought it back online. So some how it had gone sleep/hibernate mode – not very useful thing for a server.

Here’s some steps I went through to eventually get this working again…

Check the logs

Checking the logs is always a useful first step, so let’s take a look at the syslog

nano /var/log/syslog

or use

tail -f /var/log/syslog

I spotted log entries saying the machine was going to sleep.

Checking and then disabling sleep mode

Now use systemctl to confirm the status of the sleep.target, i.e. is it enabled

sudo systemctl status sleep.target

To disable sleep, we probably want to disable all options that hibernate/sleep so we can disable the lot using

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

If for some reason you wish to re-enable these, just run

sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

I’m still none the wiser as to how hibernate got enabled – but at least I’ve learned how to check and disable it.

Installing Erlang on Ubuntu 20.x

The steps below are taken from the sites listed in the reference, recreating here for my reference.

Installing

sudo apt update
sudo apt install software-properties-common apt-transport-https
wget -O- https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | sudo apt-key add -
echo "deb https://packages.erlang-solutions.com/ubuntu focal contrib" | sudo tee /etc/apt/sources.list.d/erlang.list
sudo apt update
sudo apt install erlang

Checking it all worked

Let’s check if everything worked. You can just run

erl

from your terminal OR let’s write the good ol’ hello world by create helloworld.erl and add the following code

-module(helloworld). 
-export([start/0]). 
start() -> io:fwrite("Hello World\n").

Run this using

erlc helloworld.erl
// then
erl -noshell -s helloworld start -s init stop

If all went well you’ll see Hello World written to standard io.

To add the last bit of the How to Install Erlang in Ubuntu post, to remove Erlang run

sudo apt remove erlang
sudo apt autoremove

References

How To Install Erlang on Ubuntu 22.04|20.04|18.04
How to Install Erlang in Ubuntu

Installing Kubernetes on Ubuntu

In this post I’m just going to list the steps for installing Kubernetes (k8s) on Ubuntu server using the single node, lightweight MicroK8s.

  • If you don’t have it installed, install snap

    sudo apt install snapd
    

Now let’s go through the steps to get microk8s installed

  • Install microk8s

    sudo snap install microk8s --classic
    
  • You may wish to change permissions to save using sudo everytime, if so run the following and change {username} to your user name.

    sudo usermod -a -G microk8s {username}
    sudo chown -f -R {username} ~/.kube
    
  • Verify the installation
    sudo microk8s.status
    
  • To save us typing microk8s to run k8s command let’s alias it
    sudo snap alias microk8s.kubectl kubectl
    

Accessing the Dashboard

Whilst you can now run kubectl command, the Web based dashboard is really useful – so lets deploy it using

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.1.0/aio/deploy/recommended.yaml

By default the Dashboard can only be accessed from the local machine and from a security standpoint you may even prefer not to be running a dashboard, but if you decide you want it (for example for dev work) then…

As per Accessing Dashboard

kubectl -n kubernetes-dashboard edit service kubernetes-dashboard

Change type: ClusterIP to type: NodePort and save this file.

You’ll need to run the proxy

kubectl proxy&

Now to get the port for the dashboard, run the following

kubectl -n kubernetes-dashboard get service kubernetes-dashboard

Example output (take from Accessing Dashboard)

NAME                   TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes-dashboard   NodePort   10.100.124.90   <nodes>       443:31707/TCP   21h

You can now access the dashboard remotely now on the port 31707 (see the PORT(S) listed above), for example https://{master-ip}:31707 where {master-ip} is the server’s ip address.

To get the token for the dashboard we need to set up and few things

kubectl describe secret -n kube-system | grep deployment -A 12

Then copy the whole token to the Dashboard Token edit box and login.

To enable skipping of the requirement for a token etc. on the dashboard (should only be used on a development installation) run

kubectl edit deployment kubernetes-dashboard -n kubernetes-dashboard

then add the following line

- args:
   - --auto-generate-certificates
   - --namespace=kubernetes-dashboard
   - --enable-skip-login  # this argument allows us to skip login

Setting up Swift on Ubuntu 18.04

Let’s setup a swift development environment on Ubuntu 18.04. “Why?”, you might ask, as swift was written by Apple for Mac and iOS development and I do happen to have a Apple Mac with everything installed there, my answer is “why not”, let’s give it a try.

  • Go to https://swift.org/download/#releases and locate the version of Swift you want to download, I picked Swift 5.3.2, Ubuntu 18.04. Download this to your machine.
  • From ~./Downloads run
    tar -xvzf swift-5.3.2-RELEASE-ubuntu18.04.tar.gz 
    

    Obviously replace the .tar.gz with whatever version you download.

  • Now would probably be a good time to move the resultant decompressed folder to where you want it to be kept, mine’s in a ~/Home/swift directory.
  • Open .bashrc and add the following line (or just export the path if you want it temporary without adding to .bashrc)
    export PATH=$PATH:$HOME/swift/swift-5.3.2-RELEASE-ubuntu18.04/usr/bin
    

    Don’t forget to save the .bashrc file if you’ve gone that route and ensure the path to swift usr/bin matches where you moved your files to

  • I’m going to use VSCode, as my editor, and there’s several Swift extensions, I installed Swift Language 0.2.0. This has the largest number of downloads, I’ve no idea how good this is compared to others, but that’s the one I’ve installed for now.
  • If all went well, open a terminal window in VSCode or just use the a bash terminal and type
    swift --version
    

    If all went well you’ll see the Swift version and Target listed

Getting Started

Now we have swift installed (hopefully), let’s look at the sort of “Getting Started” type of things you’ll want to try.

Let’s use the swift command to create a simple executable application. So run the following for your terminal

swift package init --type executable

This will use swift’s package manager to create a new executable application with good old “Hello World”.

To build this, simple execute the following command

swift build

and to run this we simply execute the command

swift run

When making changes to your code you can actually just save the file(s) and use the run command which will build and run the application.

The command which generated this source created a Package.swift file which is where we add dependencies etc. Source code is stored in the Sources folder, and here the file is named main.swift which simply contains

print("Hello World!")

In the Tests folder we have the tests for the application. We’re not going to go into those now except to say, you can run the following command to run the tests

swift test