Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 8. Process Management

Unit File

Unit File

Configuring Unit Files in Linux

Unit files are configuration files that contain directives for units. For example, when and how the unit will start, dependency on other units, which command will be triggered, etc. Unit files are usually saved under the /usr/lib/systemd/ directory or /etc/systemd/system directory. Edited unit files should be saved under the /etc/systemd/system directory while default unit files are kept under the /usr/lib/systemd/ directory.

Unit file structure

There are three sections in a unit file. The second section is a unit-type-specific section. For example, if the unit is a service unit, [Service] is the section’s name. If it is used for a socket unit, [Socket] is the section’s name. The unit-type-specific sections differ by type. Here are explanations for the unit file of a service unit.

  • [Unit]: This section is used to describe general directives that are not unit-type specific. Directives in this section include dependencies, order of unit file executions, conflicting units, etc.
  • [Service]: This section includes directives for commands to be triggered when the unit is started, stopped, or reloaded. It also covers unit behaviors when the process is completed.
  • [Install]: This section is an optional section and is used to set the behavior of a unit when it is enabled.

This is an example of a unit file for sshd.service.

sshd.service
[Unit]
Description=OpenBSD Secure Shell server
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
Alias=sshd.service

Example of Unit X's unit file

Explaining unit file syntax is complicated as there are many interdependent units that are related to the unit defined by the unit file. For a clearer explanation, we will refer to the unit defined by the unit file as 'Unit X'.

Linux unit file: unit relationship illustration

[Unit] section

This section is used to describe common directives including:

  • Description: a description of this unit.
  • Documentation: URI (Uniform Resource Identifier) of this unit document
  • After: units' start order information. Unit X starts only after the units listed here are active
Linux unit file: After directive
  • Before: units' start order information. Unit X should be active before the units listed here start.
Linux unit file: Before directive
  • Requires: units' dependency information. Unit X is dependent on the units listed here. If one of the units listed here fails to be activated, Unit X won't be activated
Linux unit file: Requires directive
  • Wants: units' dependency information. Unit X is less strictly dependent on the units listed here. Unit X can be activated even though the units listed here are not activated.
Linux unit file: Wants directive
  • Conflicts: the units listed here cannot be started when Unit X is running.

[Service] section

This section covers service unit-specific directives. If the unit is a socket unit or a timer unit, directive contents will be different from the directive contents described below. This section includes:

  • Type: the unit process startup type
  • EnvironmentFile: files listed here are used to support loading environment variables
  • ExecStart: commands listed here will be executed when Unit X is started
  • ExecStop: commands listed here will be executed when Unit X is stopped
  • ExecReload: commands listed here will be executed when Unit X is reloaded
  • Restart: set restart options – always, on-success, on-failure, etc.

[Install] section

This section is an optional section and is used to set the behavior of a unit when it is enabled. This section includes :

  • RequiredBy: when Unit X is enabled, this unit's symbolic link is created under the .requires directory of the units listed here, meaning that when the units listed here try to start, Unit X will be called.
Linux unit file: RequiredBy directive

WantedBy: when Unit X is enabled, this unit's symbolic link is created under the .wants directory of the units listed here, meaning that when the units listed here try to start, Unit X will be called.

Linux unit file: WantedBy directive

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Saving and Exiting Vim Editor Normal Mode

Normal Mode (7) – Save and Exit

Understanding Branches in Git with Diagrams

What Is Branch?

Setting Up Docker Environment

Setting Up Docker Environment

Updating Local Repository with git fetch

Get Remote Repository Information to Local Repository – Git Fetch

Searching Phrases in Vim Editor Normal Mode

Normal Mode (5) – Search Phrase

Saving and Exiting Vim Editor Normal Mode

Normal Mode (7) – Save and Exit

Understanding Branches in Git with Diagrams

What Is Branch?

Setting Up Docker Environment

Setting Up Docker Environment

Updating Local Repository with git fetch

Get Remote Repository Information to Local Repository – Git Fetch

Searching Phrases in Vim Editor Normal Mode

Normal Mode (5) – Search Phrase

Tags:

Service

Daemon Process

Unit File

Unit

systemctl

systemd

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server