Create Mastodon maintenance jobs for bare-metal Mastodon installation from source code.

Remove downloaded media files.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-media-remove.service
[Unit]
Description=Mastodon - media remove service
Wants=mastodon-media-remove.timer

[Service]
Type=oneshot

User=mastodon
Group=mastodon

Environment="RAILS_ENV=production"
Environment="PATH=/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/bin"
WorkingDirectory=/home/mastodon/live
ExecStart=/home/mastodon/live/bin/tootctl media remove

[Install]
WantedBy=multi-user.target
EOF

Execute it everyday at 05:00 in the morning.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-media-remove.timer
[Unit]
Description=Schedule a media remove every week

[Timer]
Persistent=true
OnCalendar=*-*-* 05:00:00
Unit=mastodon-media-remove.service

[Install]
WantedBy=timers.target
EOF

Remove preview cards.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-preview_cards-remove.service
[Unit]
Description=Mastodon - preview cards remove service
Wants=mastodon-preview_cards-remove.timer

[Service]
Type=oneshot

User=mastodon
Group=mastodon

Environment="RAILS_ENV=production"
Environment="PATH=/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/bin"
WorkingDirectory=/home/mastodon/live
ExecStart=/home/mastodon/live/bin/tootctl preview_cards remove

[Install]
WantedBy=multi-user.target
EOF

Execute it everyday at 05:30 in the morning.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-preview_cards-remove.timer
[Unit]
Description=Schedule a preview cards remove every week

[Timer]
Persistent=true
OnCalendar=*-*-* 05:30:00
Unit=mastodon-preview_cards-remove.service

[Install]
WantedBy=timers.target
EOF

Create, upgrade and populate Elasticsearch indices.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-search-deploy.service
[Unit]
Description=Mastodon - update Elasticsearch indices
Wants=mastodon-search-deploy.timer

[Service]
Type=oneshot

User=mastodon
Group=mastodon

Environment="RAILS_ENV=production"
Environment="PATH=/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/bin"
WorkingDirectory=/home/mastodon/live
ExecStart=/home/mastodon/live/bin/tootctl search deploy

[Install]
WantedBy=multi-user.target
EOF

Execute it everyday at 06:00 in the morning, but consider doing it much more often.

$ cat << EOF | sudo tee /etc/systemd/system/mastodon-search-deploy.timer
[Unit]
Description=Schedule a preview cards remove every week

[Timer]
Persistent=true
OnCalendar=*-*-* 06:00:00
Unit=mastodon-search-deploy.service

[Install]
WantedBy=timers.target
EOF

Reload systemd configuration.

$ sudo systemctl daemon-reload

Enable these timers (enable & start).

$ sudo systemctl enable --now mastodon-preview_cards-remove.timer
$ sudo systemctl enable --now mastodon-media-remove.timer
$ sudo systemctl enable --now mastodon-search-deploy.timer

List timers.

$ systemctl list-timers      
NEXT                        LEFT          LAST                        PASSED       UNIT                                ACTIVATES                            
Sat 2022-11-19 05:00:00 UTC 1h 49min left n/a                         n/a          mastodon-media-remove.timer         mastodon-media-remove.service
Sat 2022-11-19 05:30:00 UTC 2h 19min left n/a                         n/a          mastodon-preview_cards-remove.timer mastodon-preview_cards-remove.service
Sat 2022-11-19 06:00:00 UTC 2h 49min left n/a                         n/a          mastodon-search-deploy.timer        mastodon-search-deploy.service
Sat 2022-11-19 06:36:10 UTC 3h 25min left Fri 2022-11-18 06:20:33 UTC 20h ago      apt-daily-upgrade.timer             apt-daily-upgrade.service
Sat 2022-11-19 14:59:27 UTC 11h left      Fri 2022-11-18 14:59:26 UTC 12h ago      systemd-tmpfiles-clean.timer        systemd-tmpfiles-clean.service
Sat 2022-11-19 16:08:56 UTC 12h left      Sat 2022-11-19 02:28:55 UTC 41min ago    motd-news.timer                     motd-news.service
Sat 2022-11-19 16:39:19 UTC 13h left      Sat 2022-11-19 00:55:29 UTC 2h 14min ago certbot.timer                       certbot.service
Sun 2022-11-20 00:00:00 UTC 20h left      Sat 2022-11-19 00:00:20 UTC 3h 9min ago  dpkg-db-backup.timer                dpkg-db-backup.service
Sun 2022-11-20 03:10:08 UTC 23h left      Sun 2022-11-13 14:34:53 UTC 5 days ago   e2scrub_all.timer                   e2scrub_all.service
Sun 2022-11-20 03:36:53 UTC 24h left      Sat 2022-11-19 02:40:53 UTC 29min ago    man-db.timer                        man-db.service
Sun 2022-11-20 04:20:38 UTC 1 day 1h left Sat 2022-11-19 02:40:57 UTC 29min ago    exim4-base.timer                    exim4-base.service
Sun 2022-11-20 04:46:41 UTC 1 day 1h left Sat 2022-11-19 02:57:21 UTC 12min ago    plocate-updatedb.timer              plocate-updatedb.service
Sun 2022-11-20 06:15:57 UTC 1 day 3h left Sat 2022-11-19 02:40:47 UTC 29min ago    logrotate.timer                     logrotate.service

