Managing Services, Scripts, and Timers with systemd on Linux

systemd is the standard system and service manager for most modern Linux distributions. It controls how services start, stop, and interact. Learning how to use systemd to manage services like Apache, custom scripts, and scheduled jobs (timers) can greatly improve the maintainability and reliability of your servers.

Understanding systemd Units

A unit is the basic object in systemd. The most common types are:

  • service: For running and managing system services (like Apache).
  • timer: For scheduling tasks like cron jobs.
  • target: For grouping units and controlling boot order.

Managing Existing Services (e.g., Apache)

Apache is typically managed as a systemd service. Common commands:

# Start Apache service
sudo systemctl start apache2  # On Debian/Ubuntu
sudo systemctl start httpd    # On RHEL/CentOS/Fedora

# Enable service to start at boot
sudo systemctl enable apache2

# Check the service status
sudo systemctl status apache2

Replace apache2 with httpd on Red Hat-based systems.

Creating a Custom systemd Service

To run a custom script as a service, create a new unit file:

  1. Create your script, e.g., /usr/local/bin/myscript.sh.
  2. Make sure it’s executable: chmod +x /usr/local/bin/myscript.sh
  3. Create the unit file /etc/systemd/system/myscript.service:
[Unit]
Description=My Custom Script Service

[Service]
ExecStart=/usr/local/bin/myscript.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. Reload systemd and start your service:
sudo systemctl daemon-reload
sudo systemctl start myscript
sudo systemctl enable myscript

Using systemd Timers for Scheduled Jobs

systemd timers are a powerful alternative to cron.

  1. Create a service unit, e.g., /etc/systemd/system/backup.service:
[Unit]
Description=Run Backup Script

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
  1. Create a timer unit, e.g., /etc/systemd/system/backup.timer:
[Unit]
Description=Run backup every day at midnight

[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=true

[Install]
WantedBy=timers.target
  1. Enable and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer

Check timer status with:

systemctl list-timers

Conclusion

Using systemd to manage services, scripts, and scheduled tasks gives you more control and better integration with your Linux system compared to traditional tools. Embrace it to streamline your server management workflow.

Comments

One response to “Managing Services, Scripts, and Timers with systemd on Linux”

  1. Drew Avatar
    Drew

    Comment from Drew:

    Great overview! As someone who spends a lot of time deploying and maintaining Drupal sites on Linux servers, I can’t stress enough how useful systemd is for both reliability and automation. I especially appreciate how you covered custom service creation and timers—using systemd timers instead of cron has really streamlined my backup and cache-clearing routines for Drupal.

    One tip for Drupal folks: if you’re running Drush commands (like cache-rebuild or cron runs) via systemd, make sure to set the correct user in your [Service] block so permissions line up with your site’s files (e.g., add User=www-data for Debian/Ubuntu). This helps avoid permission headaches down the road.

    Thanks for the clear explanations and practical examples—systemd is a game changer for server management!

    — Drew

Leave a Reply

Your email address will not be published. Required fields are marked *