How to Run a Linux Program at Startup with systemd

Trending 1 week ago

Key Takeaways

  • systemd is simply a package that allows you to motorboat Linux programs astatine footwear connected astir Linux distributions, including Ubuntu.
  • You tin easy create an integrated work utilizing systemd, which tin beryllium controlled utilizing systemctl.
  • systemd allows your programme to constitute to nan journal, making it easier to way and show nan service's activities.

Need to motorboat a Linux programme astatine boot? The systemd package provides a measurement to do it connected immoderate Linux distro pinch systemd — which is astir of them these days, including Ubuntu. We'll locomotion you done nan process of creating an integrated work —and it moreover talks to nan journal.

Running Programs astatine Startup pinch systemd

Sometimes nan package you instal connected your machine hooks itself into nan Linux startup process, truthful that nan programme is automatically launched each clip nan machine is started. You tin easy execute this aforesaid behaviour pinch your ain programs and scripts, aliases successful truth immoderate different programme that is connected your computer. This is useful if you're hosting some benignant of server you want to tally arsenic soon arsenic nan instrumentality is powered up (like a Minecraft Server!), aliases if you person different typical package you want to tally without needing to commencement it manually.

The programs that are launched astatine startup are controlled by systemd, nan system and service manager. systemd is nan first process to tally astatine startup. It ever has process ID (PID) 1. Every different process moving successful your machine is started by systemd, aliases by a process that systemd has already started.

Programs that tally successful nan inheritance are called daemons aliases services. The "d" astatine nan extremity of systemd stands for daemon. In this article, we'll create an illustration service. To tick each nan boxes, our work must be:

  • Integrated pinch systemd done a work portion record
  • Launched astatine startup
  • Controllable utilizing systemctl, nan control interface for systemd
  • Able to constitute to nan diary

We'll show really to group up a strategy work that launches erstwhile you commencement your strategy pinch systemd. To motorboat a graphical programme erstwhile you motion in, use your desktop's startup head instead.

Creating nan Service Program for systemd to Start

We request to person a programme that systemd will launch. We'll create a elemental script, called "". This tutorial uses nan Gedit matter editor, but you tin usage immoderate matter editor you prefer.

touch gedit
touch successful a terminal window

The gedit editor will open. Copy and paste nan pursuing matter into nan editor.

#!/bin/bashecho "htg.service: ## Starting ##" | systemd-cat -p infowhile :doTIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')echo "htg.service: timestamp ${TIMESTAMP}" | systemd-cat -p infosleep 60done

Save your changes and adjacent nan editor. book successful nan gedit editor

The book doesn't do a full lot, but location are a fewer points worthy noting.

  • The 2 echo lines are piped done systemd-cat, a programme that takes nan output from a programme and sends it to nan journal. Entries to nan diary are fixed a priority. We're utilizing nan -p (priority) action to bespeak that our messages are for accusation (info) only. They're not important errors aliases warnings.
  • There is an endless while loop.
  • The TIMESTAMP adaptable is group to nan existent day and time. This is formatted into a connection and sent to nan journal.
  • The book past sleeps for 60 seconds.
  • After 60 seconds nan loop is repeated. So, this book writes a timestamped connection to nan diary erstwhile per minute.

We'll transcript nan book to nan /usr/local/bin directory.

sudo cp /usr/local/bin
sudo cp /usr/local/bin successful a terminal window

And we request to make it executable:

sudo chmod +x /usr/local/bin/
sudo chmod +x /usr/local/bin/ successful a terminal window

Creating nan Service Unit File

Each programme that is started by systemd has a meaning file, called a work portion file. This holds definite attributes that systemd tin usage to find and motorboat nan program, and to specify immoderate of its behavior.

We request to create a portion record for our caller service, but it is prudent to make judge nary of nan existing portion files person nan sanction we want to springiness our caller service.

sudo systemctl list-unit-files --type-service
sudo systemctl list-unit-files --type-service successful a terminal window

You tin scroll done the database of portion files, which is sorted alphabetically, and cheque that nan sanction you want to usage is not taken.

List of systemd portion files successful a terminal window

Our work is going to beryllium called "htg.service". No portion files person that name, truthful we tin proceed and create our portion file.

sudo gedit /etc/systemd/system/htg.service
sudo gedit /etc/systemd/system/htg.service successful a terminal window

The gedit editor will open. Copy and paste nan pursuing matter into nan editor:

