Blog 27th September 2026 – Linux Timed Service Creation One of my other posts tells the story of dealing with an eratic motherboard quite partial to randomly powering my file serving Linux system off, without warning. In the early days of exploring that fault, I didn't know if I had a hardware problem or something else and I also didn't know when it was happening. So I created a couple of services capable of sending emails to help narrow down when the problem was occuring and any symptoms that might shed light on why. In Linux you can create and manage timed jobs using cron - typically to run a BASH script at a particular time and/or date. For example I use cron to run a series of scripts to backup key areas of data using RSync (to an external drive). But you can run into a problem with cron if you need to run something requiring elevated rights (where you would prefix the command with sudo). The Linux file server I'd built is mainly used in the afternoon and evenings but can end up sitting doing nothing from about 2am onwards. Once all the backups are complete - I thought it would be rather elegant to simply sleep the machine (ie: to suspend it) until we're ready to use it and after figuring out the command to suspend was systemctl suspend... I experimented in terminal to assess how well it worked but quickly realised that it had to be run with elevated rights using the sudo prefix which meant firing it under the control of cron was going to be a problem. It turns out you actually can do this with cron - using sudo but configured so that it read a plain text admin password from the standard input...

...an obvious non starter.
A service would be a far better solution and as it happens, Linux makes creating a service reasonably easy. By way of an example, lets assume a few things:-
  • We want to create a linux service which at 2am suspends the system
  • Our name is Fred Smith and our Linux username is fsmith
  • Fred has a folder located in /home/fsmith/Scripts which he uses to hold assorted BASH scripts
Start by creating two files with a filename Suspend2am. The first will be called Suspend2am.service which will describe what the service does and the second will be called Suspend2am.timer to control when the service runs.
Open terminal and start by navigating to the folder /etc/systemd/system
cd /etc/systemd/system

Create two new empty files using touch, both with filename Suspend2am, one with the extension .service and the other .timer
sudo touch Suspend2am.service
sudo touch Suspend2am.timer

Because these two files have been created with elevated rights (using sudo), root will have both ownership and group access rights. Given root will be running these, it must have one or the other (or both) but you'll spot a problem with this arrangement as soon as you try and edit either file and find both are read only. There are a number of ways of tackling this - but I chose to ensure they were easily editable in the future by making sure root had group access, but our login account (in this example fsmith) had ownership access. We can do this using the change ownership command chown as follows:-
sudo chown fsmith:root Suspend2am.*

With our ownership set (but where root still has group access), we can now freely edit these two files using the default mint editor called xed. Use the following command line in terminal to open both files in two different editor tabs:-
xed Suspend2am.*
For the Suspend2am.service file enter the following (carefully observe the capitalisation):-
fsmith Linux Service body
Here we provide a description for this service and define the operation to be performed as a one shot (executes just once whenever the service is invoked). We also tell the service to execute a BASH script at the start of the services execution called /home/fsmith/Scripts/SuspendDaily2am.sh.

Note that locating scripts in a subfolder of your home isn't a great idea as the script really would be better located in some central location for the OS (which isn't dependent on your login). But you can figure that out to suit your system !!!
For the Suspend2am.timer file enter the following (carefully observe the capitalisation):-
fsmith Linux Timer body
In the .timer file we provide a description and define the timer - which matches any/all years, months and days but sets the time to be 2am - whenever this time is reached, regardless of the date, the service will fire. The final section defines what part of the linux system needs to be aware of this timed event. When we enable this timer file, the system controller will look at the install section and create a symbolic link in its timers.target.wants folder that forces the timer to start running as soon as the system boots. Thereafter it'll sit waiting for the time OnCalendar trigger condition to occur - before looking at the .service file with the same filename to figure out what to actually do. All we need to do now is get the system to enable the timer file using:-
systemctl enable --now Suspend2am.timer

If the operation is successful - the system will display the new symbolic link created:-
Created symlink /etc/systemd/system/timers.target.wants/Suspend2am.timer → /etc/systemd/system/Suspend2am.timer
Note you don't need to reboot for the service to start working. You can check to see if the timer is enabled and/or is active using the following command lines:-
systemctl is-enabled Suspend2am.timer
...and/or
systemctl is-active Suspend2am.timer

You can also view logging information raised by the activities of the timer (and the linked service) using the following. You'll find that the timer is enabled and active - but the linked service will show as loaded but inactive... because the timer has yet to fire it:-
systemctl status Suspend2am
In my case, I had separately configured the Linux mail service to work and so when I created the executable BASH script (in this example called SuspendDaily2am.sh) I simply sent an email whenever the script was run. In this way as soon as the service ran and the script executed, I'd know it. All well and good, but you may not have the mail service configured on your system (it isn't overly difficult, but it is a little fiddly) so your script file could confirm it is alive by writing an event to the system log using:-
echo "SuspendDaily2am.sh has just been called now $(date)" | systemd-cat
...and this logging will be available via the command line systemctl status Suspend2am
Linux services - useful information Assume for a moment, you've set a time for the service to run, but for some reason the script doesn't work. You then check the logging using systemctl status Suspend2am and you find the script failed and when you check the .service file you find a typo in the script path. Wouldn't it be nice to correct the error(s) and then set some new timer time 5 mins in the future and then somehow refresh the service so that it works with these new settings. To do this, edit the two Suspend2am files (as required) and use the following three lines to refresh and restart the service:-
sudo systemctl daemon-reload
sudo systemctl reset-failed
sudo systemctl restart Suspend2am.timer

...then, as the time is reached, check the logging (or in my case look out for an email):-
systemctl status Suspend2am
Linux services - deletion The one fiddly area of a service is removal. If I was removing Suspend2am I would do the following:-
  • sudo systemctl stop Suspend2am.timer
  • sudo systemctl disable Suspend2am.timer
  • With great care, manually check to delete any instance of Suspend2am.* from /etc/systemd/system and any subfolder
  • With great care, manually check to delete any instance of Suspend2am.* from /usr/lib/systemd/system and any subfolder
  • sudo systemctl daemon-reload
  • sudo systemctl reset-failed
...and you shouldn't need to reboot.
Linux services - listing all You can view all the services managed by Linux using:-
systemctl list-unit-files

The resulting long list shows the services at the start and the timed elements much further down. In our example you'll find the Suspend2am.service will be shown as static (and not active) because the timer element of the service hasn't yet fired it. Scroll further down and you'll find Suspend2am.timer which will be enabled and active - ready to run.
Wrap up... Linux services are relatively easy to configure and in fact provide considerable options for interactive debugging without requiring a reboot every time a change is made. Predictable, easy to use, easy to debug and reliable when in normal service and in a way that properly overcomes cron permission limitations. All pretty sweet... Comment | Back to Quick Links