
We call tasks that run on the crontab “cronjobs”
Layout: minute hour dom month dow task [arguments]
*/1 * * * * /home/jacob/notification-checker.py
0 2 * * mon /root/pull-remote-backups.sh
@reboot echo 'Message Text' | mail -s 'Server Rebooted!' email_address@domain.tld
How do I edit the Crontab?
Run this command, that’s all: crontab -e
To change your editor, you can run select-editor, or
just change your $EDITOR environment variable.
If you already have emailing setup on your Linux server (or home computer!), you can simply write, somewhere in your crontab
MAILTO=email_address@domain.tld
What is systemd? systemd is an array of
software tools for many different Linux
operating systems.
Its main goal is to serve as a “system and service manager” across Linux distributions.
The crontab actually runs on top of systemd on many systems!
According to a helpful
man systemd.servicecommand, a systemd service is a “process controlled and supervised by systemd”.
Below is a systemd service I have for a Rust Discord bot
I wrote.
[Unit]
Description=Rust Discord Bot
After=network.target
[Service]
Type=simple
EnvironmentFile=/root/Rust-Discord-Bot-Env
ExecStart=/sbin/rust-discord-bot
[Install]
WantedBy=multi-user.targetTo configure a service to start on boot, we must first add it (as a
.service file) to the systemd system directory (usually
/etc/systemd/system/).
Then, we use the systemctl command to configure the
service (sudo here only necessary our service is a
system service).
sudo systemctl enable service-name.service # Makes the service start on boot
sudo systemctl start service-name.service # Starts the service now (so no reboot)
sudo systemctl status service-name.service # Checks the current status of the service
journalctl -u service-name.service # Shows service logsYou can also have systemd services run on your user accounts
(no root needed)!
This is just as simple as the system services, with three differences to keep in mind:
You place the service file in
~/.config/systemd/user/ instead of
/etc/systemd/system/
You add a --user flag to all systemctl
commands
Your service can’t start on boot anymore, now, since it must start within the context of your user account.
April 2024