[Unit]Description=How-To Geek Service[Service]Type=simpleExecStart=/usr/local/bin/htg.shRestart=on-failureRestartSec=10KillMode=process[Install]

Save your changes and adjacent nan editor.

htg.service portion record successful nan gedit editor

The entries person these meanings. These are emblematic entries. Our elemental work doesn't really request astir of them, but including them allows america to explicate them.

  • Description: This is simply a matter explanation of your service.
  • Wants: Our work wants — but doesn't require — nan web to beryllium up earlier our work is started.
  • After: A database of portion names that should beryllium started aft this work has been successfully started, if they're not already running.
  • Type: Simple. systemd will see this work started arsenic soon arsenic nan process specified by ExecStart has been forked.
  • ExecStart: The way to nan process that should beryllium started.
  • Restart: When and if nan work should beryllium restarted. We person group it to "on-failure."
  • RestartSec: How agelong to hold earlier attempting to restart nan service. This worth is successful seconds.
  • KillMode: Defines really systemd should termination nan process if we inquire systemctl to extremity nan service. We person this group to "process." This causes systemd to usage nan SIGTERM awesome connected nan main process only. If our work was a non-trivial programme alternatively of a elemental script, we would group this to "mixed" to guarantee that immoderate spawned processes were besides terminated.
  • WantedBy: We person this group to "", which intends nan work should beryllium started arsenic agelong arsenic nan strategy is successful a authorities wherever aggregate users tin log in, whether aliases not a graphical personification interface is available.

The portion record doesn't request to beryllium executable, but nan permissions connected nan portion record should restrict who tin edit it. You don't want a malicious aliases mischievous personification changing nan portion record truthful that it executes a different programme altogether.

This bid will springiness nan proprietor publication and constitute permissions, and publication permissions to nan group. Others will person nary permissions.

sudo chmod 640 /etc/systemd/system/htg.service
sudo chmod 640 /etc/systemd/system/htg.service successful a terminal window

We tin person systemctl cheque nan syntax of our portion record for us, moreover if nan work isn't moving yet. Any errors will beryllium reported. (Actually, nan ".service" portion is optional for astir commands.)

systemctl position htg.service
systemctl position htg.service successful a terminal window

No errors are highlighted, which intends our portion record is syntactically correct.

Starting nan Service Automatically pinch nan systemd Command

When you adhd a caller portion record aliases edit an existing one, you must show systemd to reload nan portion record definitions.

sudo systemctl daemon-reload

If you want a work to beryllium launched astatine startup you must alteration it:

sudo systemctl alteration htg

Enabling a work doesn't commencement it, it only sets it to beryllium launched astatine footwear time. To commencement nan work now, you must usage systemctl pinch nan commencement option.

sudo systemctl commencement htg
sudo systemctl daemon-reload successful a terminal window

Verifying nan Service

After manually starting nan work aliases aft rebooting nan computer, we tin verify that our work is moving correctly.

sudo systemctl position htg.service
sudo systemctl position htg.service successful a terminal window

The position of nan work is displayed for us.

Status of htg.service successful a terminal widnow
  • The greenish dot intends our work is up and moving smoothly.
  • The sanction of nan work is "htg.service", and nan agelong explanation is nan 1 that we provided successful nan portion file.
  • We're shown which portion record has been loaded "/etc/systemd/system/htg.service".
  • The work is active, and nan clip nan work was launched is listed for us.
  • Its PID is 7762.
  • There are 2 tasks associated pinch nan service.
  • A full of 928 Kibibytes of memory are being utilized by nan service.
  • The power group includes nan "" book and nan slumber command, which has been launched by "" Most of nan time, nan slumber bid will beryllium doing nan activity for this service.

We're besides shown nan past 10 diary entries produced by this service. Unsurprisingly, they are each 1 infinitesimal apart.

Stopping and Disabling nan Service

If you request to extremity nan service, you tin do truthful pinch this command:

sudo systemctl extremity htg.service
sudo systemctl extremity htg.service successful a terminal window

This stops nan service, but it doesn't forestall it from restarting adjacent clip nan machine is rebooted. To extremity nan work being launched astatine startup, you request to disable it:

sudo systemctl disable htg.service
sudo systemtctl disable htg.service successful a terminal window

If nan work is running, this bid doesn't extremity it. It simply tells systemd not to motorboat nan work astatine nan adjacent reboot.

If you want to extremity nan work and forestall it from launching astatine startup, usage some commands.

Service Tip

Make judge your programme runs arsenic expected earlier you effort to motorboat it arsenic a service.

Source Tutorials