0
0
Linux CLIscripting~30 mins

systemd timers in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Automate a Task Using systemd Timers
📖 Scenario: You want to automate a simple task on your Linux system, like creating a file with the current date every minute. Instead of using cron, you will use systemd timers, which are modern and flexible.
🎯 Goal: Learn how to create a .service file to run a script and a .timer file to schedule it every minute using systemd timers.
📋 What You'll Learn
Create a systemd service file named datefile.service that runs a command to write the current date to /tmp/datefile.txt
Create a systemd timer file named datefile.timer that triggers the service every minute
Enable and start the timer using systemctl commands
Check the output file /tmp/datefile.txt to confirm the timer works
💡 Why This Matters
🌍 Real World
System administrators use systemd timers to automate tasks like backups, log rotation, or maintenance scripts without relying on cron.
💼 Career
Knowing systemd timers is important for Linux system administrators and DevOps engineers to schedule and manage automated tasks reliably.
Progress0 / 4 steps
1
Create the systemd service file
Create a file named datefile.service with the following content exactly:
[Unit]
Description=Write current date to file

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'date > /tmp/datefile.txt'
Linux CLI
Need a hint?

Use a text editor like nano or vim to create the file with the exact content.

2
Create the systemd timer file
Create a file named datefile.timer with the following content exactly:
[Unit]
Description=Run datefile.service every minute

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

[Install]
WantedBy=timers.target
Linux CLI
Need a hint?

The timer triggers the service every minute at second 0. Use OnCalendar=*-*-* *:*:00.

3
Enable and start the timer
Run these two commands exactly to enable and start the timer:
sudo systemctl enable --now datefile.timer
sudo systemctl start datefile.timer
Linux CLI
Need a hint?

Use systemctl commands with sudo to enable and start the timer.

4
Check the output file
Run cat /tmp/datefile.txt to display the content of the file created by the timer.
Linux CLI
Need a hint?

The output should show the current date and time in a simple format.