
What is a Linux Service? It is a program that runs in the background without user interaction. It usually stays running even after a computer reboot. An example is daily backing up a database from a production server. You run a task in the background that dumps the whole database and saves it to a computer.
For example, I will empty the trash on an Ubuntu desktop every minute.
Create a bash script to empty the trash, name it empty-trash.sh, save it in /opt.
#!/bin/bash
rm -rf /home/<username>/.local/share/Trash/*Make it executable.
sudo chmod +x /opt/empty-trash.shCreate the service file empty-trash.service in /etc/systemd/system as root.
sudo touch /etc/systemd/system/empty-trash.serviceNext is to copy and paste this to the empty-trash.service file.
[Unit]
Description=Empty trash every minute
[Service]
ExecStart=bash /opt/empty-trash.sh
User=<username>
[Install]
WantedBy=multi-user.target- The
[Unit]section holds the description of the service file. - The
[Service]section tells how the service should runExecStartdirective is where we specify the command that we want to run which is thebash /opt/empty-trash.sh.- The
Userdirective specifies the account under which the script will execute.
- The
[Install]section defines how systemd should manage the service during system startup or when it is enabled.WantedBywith a value ofmulti-user.targetmeans it will be linked to the system’s boot process.
Create a timer file empty-trash.timer in /etc/systemd/system as root.
sudo touch /etc/systemd/system/empty-trash.timerNext is to copy and paste this to the empty-trash.timer file.
[Unit]
Description=Run empty-trash service every minute
[Timer]
OnUnitActiveSec=1min
Persistent=true
[Install]
WantedBy=timers.target- The
[Unit]section holds the description of the timer file. - The
[Timer]section holds the configuration of the scheduling of the timerOnUnitActiveSecdirective means that the timer will trigger on the scheduled timePersistent=truemeans the timer will compensate for the missed schedule if the system is powered off.
- The
[Install]section specifies how the timer will connect to the systemWantedBy=timers.targetmeans the timer will be added totimers.target. timers.target organizes and starts all timers.
To modify the time, change the value of OnUnitActiveSec.
Run the command below to let the system know of a new service:
sudo systemctl daemon-reloadRun the commands below to start the services:
sudo systemctl start empty-trash.service
sudo systemctl start empty-trash.timerRun the commands below if you want it to enable the service at startup:
sudo systemctl enable empty-trash.service
sudo systemctl enable empty-trash.timerRun the commands below if you want to check the status:
sudo systemctl status empty-trash.service
sudo systemctl status empty-trash.timer
Run the commands below if you want to stop the services:
sudo systemctl stop empty-trash.service
sudo systemctl stop empty-trash.timerNow, you can check the trash from time to time to see if it empties.
Github Gist: https://gist.github.com/rinavillaruz/eb4b5230d5bb0fbded2e916ad0ca189c
