{"id":5544,"date":"2026-01-07T08:58:30","date_gmt":"2026-01-07T08:58:30","guid":{"rendered":"https:\/\/commandlinux.com\/?p=5544"},"modified":"2026-01-07T09:00:29","modified_gmt":"2026-01-07T09:00:29","slug":"systemd-timer","status":"publish","type":"post","link":"https:\/\/commandlinux.com\/arch-linux\/systemd-timer\/","title":{"rendered":"How To Use Systemd Timer In Linux"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <style>\n\n.systemd-timer-container        h2 {\n            font-size: 1.75rem;\n            font-weight: 600;\n            margin: 2.5rem 0 1rem 0;\n            color: #1a1a1a;\n        }\n        \n.systemd-timer-container        h3 {\n            font-size: 1.35rem;\n            font-weight: 600;\n            margin: 2rem 0 0.8rem 0;\n            color: #2c3e50;\n        }\n        \n.systemd-timer-container        h4 {\n            font-size: 1.1rem;\n            font-weight: 600;\n            margin: 1.5rem 0 0.7rem 0;\n            color: #34495e;\n        }\n        \n.systemd-timer-container        p {\n            margin-bottom: 1.2rem;\n        }\n        \n        code {\n            background: #f4f4f4;\n            padding: 2px 6px;\n            border-radius: 3px;\n            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;\n            font-size: 0.9em;\n            color: #c7254e;\n        }\n        \n        pre {\n            background: #f8f8f8;\n            border: 1px solid #e1e4e8;\n            border-radius: 5px;\n            padding: 16px;\n            overflow-x: auto;\n            margin: 1.2rem 0;\n            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;\n            font-size: 0.9em;\n            line-height: 1.5;\n        }\n        \n        pre code {\n            background: none;\n            padding: 0;\n            color: #24292e;\n        }\n        \n        .note, .warning, .tip {\n            padding: 16px 20px;\n            border-radius: 5px;\n            margin: 1.5rem 0;\n            border-left: 4px solid;\n        }\n        \n        .note {\n            background: #e7f3ff;\n            border-color: #2196F3;\n        }\n        \n        .warning {\n            background: #fff3e0;\n            border-color: #ff9800;\n        }\n        \n        .tip {\n            background: #e8f5e9;\n            border-color: #4caf50;\n        }\n        \n        .note strong, .warning strong, .tip strong {\n            display: block;\n            margin-bottom: 0.5rem;\n            font-weight: 600;\n        }\n        \n        table {\n            width: 100%;\n            border-collapse: collapse;\n            margin: 1.5rem 0;\n            font-size: 0.95em;\n        }\n        \n        th, td {\n            padding: 12px;\n            text-align: left;\n            border: 1px solid #ddd;\n        }\n        \n        th {\n            background: #f5f5f5;\n            font-weight: 600;\n            color: #2c3e50;\n        }\n        \n        tr:nth-child(even) {\n            background: #fafafa;\n        }\n        \n.systemd-timer-container        ul {\n            margin: 1rem 0 1.5rem 2rem;\n        }\n        \n.systemd-timer-container        li {\n            margin-bottom: 0.6rem;\n        }\n        \n.systemd-timer-container        a {\n            color: #2196F3;\n            text-decoration: none;\n        }\n        \n        a:hover {\n            text-decoration: underline;\n        }\n        \n        .faq-section {\n            margin-top: 3rem;\n        }\n        \n        .faq-item {\n            margin-bottom: 1rem;\n            border: 1px solid #e1e4e8;\n            border-radius: 5px;\n            overflow: hidden;\n        }\n        \n        .faq-item input[type=\"checkbox\"] {\n            display: none;\n        }\n        \n        .faq-item label {\n            display: block;\n            padding: 16px 20px;\n            background: #f8f9fa;\n            cursor: pointer;\n            position: relative;\n            transition: background 0.2s;\n        }\n        \n        .faq-item label:hover {\n            background: #e9ecef;\n        }\n        \n        .faq-item label::after {\n            content: '+';\n            position: absolute;\n            right: 20px;\n            top: 50%;\n            transform: translateY(-50%);\n            font-size: 1.5rem;\n            font-weight: 300;\n            transition: transform 0.3s;\n        }\n        \n        .faq-item input[type=\"checkbox\"]:checked + label::after {\n            transform: translateY(-50%) rotate(45deg);\n        }\n        \n        .faq-item label h3 {\n            margin: 0;\n            font-size: 1.1rem;\n            font-weight: 500;\n            color: #2c3e50;\n        }\n        \n        .faq-answer {\n            max-height: 0;\n            overflow: hidden;\n            transition: max-height 0.3s ease;\n        }\n        \n        .faq-item input[type=\"checkbox\"]:checked ~ .faq-answer {\n            max-height: 500px;\n        }\n        \n        .faq-answer p {\n            padding: 16px 20px;\n            margin: 0;\n            background: #ffffff;\n        }\n    <\/style>\n<\/head>\n<body>\n\n\n<p>A systemd timer schedules tasks on Linux systems. Each timer requires a matching service file that defines the command to execute. Timer units end with <code>.timer<\/code> and service units end with <code>.service<\/code>.<\/p>\n\n<p>Timers integrate with the systemd journal for centralized logging and support dependency management between services.<\/p>\n\n<h2>Types of Systemd Timers<\/h2>\n\n<p>Systemd provides two timer categories.<\/p>\n\n<h3>Monotonic Timers<\/h3>\n\n<p>Monotonic timers activate after a duration relative to a reference point. These timers pause during system suspension.<\/p>\n\n<table>\n<thead>\n<tr>\n<th>Keyword<\/th>\n<th>Trigger Point<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>OnBootSec<\/code><\/td>\n<td>Relative to machine startup<\/td>\n<\/tr>\n<tr>\n<td><code>OnActiveSec<\/code><\/td>\n<td>Relative to timer activation<\/td>\n<\/tr>\n<tr>\n<td><code>OnUnitActiveSec<\/code><\/td>\n<td>Relative to last service completion<\/td>\n<\/tr>\n<tr>\n<td><code>OnUnitInactiveSec<\/code><\/td>\n<td>Relative to last service stop<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n<h3>Realtime Timers<\/h3>\n\n<p>Realtime timers trigger at specific calendar moments. Use the <code>OnCalendar<\/code> directive to define these timers.<\/p>\n\n<table>\n<thead>\n<tr>\n<th>Expression<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>hourly<\/code><\/td>\n<td>Start of each hour<\/td>\n<\/tr>\n<tr>\n<td><code>daily<\/code><\/td>\n<td>Every day at midnight<\/td>\n<\/tr>\n<tr>\n<td><code>weekly<\/code><\/td>\n<td>Every Monday at midnight<\/td>\n<\/tr>\n<tr>\n<td><code>Mon..Fri 22:30<\/code><\/td>\n<td>Weekdays at 10:30 PM<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n<h2>Create a Service File<\/h2>\n\n<p>Create a service file in <code>\/etc\/systemd\/system\/<\/code>. Example service file named <code>backup.service<\/code>:<\/p>\n\n<pre><code>[Unit]\nDescription=Run backup script\n\n[Service]\nType=oneshot\nExecStart=\/usr\/local\/bin\/backup.sh<\/code><\/pre>\n\n<p>The <code>Type=oneshot<\/code> parameter indicates the service completes a task then exits.<\/p>\n\n<div class=\"note\">\n<strong>Note:<\/strong> Service files triggered by timers do not require an <code>[Install]<\/code> section.\n<\/div>\n\n<h2>Create a Timer File<\/h2>\n\n<p>Timer files define when the service runs. Create a timer file with the same base name as the service.<\/p>\n\n<h3>Monotonic Timer Example<\/h3>\n\n<p>Create <code>backup.timer<\/code> to run fifteen minutes after boot and repeat weekly:<\/p>\n\n<pre><code>[Unit]\nDescription=Backup timer\n\n[Timer]\nOnBootSec=15min\nOnUnitActiveSec=1w\n\n[Install]\nWantedBy=timers.target<\/code><\/pre>\n\n<h3>Realtime Timer Example<\/h3>\n\n<p>Create a calendar-based timer that runs every Monday:<\/p>\n\n<pre><code>[Unit]\nDescription=Weekly backup timer\n\n[Timer]\nOnCalendar=weekly\nPersistent=true\n\n[Install]\nWantedBy=timers.target<\/code><\/pre>\n\n<p>The <code>Persistent=true<\/code> option ensures missed executions run after system restart.<\/p>\n\n<h2>Activate the Timer<\/h2>\n\n<p>Place both files in <code>\/etc\/systemd\/system\/<\/code>. Enable and start the timer:<\/p>\n\n<pre><code>$ sudo systemctl daemon-reload\n$ sudo systemctl enable backup.timer\n$ sudo systemctl start backup.timer<\/code><\/pre>\n\n<div class=\"tip\">\n<strong>Tip:<\/strong> Combine enable and start with <code>systemctl enable --now backup.timer<\/code>.\n<\/div>\n\n<h2>View Running Timers<\/h2>\n\n<p>Display all active timers:<\/p>\n\n<pre><code>$ systemctl list-timers<\/code><\/pre>\n\n<p>Include inactive timers:<\/p>\n\n<pre><code>$ systemctl list-timers --all<\/code><\/pre>\n\n<p>Check a specific timer:<\/p>\n\n<pre><code>$ systemctl status backup.timer<\/code><\/pre>\n\n<h2>OnCalendar Syntax<\/h2>\n\n<p>The <code>OnCalendar<\/code> directive accepts flexible time specifications. Validate calendar expressions:<\/p>\n\n<pre><code>$ systemd-analyze calendar \"Mon..Fri 08:00\"<\/code><\/pre>\n\n<p>Complex schedule examples:<\/p>\n\n<pre><code>OnCalendar=Mon,Wed,Fri 10:00\nOnCalendar=*-*-* 06,12,18:00:00\nOnCalendar=Sat *-*-1..7 15:00:00<\/code><\/pre>\n\n<h2>Useful Timer Options<\/h2>\n\n<h3>Persistent<\/h3>\n\n<p>Add <code>Persistent=true<\/code> to run missed executions after system downtime. The timer triggers immediately when the system restarts.<\/p>\n\n<h3>AccuracySec<\/h3>\n\n<p>Control execution precision with <code>AccuracySec<\/code>. Default is one minute. Set <code>AccuracySec=1us<\/code> for microsecond accuracy:<\/p>\n\n<pre><code>[Timer]\nOnCalendar=daily\nAccuracySec=1us<\/code><\/pre>\n\n<div class=\"warning\">\n<strong>Warning:<\/strong> Lower accuracy values increase power consumption. Use precise timing only when necessary.\n<\/div>\n\n<h3>RandomizedDelaySec<\/h3>\n\n<p>Prevent simultaneous timer execution with random delays:<\/p>\n\n<pre><code>[Timer]\nOnCalendar=daily\nRandomizedDelaySec=1h<\/code><\/pre>\n\n<p>The timer executes at a random time within one hour of the scheduled time.<\/p>\n\n<h2>Transient Timers<\/h2>\n\n<p>Create temporary timers without unit files using <code>systemd-run<\/code>. Run a command after thirty seconds:<\/p>\n\n<pre><code># systemd-run --on-active=30 \/bin\/touch \/tmp\/testfile<\/code><\/pre>\n\n<p>Create a calendar-based transient timer:<\/p>\n\n<pre><code># systemd-run --on-calendar=\"Mon 14:00\" \/usr\/bin\/cleanup.sh<\/code><\/pre>\n\n<p>Stop a running transient timer:<\/p>\n\n<pre><code>$ systemctl stop run-<replaceable>unit-id<\/replaceable>.timer<\/code><\/pre>\n\n<h2>Advantages Over Cron<\/h2>\n\n<p>Systemd timers provide several benefits:<\/p>\n\n<ul>\n<li>Jobs log directly to the systemd journal<\/li>\n<li>Services can depend on other systemd units<\/li>\n<li>Resource control through cgroups<\/li>\n<li>Better error handling and failure notifications<\/li>\n<li>Missed jobs run automatically with <code>Persistent=true<\/code><\/li>\n<\/ul>\n\n<p>View logs with <code>journalctl -u service-name<\/code>.<\/p>\n\n<div class=\"faq-section\">\n<h2>FAQs<\/h2>\n\n<div class=\"faq-item\">\n<input type=\"checkbox\" id=\"faq1\">\n<label for=\"faq1\"><h3>How do systemd timers differ from cron jobs?<\/h3><\/label>\n<div class=\"faq-answer\">\n<p>Systemd timers integrate with the journal for centralized logging. They support dependencies between services and provide resource control. Cron requires manual logging configuration and lacks service dependency management.<\/p>\n<\/div>\n<\/div>\n\n<div class=\"faq-item\">\n<input type=\"checkbox\" id=\"faq2\">\n<label for=\"faq2\"><h3>Can a systemd timer trigger multiple services?<\/h3><\/label>\n<div class=\"faq-answer\">\n<p>No. Each timer file triggers one service. Create separate timer units for each service. Use the <code>Unit=<\/code> directive to specify a differently-named service if needed.<\/p>\n<\/div>\n<\/div>\n\n<div class=\"faq-item\">\n<input type=\"checkbox\" id=\"faq3\">\n<label for=\"faq3\"><h3>What happens if the system is off during a systemd timer execution?<\/h3><\/label>\n<div class=\"faq-answer\">\n<p>Set <code>Persistent=true<\/code> in the timer configuration. The timer triggers immediately when the system restarts if it missed one or more scheduled executions during downtime.<\/p>\n<\/div>\n<\/div>\n\n<div class=\"faq-item\">\n<input type=\"checkbox\" id=\"faq4\">\n<label for=\"faq4\"><h3>How do I test a systemd timer without waiting for scheduled execution?<\/h3><\/label>\n<div class=\"faq-answer\">\n<p>Manually trigger the associated service with <code>systemctl start service-name.service<\/code>. This executes the service immediately without affecting the timer schedule. Check logs with <code>journalctl<\/code>.<\/p>\n<\/div>\n<\/div>\n\n<div class=\"faq-item\">\n<input type=\"checkbox\" id=\"faq5\">\n<label for=\"faq5\"><h3>Where should I place systemd timer and service files?<\/h3><\/label>\n<div class=\"faq-answer\">\n<p>Place custom timer and service files in <code>\/etc\/systemd\/system\/<\/code>. System-provided units reside in <code>\/usr\/lib\/systemd\/system\/<\/code>. User-specific timers go in <code>~\/.config\/systemd\/user\/<\/code>.<\/p>\n<\/div>\n<\/div>\n\n<\/div>\n\n<\/body>\n<\/html>\n\n","protected":false},"excerpt":{"rendered":"<p>A systemd timer schedules tasks on Linux systems. Each timer requires a matching service file that defines the command to execute. Timer units end with .timer and service units end with .service. Timers integrate with the systemd journal for centralized logging and support dependency management between services. Types of Systemd Timers Systemd provides two timer<\/p>\n","protected":false},"author":1,"featured_media":5545,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-5544","post","type-post","status-publish","format-standard","has-post-thumbnail","category-arch-linux"],"_links":{"self":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/5544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/comments?post=5544"}],"version-history":[{"count":2,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/5544\/revisions"}],"predecessor-version":[{"id":5547,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/5544\/revisions\/5547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media\/5545"}],"wp:attachment":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media?parent=5544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/categories?post=5544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/tags?post=5544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}