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
| Word | What it means |
|---|---|
| Script | a small program that does a list of tasks for you |
| Automation | getting a computer to do a repeated task |
| Administration | looking after a system: users, updates, disks and backups |
| Shell | a program where you type commands, such as bash |
| Loop | code that repeats the same steps |
| Variable | a name that holds a value |
| Log | a file where a program writes what happened |
| Cron | the Linux tool that runs a command at set times |
| Condition | a 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 questionsYour 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
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?
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"; fiA 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.