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