Running Things Every Now and Again

From pronounmail wiki
Jump to navigation Jump to search

When building something on pronounmail.com, you might run into a situation where you want to run a script or what have you repeatedly at a certain time or after a certain interval. In Unixland, this is called a cron Job, that is, a job that the cron(8) daemon will run when the time comes. On pronounmail, we don't actually use cron. We use systemd timers. Here's how you set those up.

Common Courtesy

  • Please do not abuse this capability. If you intentionally run a very intensive script every five seconds ad infinitum, we will disable it without asking and tell you very kindly to stop doing that.
  • Furthermore, your script is probably going to be taking up more resources than you think it is. Please think very hard before scheduling timers that run more than once every half an hour or thereabouts.
  • nice is a fun command that you can use to lower the priority of your script. Use it for intensive stuff that you want to run without slowing everyone else down. For example, you could do nice -n 19 <and then the rest of your command> to run your command with the lowest priority possible.

Doing the Timer

Systemd timers are there to schedule a service to run at a specified time. For each something.service file, you can make an accompanying something.timer file.

Here's the basic procedure. I've put the stuff you want to change in bold, you can't miss it.

  • Create a file at ~/.config/systemd/user/<whatever>.service and put some stuff in it. Something like this:
[Unit]
Description=My cool script

[Service]
ExecStart=/home/<your username>/wherever/your/script/is.sh
  • Now you can create a ~/.config/systemd/user/<whatever>.timer file and put some more stuff in that.
[Unit]
Description="run my service every now and again" # a little description to identify this timer

[Timer]
# this is the bit that actually decides when your timer runs. 
# see below for the wacky syntax. this one runs at 10am every weekday
OnCalendar=Mon..Fri *-*-* 10:00:*

Unit=<whatever>.service

# please don't remove this! this prevents 1 trillion timers running at the same time by
# adding a random delay—up to one hour—before the timer starts.
RandomizedDelaySec=1h

[Install]
WantedBy=multi-user.target # this'll enable the timer automatically when the system starts up
  • Now, run this to make it do the thing.
systemctl --user enable --now <whatever>.timer
  • You can check on how it's doing like so:
systemctl --user status <whatever>.timer # for the timer
systemctl --user status <whatever>.service # for the actual task being run

Making it run at different times

More Nonsense