Linux System Administration for Beginners

Operation of Running Systems

Use scripting to automate system maintenance tasks

Automating routine system maintenance on CentOS (or similar Linux distributions) saves time and reduces human error. In this guide, you’ll learn how to:

  • Write and run simple Bash scripts
  • Archive directories reliably
  • Manage multiple backup generations
  • Use exit statuses in conditions
  • Examine a real-world example (Anacron)

Table of Contents

  1. Understanding the Bash Shell
  2. Creating Your First Script
  3. Automating Backups
  4. Using Exit Status in Conditions
  5. Real-World Example: Anacron
  6. Quick Reference: Shell Constructs
  7. Further Resources

Understanding the Bash Shell

When you log in, you land at a shell prompt managed by bash, the Bourne Again SHell. It interprets commands you type or reads and executes commands from a file (a script) in sequence.

$ date
Mon Dec  6 16:28:09 CST 2021

Bash supports redirection, pipelines, variables, loops, functions, and more—the same features you use interactively are available in scripts.


Creating Your First Script

Follow these steps to build and run a basic maintenance script.

1. Create the Script File

$ touch script.sh
$ vim script.sh

2. Add Script Contents

#!/bin/bash
# Log the date and time of execution
date >> /tmp/script.log
# Capture the current kernel version
cat /proc/version >> /tmp/script.log

Note

The first line (#!/bin/bash) is the shebang, telling the system which interpreter to use.

  • Lines beginning with # are comments.
  • >> appends output rather than overwriting.

3. Make the Script Executable

$ chmod u+x script.sh
# or allow everyone:
$ chmod +x script.sh

4. Run and Verify

$ ./script.sh
$ cat /tmp/script.log
Mon Dec  6 17:06:16 CST 2021
Linux version 4.18.0-348.2.1.el8_5.x86_64 ...

Automating Backups

Backup scripts are ideal for automating directory archiving. Below are two approaches: a simple archive and one that retains the previous generation.

Archiving a Directory

Create archive-dnf.sh:

#!/bin/bash
tar acf /tmp/archive.tar.gz /etc/dnf
$ chmod +x archive-dnf.sh
$ ./archive-dnf.sh
$ ls /tmp
archive.tar.gz

You can inspect the archive with:

tar tf /tmp/archive.tar.gz

Keeping Two Generations of Backups

To avoid overwriting a good backup, rename the old archive before creating a new one.

  1. Save this as archive-dnf-2.sh:

    #!/bin/bash
    if test -f /tmp/archive.tar.gz; then
        mv /tmp/archive.tar.gz /tmp/archive.tar.gz.OLD
        tar acf /tmp/archive.tar.gz /etc/dnf
    else
        tar acf /tmp/archive.tar.gz /etc/dnf
    fi
    
  2. Make it executable and run:

    $ chmod +x archive-dnf-2.sh
    $ ./archive-dnf-2.sh
    $ ls /tmp
    archive.tar.gz
    archive.tar.gz.OLD
    

Warning

Moving or deleting files in /tmp can remove critical data if misused. Always verify paths and filenames before executing backup scripts.


Using Exit Status in Conditions

Every command returns an exit status: 0 (success) or nonzero (failure). You can leverage this in if statements:

#!/bin/bash
if grep -q '5' /etc/default/grub; then
    echo 'Grub has timeout of 5 seconds.'
else
    echo 'Grub DOES NOT have a timeout of 5 seconds.'
fi
  • grep -q runs quietly (-q) and sets exit status accordingly.
  • Save as check-grub-timeout.sh, make it executable, then:
$ chmod +x check-grub-timeout.sh
$ ./check-grub-timeout.sh
Grub has timeout of 5 seconds.

Real-World Example: Anacron

Inspect /etc/cron.hourly/0anacron to see conditionals, loops, and file checks in action:

#!/bin/sh
# Check whether @anacron ran today
if test -r /var/spool/anacron/cron.daily; then
    day=$(cat /var/spool/anacron/cron.daily)
fi
if [ "$(date +%Y%m%d)" = "$day" ]; then
    exit 0
fi

# Skip jobs when on battery power
online=1
for psupply in AC AD0*; do
    sysfile="/sys/class/power_supply/$psupply/online"
    if [ -f "$sysfile" ]; then
        if [ "$(cat "$sysfile" 2>/dev/null)" = 1 ]; then
            online=1
            break
        else
            online=0
        fi
    fi
done
if [ "$online" = 0 ]; then
    exit 0
fi
# …rest of the script…

Quick Reference: Shell Constructs

ConstructUse CaseExample
shebangSelect interpreter#!/bin/bash
if … thenConditional executionif grep -q 'foo' file; then echo yes; fi
for … doIterate over listsfor file in *.log; do gzip "$file"; done
>>Append redirectiondate >> /tmp/script.log
test -fCheck file existenceif test -f /path/to/file; then …
exit statusCheck command success/fail`command && echo success

Further Resources

Make sure to explore bash built-ins (help) and system scripts under /etc/cron.* for more real-world patterns and advanced techniques.

Watch Video

Watch video content

Previous
Install configure and troubleshoot bootloaders