How would I install psensor-server as a service on Ubuntu 16.04 LTS

I am looking for the ability to create a service to run psensor-server on startup and be able to restart it using systemctl if possible?


Solution 1:

I have tried to find a place to actually post a tutorial on this, wiki wouldn't load for me to add a page, and I really don't know where it goes. But I wrote a tutorial for this and I'm by no means an expert but this was how I managed to get this working on my server without having to open a terminal and execute the start command every time. If anyone can think of a better place to put this, or knows where it should be please do let me know...

I'm aware some have said that ppa isn't needed, you can skip this step if you wish as it is already in the universe repository, however adding the PPA will guarantee that you receive the latest stable updates when pushed by the psensor team.

This guide will show you how to install Psensor-Server on ubuntu 16.04 Server, and then create a service to launch it on a given port at system start

The first thing we need to do is be sure the system is up to date package wise, not sure this matters, but it's always good practice.

sudo apt update
sudo apt upgrade

The next thing is to install the prerequisites for Psensor

sudo apt install lm-sensors

Start the detection of your hardware sensors

sudo sensors-detect 

It will ask a series of questions, answer yes to everything unless you know what you need.

Verify it works by running the following:

sensors

Output should look something like this, however please note yours will vary based on how many sensors you have in your PC so your reading will not be exact.

coretemp-isa-0000
Adapter: ISA adapter
Core 0: +46.0C (high = +76.0C, crit = +100.0C)

coretemp-isa-0001
Adapter: ISA adapter
Core 1: +44.0C (high = +76.0C, crit = +100.0C)

After this we will install psensor and psensor-server We will use the repository to be sure we have the latest stable version. Install the prerequisites for add-apt if their not already present on your machine.

sudo apt install software-properties-common python-software-properties

Next step is adding the repositories

sudo add-apt-repository ppa:jfi/psensor

Update the Repositories sudo apt update Install Psensor and Psensor-Server

sudo apt update && sudo apt install psensor psensor-server

Now that we have Psensor-server installed we have to write a couple scripts to start and stop the service.
Warning, the stop script just simply uses pkill which I've been made aware that probably isn't the best option, but for my purposes this wasn't an issue, you may need to adapt the script to your needs accordingly.

the first script is created as follows

I use nano, you may use whatever editor you prefer. If you do not have nano you can obtain it by the following.

sudo apt install nano

then

sudo nano /home/someuser/startps.sh

replace someuser with your username

when it opens type the following

#!/bin/bash
sudo psensor-server --port=8085

You may use whatever port you wish, just ensure it doesn't conflict with ports already open on the system.

CTRL + X and answer yes to save

then issue

sudo nano /home/someuser/stopps.sh

Again replace someuser with your user account.

enter the following

#!/bin/bash
sudo pkill psensor-server

CTRL + X and answer yes to save.

To create the service issue the following command

sudo nano /etc/systemd/system/startps.service

Enter the Following

[Unit]

Description=psensor service



[Service]

ExecStart=/home/someuser/startps.sh
ExecStop=/home/someuser/stopps.sh



[Install]

WantedBy=multi-user.target

Replace someuser in the paths to the same username you used when creating the .sh scripts.
CTRL + X and yes to save.
Then enter the following at your terminal

sudo chmod u+x /home/someuser/startps.sh && sudo chmod u+x /home/someuser/stopps.sh

Again remember to replace someuser with your username.

Now to start our service we type

sudo systemctl start startps

it should start and take you back to the terminal prompt, at which time we enter

sudo systemctl status startps

The output should be as follows

 startps.service - psensor service
   Loaded: loaded (/etc/systemd/system/startps.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-08-10 16:42:43 CDT; 2s ago
  Process: 15300 ExecStop=/home/someuser/stopps.sh (code=exited, status=203/EXEC)
 Main PID: 15385 (startpf.sh)
    Tasks: 4
   Memory: 1.9M
      CPU: 21ms
   CGroup: /system.slice/startps.service
           ├─15385 /bin/bash /home/someuser/startpf.sh
           ├─15387 sudo psensor-server --port=8085
           └─15388 psensor-server --port=8085

Aug 10 16:42:43 [hostname] systemd[1]: Started psensor service.
Aug 10 16:42:43 [hostname] sudo[15387]:     root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/bin/psensor-server --port=8085
Aug 10 16:42:43 [hostname] sudo[15387]: pam_unix(sudo:session): session opened for user root by (uid=0)

Please note where it says disabled, it should also tell you the process is running.
Now to enable your service type

sudo systemctl enable startps

if it returns to the terminal you may issue

sudo systemctl status startps

You should now see this response.

 startps.service - psensor service
   Loaded: loaded (/etc/systemd/system/startps.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-08-10 16:42:43 CDT; 45min ago<br>
 Main PID: 15385 (startpf.sh)
   CGroup: /system.slice/startps.service
           ├─15385 /bin/bash /home/someuser/startps/startps.sh
           ├─15387 sudo psensor-server --port=8085
           └─15388 psensor-server --port=8085

Aug 10 16:42:43 [hostname] systemd[1]: Started psensor service.
Aug 10 16:42:43 [hostname] sudo[15387]:     root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/bin/psensor-server --port=8085
Aug 10 16:42:43 [hostname] sudo[15387]: pam_unix(sudo:session): session opened for user root by (uid=0)

The main thing you're looking for here is that it says enabled in the second line.

If you see these outputs, then lets move onto the next step which is opening the firewall to allow access from the client machine within our network.

I feel you should know that psensor-server with it's built in HTTP server has no security measures what so ever. So essentially if you open it to the outside world you do so at your own risk, and I assume no responsibility should a hacker frag your computer That is why I specifically included the next part of this guide to show you how to open the port in iptables to one client machine, you may adapt this as many times as you like.

so to open the port to a client machine you would use the following

sudo iptables -I INPUT -p tcp -s x.x.x.x --dport 8085 -j ACCEPT 

Keep in mind x.x.x.x would be the IP of your client machine you want to access the psensor-server http page.

Also if you set a different port after the -dport flag you would need to adapt 8085 to the port you chose in the creation of the startps.sh in the steps above.

Now you should be able to go to the client machine and type http://x.x.x.x:8085 wherein x.x.x.x is your server's IP and 8085 is the port you set in the creation of startps.sh in the steps above.

The following guides were used in creating this guide. The commands were adapted to remove apt-get as apt is now the default way to install software in ubuntu 16.04 https://wpitchoune.net/psensor/
https://wpitchoune.net/psensor/README.html
http://lifeonubuntu.com/ubuntu-missing-add-apt-repository-command/
https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
How do I run a single command at startup using systemd?
https://www.garron.me/en/bits/iptables-open-port-for-specific-ip.html
As well as the #ubuntu-server irc channel.
I hope this helps someone who wants to do what I did but doesn't have the know how. Please note this guide was obviously meant for users like myself who really don't know much about Ubuntu and/or the terminal window. If anyone has an easier way feel free to note it as an answer and I'll update as I get time.