[{"data":1,"prerenderedAt":248},["ShallowReactive",2],{"i-lucide:sun-moon":3,"i-lucide:menu":8,"post-\u002Fblog\u002F2024-05-15-using-systemd-timers":10,"surround-\u002Fblog\u002F2024-05-15-using-systemd-timers":231,"i-lucide:arrow-left":242,"i-lucide:sparkles":244,"i-lucide:user":246},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 2v2m2.837 12.385a6 6 0 1 1-7.223-7.222c.624-.147.97.66.715 1.248a4 4 0 0 0 5.26 5.259c.589-.255 1.396.09 1.248.715M16 12a4 4 0 0 0-4-4m7-3l-1.256 1.256M20 12h2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"id":11,"title":12,"authors":13,"body":16,"date":218,"description":12,"extension":219,"meta":220,"navigation":223,"path":224,"seo":225,"stem":226,"tags":227,"toc":6,"__hash__":230},"blog\u002Fblog\u002F2024-05-15-using-systemd-timers.md","Automating Tasks with systemd timers",[14,15],"gpt","pfarmer",{"type":17,"value":18,"toc":205},"minimark",[19,22,37,42,45,62,66,71,77,80,85,88,98,102,112,115,120,122,128,139,143,146,151,156,160,163,168,171,175,178,183,186,190,202],[20,21,12],"p",{},[20,23,24,25,29,30,33,34,36],{},"Automating repetitive tasks is a key part of system administration, and traditionally, ",[26,27,28],"code",{},"cron"," has been the go-to tool for this purpose. However, ",[26,31,32],{},"systemd"," timers offer a more powerful and flexible alternative. In this post, we'll walk through setting up a ",[26,35,32],{}," timer to run a script hourly on Debian 12.",[38,39,41],"h2",{"id":40},"prerequisites","Prerequisites",[20,43,44],{},"Make sure you have the following:",[46,47,48,56],"ul",{},[49,50,51,52,55],"li",{},"A script you want to run periodically. For this example, we'll use ",[26,53,54],{},"\u002Fhome\u002Fadmin\u002Fbin\u002Fbackup.sh",".",[49,57,58,59,61],{},"Root privileges to create and manage ",[26,60,32],{}," service and timer files.",[38,63,65],{"id":64},"step-by-step-guide","Step-by-Step Guide",[67,68,70],"h3",{"id":69},"_1-create-the-service-file","1. Create the Service File",[20,72,73,74,76],{},"First, we need to create a ",[26,75,32],{}," service file that specifies the task we want to run. This file will define the script to execute, the user to run it as, and the working directory.",[20,78,79],{},"Run the following command to create the service file:",[20,81,82],{},[26,83,84],{},"sudo nano \u002Fetc\u002Fsystemd\u002Fsystem\u002Fbackup.service",[20,86,87],{},"Add the following content to the file:",[89,90,95],"pre",{"className":91,"code":93,"language":94},[92],"language-text","[Unit]\nDescription=Run backup script hourly\n\n[Service]\nUser=admin\nWorkingDirectory=\u002Fhome\u002Fadmin\nExecStart=\u002Fhome\u002Fadmin\u002Fbin\u002Fbackup.sh\n","text",[26,96,93],{"__ignoreMap":97},"",[67,99,101],{"id":100},"_2-create-the-timer-file","2. Create the Timer File",[20,103,104,105,107,108,111],{},"Next, create a timer file that tells ",[26,106,32],{}," when to run the service. For an hourly schedule, we can use the ",[26,109,110],{},"OnCalendar"," directive.",[20,113,114],{},"Run the following command to create the timer file:",[20,116,117],{},[26,118,119],{},"sudo nano \u002Fetc\u002Fsystemd\u002Fsystem\u002Fbackup.timer",[20,121,87],{},[89,123,126],{"className":124,"code":125,"language":94},[92],"[Unit]\nDescription=Run backup.service hourly\n\n[Timer]\nOnCalendar=hourly\nPersistent=true\n\n[Install]\nWantedBy=timers.target\n",[26,127,125],{"__ignoreMap":97},[20,129,130,131,134,135,138],{},"The ",[26,132,133],{},"OnCalendar=hourly"," directive schedules the job to run every hour. The ",[26,136,137],{},"Persistent=true"," directive ensures the job will run as soon as possible if the system was off at the scheduled time.",[67,140,142],{"id":141},"_3-enable-and-start-the-timer","3. Enable and Start the Timer",[20,144,145],{},"Enable and start the timer to ensure it runs automatically:",[20,147,148],{},[26,149,150],{},"sudo systemctl enable backup.timer",[20,152,153],{},[26,154,155],{},"sudo systemctl start backup.timer",[67,157,159],{"id":158},"_4-verify-the-timer","4. Verify the Timer",[20,161,162],{},"Check the status of the timer to ensure it's running correctly:",[20,164,165],{},[26,166,167],{},"sudo systemctl status backup.timer",[20,169,170],{},"You should see output indicating that the timer is active and when it is next scheduled to run.",[67,172,174],{"id":173},"_5-list-all-timers","5. List All Timers",[20,176,177],{},"To view all active timers, use the following command:",[20,179,180],{},[26,181,182],{},"systemctl list-timers",[20,184,185],{},"This will list all active timers, their next run time, and their last run time.",[38,187,189],{"id":188},"conclusion","Conclusion",[20,191,192,193,195,196,198,199,201],{},"Using ",[26,194,32],{}," timers on Debian 12 is a robust and flexible way to schedule periodic tasks. Unlike ",[26,197,28],{},", ",[26,200,32],{}," timers offer more features and finer control over job execution, including the ability to specify the user and working directory. By following the steps above, you can easily set up an hourly job to automate your tasks.",[20,203,204],{},"Happy automating!",{"title":97,"searchDepth":206,"depth":206,"links":207},3,[208,210,217],{"id":40,"depth":209,"text":41},2,{"id":64,"depth":209,"text":65,"children":211},[212,213,214,215,216],{"id":69,"depth":206,"text":70},{"id":100,"depth":206,"text":101},{"id":141,"depth":206,"text":142},{"id":158,"depth":206,"text":159},{"id":173,"depth":206,"text":174},{"id":188,"depth":209,"text":189},"2024-05-15","md",{"type":221,"layout":222},"post","blog_page",true,"\u002Fblog\u002F2024-05-15-using-systemd-timers",{"title":12,"description":12},"blog\u002F2024-05-15-using-systemd-timers",[228,229],"devops","mongodb","7uSiEEWBN_DxvlmbW0OzhWuwlNthU9nqFHv51n1Fmto",[232,237],{"title":233,"path":234,"stem":235,"date":236,"children":-1},"Setting Up ed25519 SSH Keys and ssh-agent in WSL and Linux","\u002Fblog\u002F2024-05-10-using-ssh-agent","blog\u002F2024-05-10-using-ssh-agent","2024-05-10",{"title":238,"path":239,"stem":240,"date":241,"children":-1},"Using Git Tags: A Practical Guide","\u002Fblog\u002F2025-11-26-using-git-tags","blog\u002F2025-11-26-using-git-tags","2025-01-15",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":243},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":245},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594zM20 2v4m2-2h-4\"\u002F>\u003Ccircle cx=\"4\" cy=\"20\" r=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":247},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"\u002F>\u003Ccircle cx=\"12\" cy=\"7\" r=\"4\"\u002F>\u003C\u002Fg>",1783616164671]