Score:0

restart service daily at 4 am

cn flag

I have a small minecraft server running in ubuntu 22.04.1.

Currently, ubuntu has the following service:

[Unit]
Description=Minecraft server
After=network.target

[Service]
User=drgadmin
WorkingDirectory=/home/drgadmin/minecraft-server/
ExecStart=bash ./start-server.bash
Restart=on-failure
RestartSec=30s
StartLimitInterval=10m
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

./start-server.bash creates a new backup, then starts the server.

In order to have a daily backup routine, I would like to restart the service daily at 4 am.

Is it possible to configure this daily restart of the service in the service file? If not, what alternatices can I use?

This is my current start-server.bash:

#!/bin/bash 
ServerFolder=./paper-1.19.3/

# Current date
now=$(date +"%Y-%m-%d")

# Create backup folder if it doesn't exist
if [ ! -d "./backups" ]; then
  mkdir "./backups"
fi

# Create a zip archive of the folder specified in the variable ServerFolder
# the file name is adjusted with the current date
filename="$(basename "$ServerFolder")_$now.tar.gz"
tar cf - "$ServerFolder" | pv | gzip > "./backups/$filename"

# Keep one week of daily backups
# Keep one monthly backup after that time
find "./backups" -type f ! -name "$(basename "$ServerFolder")_*-??-01.tar.gz" -mtime +62 -delete

cd "$ServerFolder"
java -Xms1024M -Xmx1024M -jar ./server.jar nogui
julian bechtold avatar
cn flag
the script is used to create a backup of the server before it starts. Starting the server directly will not create a backup.
julian bechtold avatar
cn flag
my fundamental problem is not how to stop the server safely. So far, my server gets stopped properly when I request service stop. My question is: is it possible to configure a service to restart at a given time daily?
user535733 avatar
cn flag
Certainly. You can use cron or systemd timers to do so.
cn flag
@julianbechtold see the oncalender event for systemd if you want to do it from within your unit. you can do it from cron but that is the old pre-systemd method ;-)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.