8.2 System Management
1. π§ Run Level (System Management Concept β Traditional SysVinit Systems)
Run levels are predefined states of a Unix/Linux system that define what services and processes are running. Theyβre part of the traditional SysVinit initialization system (before systemd
became the standard).
πΉ Common Run Levels (SysVinit-based Systems)
Runlevel | Description |
---|---|
0 | Halt (shutdown) |
1 | Single-user mode (maintenance) |
2 | Multi-user mode (no networking) |
3 | Multi-user mode (with networking) |
4 | Unused (custom) |
5 | Multi-user + GUI (graphical login) |
6 | Reboot |
Note: Different distributions may define levels 2β5 differently.
πΉ How It Worked
- At boot, the system enters a default run level (defined in
/etc/inittab
). - Each run level starts/stops specific services using scripts in
/etc/rcX.d/
(whereX
is the run level).
πΉ Example Commands (SysVinit)
# Check current run levelrunlevel
# Change to run level 3sudo init 3
# Reboot the system (runlevel 6)sudo init 6
β οΈ Modern Systems (systemd)
Most modern Linux distributions now use systemd
, which replaces run levels with βtargetsβ (like graphical.target
, multi-user.target
, etc.).
Example systemd
equivalents:
# Show current targetsystemctl get-default
# Set to graphical target (like runlevel 5)sudo systemctl set-default graphical.target
# Change current target (no reboot)sudo systemctl isolate multi-user.target
β Summary
Term | SysVinit | systemd |
---|---|---|
Run level 3 | CLI, multi-user | multi-user.target |
Run level 5 | GUI login | graphical.target |
Run level 0 | Shutdown | poweroff.target |
2. Shutdown
2.1 Basics of shutdown
command
πΉ 1. Halt the System Immediately
sudo shutdown -h now
-h
= halt (shut down and power off)now
= do it immediately
π Alternative:
sudo poweroff
πΉ 2. Reboot the System in 1 Minute
sudo shutdown -r +1
-r
= reboot+1
= wait 1 minute before rebooting
π‘ You can also add a message:
sudo shutdown -r +1 "Rebooting in 1 minute for maintenance"
π§― 3. Cancel a Scheduled Shutdown (if you change your mind):
sudo shutdown -c
2.2 systemctl
Equivalents
Here are the systemctl
equivalents to the shutdown
commands:
πΉ 1. Halt the System Immediately
sudo systemctl halt
- Halts the system β stops all CPUs but does not necessarily power off.
πΉ To Power Off (Shutdown) Immediately
sudo systemctl poweroff
- This is the modern equivalent to
shutdown -h now
.
πΉ 2. Reboot the System in 1 Minute
systemctl
does not have a built-in delay, but you can use sleep
to schedule it:
sleep 60 && sudo systemctl reboot
- This waits 60 seconds, then reboots.
π If you want to run it in the background so your terminal isnβt stuck:
(sleep 60 && sudo systemctl reboot) &
πΉ Cancel a Scheduled Reboot (if using shutdown -r +X
)
sudo shutdown -c
2.3 Why systemctl
and shutdown
?
Both shutdown
and systemctl
can be used to shut down or reboot a Linux system β but they exist for different historical and technical reasons.
π§± 1. Why shutdown
exists
shutdown
is a classic command from SysVinit days.- It schedules or triggers system power actions (halt, poweroff, reboot).
- Itβs user-friendly and still widely used.
Example:
sudo shutdown -h now
βοΈ 2. Why systemctl
is used
-
systemctl
is part ofsystemd
, the modern init system used by most Linux distros today (like Ubuntu, Fedora, Arch). -
It replaces older tools (
shutdown
,reboot
,init
, etc.) with a unified interface to manage:- System boot and shutdown
- Services (daemons)
- System targets (replacing runlevels)
Example:
sudo systemctl poweroff
π 3. They both still work β but systemctl
is preferred in modern systems
-
On modern distros, the
shutdown
command is often just a wrapper forsystemctl
. -
You can check this:
Terminal window which shutdownls -l $(which shutdown)It often links to
systemctl
or is configured to call it under the hood.
β So, which should you use?
Command | Use when you want⦠| Notes |
---|---|---|
shutdown | A familiar, older-style syntax | Still works fine, good for scheduling |
systemctl | Modern, explicit control with systemd features | Preferred for scripting and consistency |
3. What is systemctl
?
systemctl
is the command-line tool used to interact with systemd
, the modern init system used by most Linux distributions (like Ubuntu, Fedora, Arch, Debian, etc.).
3.1 π§ What is systemd
?
systemd
is the software that boots your system, manages system services (like networking, cron, SSH), and handles shutdowns, logins, mounts, and more. Itβs the replacement for older init systems like SysVinit or Upstart.
3.2 π What You Can Do With systemctl
Task Type | Command Example | Description |
---|---|---|
Start service | sudo systemctl start nginx | Starts the nginx service right now |
Stop service | sudo systemctl stop sshd | Stops the SSH service |
Restart service | sudo systemctl restart apache2 | Restarts a service |
Enable service | sudo systemctl enable bluetooth | Start at boot |
Disable service | sudo systemctl disable bluetooth | Donβt start at boot |
Check status | systemctl status cups | Shows whether a service is active/running |
Show all units | systemctl list-units | Lists active services, mounts, etc. |
Shutdown system | sudo systemctl poweroff | Shuts down the computer |
Reboot system | sudo systemctl reboot | Reboots the computer |
Change target | sudo systemctl isolate multi-user.target | Change run level (e.g., GUI β CLI) |
3.3 π Targets vs Runlevels
In systemd
, targets replace old runlevels:
Runlevel (SysV) | systemd Target |
---|---|
0 | poweroff.target |
1 | rescue.target |
3 | multi-user.target |
5 | graphical.target |
6 | reboot.target |
3.4 β Summary
systemctl
= the control tool forsystemd
.- It lets you start/stop services, reboot/shutdown, and manage boot behavior.
- Itβs modern, consistent, and the preferred tool on most current Linux distros.
4. Managing Services with systemctl
Hereβs how you can handle both parts of your task using systemctl
and cron
on a modern Fedora Linux system:
To manage the cron
service (called crond
on Fedora):
4.1 π What is the cron
service?
The cron
service (called crond
on most Linux systems) is a background daemon that runs scheduled tasks automatically at specified times and intervals.
Think of it as Linuxβs built-in task scheduler β like Windows Task Scheduler.
π§ What It Does
- Runs commands or scripts at specific times (e.g., backups, cleanups, updates).
- Uses cron tables (
crontab
files) where users define what and when to run.
π§ Terminology
Term | Meaning |
---|---|
cron | The overall scheduling system |
crond | The background daemon that runs jobs |
crontab | The configuration file that lists jobs |
ποΈ Types of Crontabs
- System-wide:
/etc/crontab
- User-specific: managed with
crontab -e
(stored in/var/spool/cron/username
) - Package-specific: jobs placed in
/etc/cron.daily/
,/etc/cron.hourly/
, etc.
π§Ύ Example of a Crontab Entry
0 22 * * * /home/user/mybackup.sh
β‘οΈ This runs mybackup.sh
every day at 10:00 PM.
β Basic Commands
Command | What it does |
---|---|
sudo systemctl start crond | Start the cron service |
sudo systemctl stop crond | Stop it |
crontab -e | Edit your userβs crontab file |
crontab -l | List scheduled jobs for your user |
π οΈ Use Cases
- Automating backups
- Running cleanup scripts
- Scheduling periodic system tasks
- Sending reminder emails or reports
β Stop the cron service:
sudo systemctl stop crond
β Restart the cron service:
sudo systemctl restart crond
You can also use
status
to verify:
systemctl status crond
4.2 π
Scheduling mybackup.sh
in /etc/crontab
Letβs say your backup script is located at:
/home/username/mybackup.sh
You want it to run every day at 10:00 PM (22:00).
π Step 1: Make sure the script is executable
chmod +x /home/username/mybackup.sh
π Step 2: Edit /etc/crontab
with root
Open the file with:
sudo nano /etc/crontab
Youβll see lines like this:
SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
This shows the field order:
minute hour day_of_month month day_of_week user command
π§Ύ Step 3: Add your cron job
To run the backup every day at 10PM as user username
, add:
0 22 * * * username /home/username/mybackup.sh
πΉ Breakdown:
0
= minute22
= hour (10PM)* * *
= every day/month/weekusername
= the user to run the command as/home/username/mybackup.sh
= full path to the script
β Final Notes
- After editing
/etc/crontab
, no restart is required βcrond
automatically picks up the change. - You can view logs in
/var/log/cron
or withjournalctl -u crond
.