Run apps as a service on Ubuntu

On April 3rd, 2021 in Tech

If you have a script (jar, py, or whatever) or a software that you need to run as a service and also if you need it to start automatically if/when system restarts, you can follow the instructions bellow.

I’ll show 2 examples of my own: Tabula and Archivebox. They all run on my Ubuntu Server VM.

Step 1 – Create a service

Create a service at “/etc/systemd/system”. You name it and use the text editor of your preference.

I’m using “nano” and named them “tabula.service” and “archivebox.service“.

For Tabula

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

Edit your file. You can use my code bellow, changing as necessary.

Note that lines beginning with a # are comments and your computer won’t execute them. You can delete them if you like. I also kept how I used in my case.

[Unit] # Add the description of your service # Description=Tabula Description=YOUR_DESCRIPTION [Service] # Change this to your workspace (where your script will run). I decided to keep my Tabula jar file on my home directory. # WorkingDirectory=/home/fsugi/tabula WorkingDirectory=PATH_TO_WORKING_DIRECTORY # Path to executable. Executable is a bash script which calls jar file. # NOTE: bash script usually ends with ".sh" but I didn't do that. That's why my script example is "tabula" # ExecStart=/home/fsugi/tabula/tabula ExecStart=PATH_TO_SCRIPT # Other options that you can change, if necessary. I suggest you keep as it. SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=30 [Install] WantedBy=multi-user.target
Code language: PHP (php)

For Archivebox

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

Edit your file. You can use my code bellow, changing as necessary.

Note that lines beginning with a # are comments and your computer won’t execute them. You can delete them if you like. I also kept how I used in my case.

[Unit] # Add the description of your service # Description=Archivebox service Description=YOUR_DESCRIPTION [Service] # If you need to run the service as a user, you must define them. If none is declared, root is the default. # User=fsugi # Group=fsugi User=USER Group=GROUP # Change this to your workspace (where your script will run). I decided to keep my Tabula jar file on my home directory. # WorkingDirectory=/home/fsugi/archivebox WorkingDirectory=PATH_TO_WORKING_DIRECTORY # Path to executable. Executable is a bash script which calls jar file. # NOTE: bash script usually ends with ".sh" but I didn't do that. That's why my script example is "archivebox" # ExecStart=/home/fsugi/archivebox/archivebox ExecStart=PATH_TO_SCRIPT # Other options that you can change, if necessary. I suggest you keep as it. SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=30 [Install] WantedBy=multi-user.target
Code language: PHP (php)

Step 2 – Create bash script to call your service

You must create now a bash script.

I’m using again “nano” as a editor. Notice that I’m not using “sudo” and I’m creating the script where I defined on the “.service” above.

For Tabula

nano /home/fsugi/tabula/tabula

Edit your file. You can use my code bellow, changing as necessary.

Note that lines beginning with a # are comments and your computer won’t execute them. You can delete them if you like. I also kept how I used in my case.

#!/bin/sh # This is the command that runs my jar file. Notice that I wrote the complete path to run "java" binary. The other atributes are parameters defined for running Tabula /usr/bin/java -Dfile.encoding=utf-8 -Xms256M -Xmx1024M -Dwarbler.port=1111 -jar /home/fsugi/tabula/tabula.jar
Code language: PHP (php)

For Archivebox

nano /home/fsugi/archivebox/archivebox

Edit your file. You can use my code bellow, changing as necessary.

Note that lines beginning with a # are comments and your computer won’t execute them. You can delete them if you like. I also kept how I used in my case.

#!/bin/sh # Notice that I wrote the complete path to run the "archivebox" binary. The other atributes are parameters defined for running the Archivebox. /usr/bin/archivebox server 0.0.0.0:7000
Code language: PHP (php)

Step 3 – Start service

Every time that you change a service, you must first reload them.

sudo systemctl daemon-reload

To test (or start) your service (I’m using tabula as example):

sudo systemctl start tabula.service
Code language: CSS (css)

To stop your service:

sudo systemctl stop tabula.service
Code language: CSS (css)

To enable your service to automatically load on start-up:

sudo systemctl enable tabula.service
Code language: CSS (css)

To disable your service to automatically load on start-up:

sudo systemctl disable tabula.service
Code language: CSS (css)

To check the status of your service:

sudo systemctl status tabula.service
Code language: CSS (css)

It’s always good to check the status of your service while setting it up to identify errors.

Additional – Logging

If you want to check all the log for your service (I’m using the tabula.service):

sudo journalctl --unit=tabula.service

You can tail the live log using the -f option:

sudo journalctl -f -u tabula.service
Code language: CSS (css)

Use -n <# of lines> to view specified number of lines of log

sudo journalctl -f -n 1000 -u tabula.service
Code language: CSS (css)
Tags: automations digital life

Automate my life

On March 13th, 2021 in Tech

One of my first non-listed project of this year is to automate the boring stuffs of my life. I have an urge to control every aspect of my life and this makes me create a lot of small, tedious and time consuming tasks every day.

Here I show some of the automations that I implemented on my daily life:

Home

Home Assistant

We automated some outlets at home.

  1. Every afternoon before sunset, the outdoor light turns on. It uses the sun position to determine at what time it must turn on. Not only we don’t need to bother turning on the light, but it also changes automatically according to time of the year (longer/shorter days on summer/winter).
  2. Our electric kettle turns off every night and turn on right before we wake up.
  3. We must keep the humidity level as constant as possible as different humidity levels may cause damage to the piano. I installed a humidity and temperature sensor on the humidifier and set it to turn on or off according to the humidity on the room.
  4. Our Home Assistant setup is not exposed to the internet. It only works on our private network (as planned). Nevertheless, it connects to the internet to control some devices that we own and to send notifications using Telegram bot. So I automated some actions to send me a telegram message warning the status change.

I plan to do many other automations and sensors in the future.

Digital Life

TrueNAS

My digital setup is ready. Our photos and videos are already properly stored and our most important documents are backed up using Backblaze B2 service.

Backblaze

I really enjoy the B2 Cloud Storage. There’s a one pricing table for all regions, you pay only for what you use and it is very affordable.

B2 cloud storage pricing (feb 2021)

I have more than 5TB of data stored on TrueNAS. Most of it are RAW photo files, but all the exported JPEG photos and documents occupy only 200GB.

You can use the Backblaze calculator to estime how much you would pay, this is my situation:

And yes, I’m hunting down subscriptions, but for 1.3 USD per month I can have my most important documents safely stored. I think it worths.

Note: this is not a paid review, but the link is an affiliated link.

Gmail

Every month I open and save all kinds of invoices and other documents I get by mail in my Google Drive. Usually they come as a PDF attachment. Wouldn’t it be nice to save those PDFs in Google Drive as well?

After a few research on Google, I automated it using an amazing Google Script from Andreas Hochsteger. It’s simple, fast and efficient.

Tabula

I’m running a Ubuntu Server VM on TrueNAS it’s running Tabula as a service.

Tabula is a tool for liberating data tables locked inside PDF files.

https://tabula.technology/

It only works on my private network, but it works just fine and makes my life much easier when working with tables.

Tags: automations digital life