Installing Glances web server to run as a service on Ubuntu Linux

Glances screenshot from the Glances documentation

I wanted to have an easy way to monitor how my server is doing. Glances is a software that gives you a good overview of your system. So I decided to install it.

I have a headless Ubuntu Linux server machine for random programming and hobby projects. It runs a web server and a couple of other services. With "headless" I mean the server doesn't have a monitor or a desktop environment.

I also have Home Assistant installed on a Raspberry Pi 4. I wanted to bring the monitoring data there for easy review.

Glances allows one to do that. It provides a web interface for directly checking the status of the machine and an API for Home Assistant to gather status data.

Glances installation the Ubuntu way is problematic


I first tried to install Glances using the built-in package manager apt. This proved to be a bad idea for two reasons.

First off, the Glances version that you will get with apt is outdated.

Especially if you have an older Ubuntu, such as 18.04 LTS or 20.04 LTS, you will get a version of Glances that does not work like the documentation states, might have some bugs in it, or might be missing some features. Even the Glances version that apt installs on Ubuntu 22.04 is not going to be the newest version.

The second problem is that on the newer Ubuntu releases (22.04 for example) the Glances installation is missing some javascript files that are necessary for the Glances web server to work. This means you will get a blank white page if you open the http://server.ip.address.here:61208 address with a web browser. If you check the developer console of your web browser, it will show an error that a .js file could not be found.

If you still want to install Glances using this method, just do the following:

sudo apt update
sudo apt install glances


But note that Glances will be installed in a different path so you have to adapt the later instructions in this guide to reflect that.


Glances installation with PIP


The better way is to install glances with PIP.

You should do this with your personal username. The root user shouldn't be used - otherwise the process will have root privileges, which is a potential security risk. You can also mess up the Python installation of your distribution if you use pip as root.

To install glances, run this command:
python3 -m pip install glances


Getting Glances to run as a service (to survive a reboot)


Now when we have installed Glances as a user, we will also have to start the service with the same username.

The Glances executable is located in
/home/LINUX_USERNAME/.local/bin/glances

You can find that out with the command
which glances

We need to create a service for Glances:
sudo nano /etc/systemd/system/glances.service

Enter the following text and then exit by pressing Ctrl-X and then Y to save the modified file.

[Unit]
Description=Glances
After=network.target

[Service]
User=LINUX_USERNAME
Group=LINUX_USERNAME
ExecStart=/home/LINUX_USERNAME/.local/bin/glances -w
Restart=on-abort
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target


You can then start glances with
sudo systemctl start glances

If you instead used apt to install Glances, you can find some example services here.


Editing the Glances configuration file


The Glances configuration file resides in
/etc/glances/glances.conf

After editing the configuration, you should restart the glances service for the changes to take effect:
sudo systemctl restart glances


How to set a user and password for Glances web server


Stop the glances service if it is started:
sudo systemctl stop glances

Start glances with
glances -w --username --password

You will be prompted for a glances username and password. After that, hit ctrl-c to stop the Glances server. You can then edit the ExecStart line in the Glances service to look like this:

ExecStart=/home/LINUX_USERNAME/.local/bin/glances -w -u GLANCES_USERNAME

Now start the Glances service and you're good to go:
sudo systemctl start glances

Comments

Popular posts from this blog

How to fix the weird audio bug in Kingdom Come: Deliverance

How to install and play Curse of Monkey Island on Android

How to get rid of coil whine just by tweaking BIOS

Where does uTorrent keep its .torrent files?

Minecraft recv failed error... fixed!

Stop the CxUiUSvc service from eating your RAM