From 485aedb0ac2279a47a7f7b7008439e9463220818 Mon Sep 17 00:00:00 2001 From: Maksym Buz Date: Tue, 26 Aug 2025 21:42:05 +0200 Subject: [PATCH] ADD: Added two mds to compare AI generated manuals --- timer-as-service-gemini.md | 93 ++++++++++++++++++++++++++++ timer-as-service.md | 124 +++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 timer-as-service-gemini.md create mode 100644 timer-as-service.md diff --git a/timer-as-service-gemini.md b/timer-as-service-gemini.md new file mode 100644 index 0000000..6678124 --- /dev/null +++ b/timer-as-service-gemini.md @@ -0,0 +1,93 @@ +## How to Set Up a systemd Timer + +This guide provides a quick walkthrough for creating, enabling, and managing systemd timer units to schedule tasks. + +### 1. Create a Service Unit + +First, create a `.service` file that defines the job you want to run. This file will be placed in `/etc/systemd/system/`. + +**Example: `/etc/systemd/system/mytask.service`** +```ini +[Unit] +Description=A description for your task + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/mytask.sh +``` +- `Type=oneshot`: Indicates that this service is a short-lived task. +- `ExecStart`: The command or script to be executed. + +### 2. Create a Timer Unit + +Next, create a corresponding `.timer` file. This file defines the schedule for when the service should run. + +**Example: `/etc/systemd/system/mytask.timer`** +```ini +[Unit] +Description=Run mytask.service every day at 3 AM + +[Timer] +OnCalendar=*-*-* 03:00:00 +Persistent=true + +[Install] +WantedBy=timers.target +``` +- `OnCalendar`: Defines a calendar-based schedule. The format is `DayOfWeek Year-Month-Day Hour:Minute:Second`. An asterisk (`*`) matches any value. +- `Persistent=true`: If the system was down when the timer should have run, the job will execute as soon as the system boots up. + +### 3. Enable and Start the Timer + +After creating the files, reload the systemd daemon and start the timer: +```bash +# Reload systemd to recognize the new files +sudo systemctl daemon-reload + +# Enable the timer to start on boot +sudo systemctl enable mytask.timer + +# Start the timer immediately +sudo systemctl start mytask.timer +``` + +You can do this in one command with `--now`: +```bash +sudo systemctl enable --now mytask.timer +``` + +### 4. List Existing Timers + +To see all active timers and when they are scheduled to run next, use the `list-timers` command: + +```bash +systemctl list-timers +``` + +**Output Example:** +``` +NEXT LEFT LAST PASSED UNIT ACTIVATES +Wed 2025-08-27 03:00:00 CEST 12h left Tue 2025-08-26 03:00:00 CEST 9h ago mytask.timer mytask.service +``` +- `NEXT`: The next time the timer will trigger. +- `LEFT`: Time remaining until the next trigger. +- `LAST`: The last time the timer was triggered. + +To include inactive timers, add the `--all` flag: +```bash +systemctl list-timers --all +``` + +### 5. Check Status and Logs + +To check the status of a specific timer or service: +```bash +systemctl status mytask.timer +systemctl status mytask.service +``` + +To view the logs generated by your service: +```bash +journalctl -u mytask.service +``` +To follow the logs in real-time, add the `-f` flag. diff --git a/timer-as-service.md b/timer-as-service.md new file mode 100644 index 0000000..a930229 --- /dev/null +++ b/timer-as-service.md @@ -0,0 +1,124 @@ +## Timer as a service — quick manual + +This short guide shows how to set up a systemd timer that runs a job, how to enable/start it, and how to list and inspect existing timers. + +### Checklist +- Create a `.service` that performs the work +- Create a corresponding `.timer` with the schedule +- Reload systemd, enable and start the timer +- List timers and inspect logs for failures + +### Units: example +System-wide units live under `/etc/systemd/system`. For per-user timers use `~/.config/systemd/user/` and `systemctl --user`. + +Create `/etc/systemd/system/my-job.service`: +```ini +[Unit] +Description=My scheduled job + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/my-job.sh +``` + +Create `/etc/systemd/system/my-job.timer`: +```ini +[Unit] +Description=Run my-job every day at 02:30 + +[Timer] +OnCalendar=*-*-* 02:30:00 +Persistent=true + +[Install] +WantedBy=timers.target +``` + +Notes: +- `Type=oneshot` is appropriate for short scripts that exit. +- `Persistent=true` makes missed runs (while the machine was down) run on next boot. + +### Install, enable and start +After creating or editing unit files run: +```bash +sudo systemctl daemon-reload +sudo systemctl enable --now my-job.timer +``` + +For a user timer (no sudo): +```bash +systemctl --user daemon-reload +systemctl --user enable --now my-job.timer +``` + +### List existing timers +Show all timers (system): +```bash +systemctl list-timers --all +``` +This shows UNIT, ACTIVATES, NEXT, LEFT, LAST and PASSED columns. `NEXT` is the next trigger time. + +User timers: +```bash +systemctl --user list-timers --all +``` + +Filter for a specific timer: +```bash +systemctl list-timers --all | grep my-job +systemctl status my-job.timer +systemctl status my-job.service +``` + +### Logs and debugging +View recent service logs: +```bash +journalctl -u my-job.service -b +``` +View timer logs: +```bash +journalctl -u my-job.timer -b +``` + +If the job never ran, check: +- `systemctl status my-job.timer` for errors +- `ExecStart` path and executable permissions +- run the `ExecStart` command manually to verify behavior +- `OnCalendar` syntax (see `man systemd.time`) + +Common fixes: +- `sudo systemctl daemon-reload` after edits +- ensure `my-job.service` is reachable and executable by the user that runs it +- for user timers, make sure the user session is running or enable linger if run outside a login session + +### Helpful examples +Run the job 10 seconds after boot and every 5 minutes: +```ini +[Timer] +OnBootSec=10s +OnUnitActiveSec=5min +Persistent=true +``` + +Use environment variables inside the service: +```ini +[Service] +Environment=ENVIRONMENT=production +EnvironmentFile=/etc/default/my-job +``` + +### Remove or disable a timer +```bash +sudo systemctl disable --now my-job.timer +sudo rm /etc/systemd/system/my-job.timer /etc/systemd/system/my-job.service +sudo systemctl daemon-reload +``` + +### Quick troubleshooting checklist +- Confirm unit files are syntactically correct +- Check permissions and shebang on the script +- `journalctl -u ` for errors +- For user timers: check `systemctl --user status` and enable linger with `loginctl enable-linger ` if needed + +--- +This file provides a compact reference; tell me if you want a tailored example script and a ready-to-install pair of units added to the repo.