Skip to content

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)

RunlevelDescription
0Halt (shutdown)
1Single-user mode (maintenance)
2Multi-user mode (no networking)
3Multi-user mode (with networking)
4Unused (custom)
5Multi-user + GUI (graphical login)
6Reboot

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/ (where X is the run level).

πŸ”Ή Example Commands (SysVinit)

Terminal window
# Check current run level
runlevel
# Change to run level 3
sudo 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:

Terminal window
# Show current target
systemctl 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

TermSysVinitsystemd
Run level 3CLI, multi-usermulti-user.target
Run level 5GUI logingraphical.target
Run level 0Shutdownpoweroff.target

2. Shutdown

2.1 Basics of shutdown command

πŸ”Ή 1. Halt the System Immediately

Terminal window
sudo shutdown -h now
  • -h = halt (shut down and power off)
  • now = do it immediately

πŸ“Œ Alternative:

Terminal window
sudo poweroff

πŸ”Ή 2. Reboot the System in 1 Minute

Terminal window
sudo shutdown -r +1
  • -r = reboot
  • +1 = wait 1 minute before rebooting

πŸ’‘ You can also add a message:

Terminal window
sudo shutdown -r +1 "Rebooting in 1 minute for maintenance"

🧯 3. Cancel a Scheduled Shutdown (if you change your mind):

Terminal window
sudo shutdown -c

2.2 systemctl Equivalents

Here are the systemctl equivalents to the shutdown commands:

πŸ”Ή 1. Halt the System Immediately

Terminal window
sudo systemctl halt
  • Halts the system β€” stops all CPUs but does not necessarily power off.

πŸ”Ή To Power Off (Shutdown) Immediately

Terminal window
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:

Terminal window
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:

Terminal window
(sleep 60 && sudo systemctl reboot) &

πŸ”Ή Cancel a Scheduled Reboot (if using shutdown -r +X)

Terminal window
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:

Terminal window
sudo shutdown -h now

βš™οΈ 2. Why systemctl is used

  • systemctl is part of systemd, 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:

Terminal window
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 for systemctl.

  • You can check this:

    Terminal window
    which shutdown
    ls -l $(which shutdown)

    It often links to systemctl or is configured to call it under the hood.

βœ… So, which should you use?

CommandUse when you want…Notes
shutdownA familiar, older-style syntaxStill works fine, good for scheduling
systemctlModern, explicit control with systemd featuresPreferred 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 TypeCommand ExampleDescription
Start servicesudo systemctl start nginxStarts the nginx service right now
Stop servicesudo systemctl stop sshdStops the SSH service
Restart servicesudo systemctl restart apache2Restarts a service
Enable servicesudo systemctl enable bluetoothStart at boot
Disable servicesudo systemctl disable bluetoothDon’t start at boot
Check statussystemctl status cupsShows whether a service is active/running
Show all unitssystemctl list-unitsLists active services, mounts, etc.
Shutdown systemsudo systemctl poweroffShuts down the computer
Reboot systemsudo systemctl rebootReboots the computer
Change targetsudo systemctl isolate multi-user.targetChange run level (e.g., GUI ↔ CLI)

3.3 πŸ“Œ Targets vs Runlevels

In systemd, targets replace old runlevels:

Runlevel (SysV)systemd Target
0poweroff.target
1rescue.target
3multi-user.target
5graphical.target
6reboot.target

3.4 βœ… Summary

  • systemctl = the control tool for systemd.
  • 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
TermMeaning
cronThe overall scheduling system
crondThe background daemon that runs jobs
crontabThe 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
CommandWhat it does
sudo systemctl start crondStart the cron service
sudo systemctl stop crondStop it
crontab -eEdit your user’s crontab file
crontab -lList 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:

Terminal window
sudo systemctl stop crond

βœ… Restart the cron service:

Terminal window
sudo systemctl restart crond

You can also use status to verify:

Terminal window
systemctl status crond

4.2 πŸ“… Scheduling mybackup.sh in /etc/crontab

Let’s say your backup script is located at:

Terminal window
/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

Terminal window
chmod +x /home/username/mybackup.sh

πŸ“„ Step 2: Edit /etc/crontab with root

Open the file with:

Terminal window
sudo nano /etc/crontab

You’ll see lines like this:

SHELL=/bin/bash
PATH=/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 = minute
  • 22 = hour (10PM)
  • * * * = every day/month/week
  • username = 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 with journalctl -u crond.