13 timers listed.
Pass --all to see loaded but inactive timers, too.

List timers after several hours.

$ systemctl list-timers      
$ systemctl list-timers      
NEXT                        LEFT          LAST                        PASSED       UNIT                                ACTIVATES                            
Sat 2022-11-19 14:59:27 UTC 3h 31min left Fri 2022-11-18 14:59:26 UTC 20h ago      systemd-tmpfiles-clean.timer        systemd-tmpfiles-clean.service
Sat 2022-11-19 16:08:56 UTC 4h 41min left Sat 2022-11-19 02:28:55 UTC 8h ago       motd-news.timer                     motd-news.service
Sat 2022-11-19 16:39:19 UTC 5h 11min left Sat 2022-11-19 00:55:29 UTC 10h ago      certbot.timer                       certbot.service
Sun 2022-11-20 00:00:00 UTC 12h left      Sat 2022-11-19 00:00:20 UTC 11h ago      dpkg-db-backup.timer                dpkg-db-backup.service
Sun 2022-11-20 03:10:08 UTC 15h left      Sun 2022-11-13 14:34:53 UTC 5 days ago   e2scrub_all.timer                   e2scrub_all.service
Sun 2022-11-20 03:36:53 UTC 16h left      Sat 2022-11-19 02:40:53 UTC 8h ago       man-db.timer                        man-db.service
Sun 2022-11-20 04:20:38 UTC 16h left      Sat 2022-11-19 02:40:57 UTC 8h ago       exim4-base.timer                    exim4-base.service
Sun 2022-11-20 04:46:41 UTC 17h left      Sat 2022-11-19 02:57:21 UTC 8h ago       plocate-updatedb.timer              plocate-updatedb.service
Sun 2022-11-20 05:00:00 UTC 17h left      Sat 2022-11-19 05:00:10 UTC 6h ago       mastodon-media-remove.timer         mastodon-media-remove.service
Sun 2022-11-20 05:30:00 UTC 18h left      Sat 2022-11-19 05:30:01 UTC 5h 57min ago mastodon-preview_cards-remove.timer mastodon-preview_cards-remove.service
Sun 2022-11-20 06:00:00 UTC 18h left      Sat 2022-11-19 06:00:08 UTC 5h 27min ago mastodon-search-deploy.timer        mastodon-search-deploy.service
Sun 2022-11-20 06:15:57 UTC 18h left      Sat 2022-11-19 02:40:47 UTC 8h ago       logrotate.timer                     logrotate.service
Sun 2022-11-20 06:56:49 UTC 19h left      Sat 2022-11-19 06:36:37 UTC 4h 50min ago apt-daily-upgrade.timer             apt-daily-upgrade.service

13 timers listed.
Pass --all to see loaded but inactive timers, too.

Inspect sample service state.

$ sudo systemctl status mastodon-search-deploy
○ mastodon-search-deploy.service - Mastodon - update Elasticsearch indices
     Loaded: loaded (/etc/systemd/system/mastodon-search-deploy.service; disabled; vendor preset: enabled)
     Active: inactive (dead) since Sat 2022-11-19 06:00:24 UTC; 5h 30min ago
TriggeredBy: ● mastodon-search-deploy.timer
    Process: 197450 ExecStart=/home/mastodon/live/bin/tootctl search deploy (code=exited, status=0/SUCCESS)
   Main PID: 197450 (code=exited, status=0/SUCCESS)

Nov 19 06:00:09 mastodon systemd[1]: Starting Mastodon - update Elasticsearch indices...
Nov 19 06:00:24 mastodon tootctl[197450]: Progress: |====================================================================|
Nov 19 06:00:24 mastodon tootctl[197450]: Indexed 9786 records, de-indexed 0
Nov 19 06:00:24 mastodon systemd[1]: mastodon-search-deploy.service: Deactivated successfully.
Nov 19 06:00:24 mastodon systemd[1]: Finished Mastodon - update Elasticsearch indices