Part of Datorkirurgen AB – IT experts across Sweden

Bash scripts that automate backups, reports and operation on Linux. We write, review and troubleshoot scripts for businesses in Stockholm and across Sweden.

Why scripts pay off

Every time an engineer does the same thing by hand, there is a risk of error. A command ends up in the wrong order, a file is forgotten or a check is skipped. With well-written Bash scripts, the computer does the work in exactly the same way every time. This saves time, but above all it gives predictability. Therefore, almost all our operations assignments start with us looking at what can be automated.

Typical examples are database backups, cleaning up old logs and checking certificates. Other clients want a daily report on disk space and updates. A script can also move files between systems or import data from a supplier. In short, Bash suits everything that involves files, commands and scheduled tasks. For more advanced logic, however, we choose Python instead.

Bash scripts that take a database backup and log the result
Bash scripts that take a database backup and log the result. This is how Linux Service, part of Datorkirurgen AB, works for businesses in Stockholm and remotely.

How we write robust scripts

A script that works on the test server does not necessarily work in production. Therefore, we follow a fixed template. Every script starts with set -euo pipefail, so that it stops at the first error instead of carrying on blindly. In addition, we put quotation marks around all variables, since file names with spaces otherwise cause problems. We also lock the script with flock, so that two runs do not collide.

We log everything the script does to the system log. As a result, when something goes wrong, we can see exactly when and why. We never put sensitive details such as passwords in the script itself. Instead, they sit in a separate file with strict permissions, or in a key manager. Finally, we run every script through ShellCheck, which finds common mistakes before they reach the server.

Checklist for the quality of scripts that run in production
Checklist for the quality of scripts that run in production. Do you need help with Linux? Linux Service helps you in Östermalm and Kungsholmen, on site or remotely.

Scheduling with cron and systemd

When the script is ready, it must run at the right time. The classic tool is cron, which exists on all Linux systems. We put our jobs in separate files under /etc/cron.d, so that they are traceable. For more demanding jobs, however, we use systemd timers. They give better logging, and in addition they can run a missed job when the server starts again.

For example, we have a clean-up script on our own web servers that runs on the first and the fifteenth of every month. It removes old logs, temporary files and cache. Before we introduced it, the disk on one server filled up, and then the websites stopped responding. Today that problem is gone. More about it in the article on disk space, journald and swap.

Bash scripts scheduled with a systemd timer instead of cron
Bash scripts scheduled with a systemd timer instead of cron. An example from our work for clients in Solna and Sundbyberg.

Troubleshooting existing scripts

Many clients have scripts that someone wrote a long time ago. They worked once, but after an update they stopped working. Often this is because a command has changed its behaviour or a path has moved. We read the script line by line and run it in debug mode with bash -x. In this way, we see exactly where it goes wrong.

After that, we fix the fault and modernise the script where needed. We add error handling, logging and comments. As a result, the script becomes both more stable and easier to maintain for the next engineer. Sometimes it turns out that several scripts do almost the same thing. In that case, we merge them into one, which simplifies operation further.

Workflow when we troubleshoot an existing script on a Linux server
Workflow when we troubleshoot existing Bash scripts. Linux Service, part of Datorkirurgen AB, helps businesses in Stockholm with servers, scripts and databases.

Security and permissions

A script that runs as root can do great damage if it contains an error. Therefore, we let scripts run with the lowest possible permissions. If a script only needs to read a directory, it gets its own account with exactly that permission. In addition, we check that no other user can change the script file. Otherwise, an attacker could insert their own commands that run with high permissions.

We are also careful with data that comes from outside. We check file names, form data and responses from other systems before the script uses them, because that is the most common path for attacks on scripts. More about file permissions in Linux in our separate article.

Contact Linux Service about Bash scripts

Linux Service is part of Datorkirurgen AB and has written scripts for Linux for more than 30 years. We write new scripts, review existing ones and troubleshoot those that have stopped working. The official manual is the GNU Bash Reference Manual, but we are happy to take care of the work.

Therefore, contact us and describe which task you want to automate. We propose a solution and give a time estimate. We work remotely across Sweden. Moreover, you always receive the scripts with comments and a short description.

Frequently asked questions about Bash scripts

Questions about Bash scripts

Bash scripts: scheduling and troubleshooting

More about security and assignments

Website developed by Datorkirurgen AB · Privacy policy