Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DI 2.3: Scripts that automate administration

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

The Friday job nobody likes

Every Friday, a technician checks twenty servers. Is the disk full? Did last night's backup run? Is the log full of errors?

It takes three hours. One Friday she is ill, and nobody does it. The next week a full disk stops the payroll.

A script does the same checks in seconds, the same way, every time.

Key words

WordWhat it means
Scripta small program that does a list of tasks for you
Automationgetting a computer to do a repeated task
Administrationlooking after a system: users, updates, disks and backups
Shella program where you type commands, such as bash
Loopcode that repeats the same steps
Variablea name that holds a value
Loga file where a program writes what happened
Cronthe Linux tool that runs a command at set times
Conditiona test in a script that decides which step to take

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can say why a technician writes scripts. I can read a short shell script and write a small Python one that checks a log.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Why script?

A technician scripts for three reasons. A script is fast: it runs in seconds. It is repeatable: it does the same steps each time, so nothing is forgotten. And it can run on its own, at night, using cron.

Scripts are used for administration: add ten users, check disk space, clean old files, copy backups, and restart a service.

You can write them in a shell such as bash, or in a language such as Python.

Run a first line

Press Start the lab, then press Run on each command. The lab is a Linux server, so the commands run in a real shell.

This one prints the name of the machine and the date. The name will be different on your machine.

Print the machine name and the date

Command
echo "Hello from $(hostname) on $(date +%F)"

A command for the lesson’s lab terminal. It is shown here and not run.

A condition: is the disk too full?

Command
used=$(df --output=pcent / | tail -1 | tr -dc 0-9); if [ "$used" -gt 80 ]; then echo "WARNING: disk is $used percent full"; else echo "OK: disk is $used percent full"; fi

A command for the lesson’s lab terminal. It is shown here and not run.

How to read it

Note

The first part finds how full the disk is and keeps the number in the variable used. The if is the condition: if the number is more than 80, print WARNING, otherwise print OK. On a new lab it prints OK.

The lesson goes on

8 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.