Hello, readers!

Some of you may know that I posted about doing some automated maintenance for mastodon. I've updated the script that I'm running, and personally set it up as a systemd timer as well!

You can use the main maintenance script and use crontab if you prefer, I just used it as an excuse to learn how to make a timer.

First, the current version of the cleanup.bash script:

Please remember that this is configured for my setup, you may need to modify. Don't just copypaste my stuff, blindly copypasting is dangerous!

#!/bin/bash
## First we set up our environment so Rails will play nice
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export RAILS_ENV=production

## This command will remove old media from other instances, leaving
## only three days worth. 
/home/mastodon/live/bin/tootctl media remove --days=3 --background
## Do the same with statuses i've not interacted with from other
## instances
/home/mastodon/live/bin/tootctl statuses remove --days=3
## Refresh account data. Purely optional, and will query the servers
## for all accounts your instance shas seen.
## I do this because I was getting annoyed that I couldn't see when
## friends updated their info!
/home/mastodon/live/bin/live/bin/tootctl accounts  refresh --all

For my instance, I've moved from a crontab entry to creating a systemd unit and timer, as mentioned earlier.

After looking into it, systemd timers are pretty powerful, they have a few options, like being able to run x amount of minutes after a start up or user login (depending if it's a user or system timer), and, it's a separate file that shares the same name (extension excepting) as a service it will trigger.

If that's a little confusing, this should make it easier:

Create a service file. Mark it as a oneshot type service. Call it, say, mastodon-cleanup.service. Now to make a timer for it, we make mastodon-cleanup.timer. You don't need to mention the .service file in the file, it will use the name to know what to trigger!

So, we create the following two files:

Please remember that this is configured for my setup, you may need to modify. Don't just copypaste my stuff, blindly copypasting is dangerous!

mastodon-cleanup.service

[Unit]
Description=Mastodon Cleanup service

[Service]
User=mastodon
Group=mastodon
WorkingDirectory=/home/mastodon
Type=simple
Type=oneshot
ExecStart=/home/mastodon/cleanup.bash

The service file is pretty simple. It calls the cleanup.bash script we made earlier, and sets the running user and group as the mastodon user, and changes the working directory to /home/mastodon, which cron would usually do.

Please remember that this is configured for my setup, you may need to modify. Don't just copypaste my stuff, blindly copypasting is dangerous!

mastodon-cleanup.timer

[Unit]
Description=Mastodon Cleanup timer

[Timer]
OnBootSec=4min
OnUnitActiveSec=2hr

[Install]
WantedBy=timers.target

And here we have the magic, the actual timer. Very simple, more options can be found in a few places. I personally like The arch wiki's explanation for this

I've set up this one to start 4 minutes after the system has booted, allowing time for masto to fire up fully and settle before beginning maintenance tasks. Then, it will wait for two hours before firing off again, and fires off every two hours!

That's all for now, I hope that this helps some of you Mastodon instance admins!

Next Post Previous Post