Kennisbank › Windows Server › Bash and Python automation on the server: Practical guide
windows server gemiddeld 10 July 2026

Bash and Python automation on the server: Practical guide

Learn how to combine Bash and Python with cron for powerful server automation. Practical examples and best practices for developers and sysadmins.

Automation is the backbone of modern server management processes. Whether you are a developer looking to streamline tasks or a system administrator wanting to eliminate repetitive work, Bash and Python form a powerful combination for server automation. In this article, we explore how to combine these tools with schedulers like cron for efficient, reliable automation.

Why Bash and Python for server automation?

For server automation, two main approaches are popular:

  • Bash scripts - Lightweight, ideal for file management, process control, and command-line tasks
  • Python scripts - More powerful for complex logic, API interactions, and data processing

The combination of both offers the best of both worlds. Bash for quick system tasks, Python for more complex operations - together executed via cron for scheduled execution.

Basics of cron: Scheduling your automation

Cron is the standard task scheduler on Unix systems. Important commands:

crontab -e Edit your crontab file
crontab -l Show scheduled tasks

A standard crontab line looks like this:

* /path/to/script.sh

Where the asterisks stand for:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-7, where both 0 and 7 are Sunday)

Practical Bash automation examples

1. Automatically cleaning up log files

Create a script clean_logs.sh:

#!/bin/bash
find /var/log -name "*.log" -type f -mtime +30 -exec rm -f {} \;
echo "$(date): Logs older than 30 days removed" >> /var/log/cleanup.log

Add to cron for monthly execution:

0 0 1 /bin/bash /path/to/clean_logs.sh

2. Creating automatic backups

#!/bin/bash
tar -czf /backups/website_$(date +\%Y\%m\%d).tar.gz /var/www/html
find /backups -name "*.tar.gz" -mtime +14 -exec rm {} \;

Python automation for more complex tasks

Where Bash falls short, Python excels. Example: a monitoring script that checks an API and sends a notification when there are problems.

#!/usr/bin/env python3
import requests
import smtplib

Pixzo

Pixzo - My Blog

Stories about Linux, Docker, and more

Getting Started with Docker

Published on: March 15, 2025

Docker makes it easy to run applications in containers. In this post, I will explain the basics.

First, install Docker on your system. On Ubuntu, you can use the following command:

sudo apt install docker.io

Then, start the Docker service:

sudo systemctl start docker

To test, run the classic hello-world container:

docker run hello-world

If you want to pull an image, for example nginx, you can do:

docker pull nginx

That's it for the basics. In the next post, I will cover more advanced topics.

Setting Up a Simple Web Server

Published on: March 10, 2025

Here I will show you how to set up a simple HTTP server using Python. This is useful for testing.

Run the following command in a directory with your files:

python3 -m http.server 8000

Then open your browser to

http://localhost:8000
to see the files.

Alternatively, you can use nginx for a more robust setup, but that requires more configuration.

Checking API Health

Published on: March 5, 2025

It's important to monitor your services. Here is a simple Python script to check if an API is up:

def check_api():
    try:
        response = requests.get("https://api.je-service.nl/status")
        return response.status_code == 200
    except:
        return False

This function returns True if the API responds with a 200 status code, otherwise False.

← Terug naar kennisbank