Hi folks. Is there an approved method to schedule a poweroff at one specific time every night? Say, at 11pm for example.
The use case is my Pi 4B Plex server. Note that the external media drive is already set to be controlled by the Pi, so just the Pi needs a safe shut down. As well, the Pi is on a smart plug so the whole thing can be turned back on remotely, or in person.
So what’s remaining is just to learn how to implement a scheduled poweroff.
I don’t see anything specific to that in the config menus – unless it’s perhaps a custom script in Autostart (although that’s seemingly for start up and not shut down).
Thanks. I’m definitely a novice when it comes to this aspect of it. I’m not even sure where this command would go for starters
So, where is the best place for me to learn a bit? I ask since it’s possible I may want to evolve the command a bit – for example, so Plex shuts down only 6 nights a week. That would leave 1 night for Plex to do its scheduled backups, database checks, etc.
The Plex scheduling is super easy to change in their GUI. But if I’m going to attempt things from the Pi end… I should learn a bit more about this command you gave me (especially if it is at all unique to DietPi).
Theoretically you could ask AI on how Debian/Linux cron is working. How to add to the respective configuration files. This is plain Debian and has nothing specific for DietPi. Same goes to systemd.
For the sake of completeness, here is what ChatGPT ended up giving me, with me tailoring it a bit. Here’s the summary I asked for, after we got it all done. I removed my Plex token of course, and the URL is just the standard Plex URL and port, so no issue sharing that. I allowed Fri and Sat nights for later Plex use, and that also covers Plex needing at least one night a week to do its backups and database tidying.
Raspberry Pi Plex Smart Shutdown — Summary
1️⃣ Script File
Location: /usr/local/bin/plex-safe-shutdown.sh
Contents (replace YOUR_TOKEN_HERE with your Plex token):
#!/bin/bash
# Your Plex token
PLEX_TOKEN="YOUR_TOKEN_HERE"
# Plex server URL (localhost)
PLEX_URL="http://127.0.0.1:32400/status/sessions"
# Logging function
log() {
echo "$(date): $1" >> /var/log/plex-safe-shutdown.log
}
# Loop until no active streams
while true; do
# Count active <Video> sessions
ACTIVE_SESSIONS=$(curl -s "${PLEX_URL}?X-Plex-Token=${PLEX_TOKEN}" | grep -c "<Video")
if [ "$ACTIVE_SESSIONS" -eq 0 ]; then
log "No active streams. Shutting down."
/sbin/shutdown -h now
exit 0
else
log "Active streams detected ($ACTIVE_SESSIONS). Waiting 30 minutes."
sleep 1800 # wait 30 minutes
fi
done
Then to make executable:
sudo chmod +x /usr/local/bin/plex-safe-shutdown.sh
2️⃣ Cron Job
Root crontab:
sudo crontab -e
Entry added:
# Run Plex-safe shutdown script at 11 PM Sunday-Thursday.
# The script checks Plex for active streams.
# If nobody is watching, it shuts down the Pi immediately.
# If someone is streaming, it waits 30 minutes, then checks again.
# This loop continues every 30 minutes until Plex is idle.
0 23 * * 0-4 /usr/local/bin/plex-safe-shutdown.sh
Check cron:
sudo crontab -l
3️⃣ Log File
Location: /var/log/plex-safe-shutdown.log
View log:
cat /var/log/plex-safe-shutdown.log
Tail last few lines:
tail /var/log/plex-safe-shutdown.log
Follow log in real time:
tail -f /var/log/plex-safe-shutdown.log
4️⃣ Testing (Optional)
• Test mode: Temporarily replace shutdown line with:
log "Would shut down now (test mode)."
exit 0
• Run script manually:
sudo /usr/local/bin/plex-safe-shutdown.sh
• Check log for output.
• Restore real shutdown command after testing:
/sbin/shutdown -h now
✅ Notes
• Script runs 11 PM Sunday–Thursday only.
• Waits 30 minutes between checks if someone is streaming.
• Logs every check and decision.
• Plex token allows querying the local Plex server.
• No action is taken on Friday or Saturday nights.
@Joulinar Well, AI is certainly far from perfect, so thanks for catching that. As far as errors go, at least that’s a fairly minor one. AI was suggested to me so I figured I should share the result.
What would you have me modify? I have a backup of my SD card of course, so none of this is permanent, should I want to jump back and set this up differently.
I should ask though, if you have a log file building up over time… is that also an issue? Is there a way to automatically trim down a log file size once a month, or once every six months, etc?
@Jappe The check for active streams is a bit cute certainly But it does prevent kicking my son (who is away at university) off Plex after I’ve already gone to bed. That it checks every 30 mins until it can shut down “cleanly” is useful in that case. Soon I’ll have a daughter out there too
My opinion is that the log file is perhaps the part that’s less necessary, as it should be pretty obvious that the script is working if five days a week the Pi is shut down when I get up in the morning. But I know log files can be useful for troubleshooting and that’s why it was suggested, so I went with it.
Happy to take any suggestions to start over, or to just make a small adjustment to what’s already been set up.
depending on the criticality of the log file, you could store it in root user home directory or somewhere on in /mnt/dietpi_userdata/
As far as the size of log files is concerned, they can certainly grow over time. However, plain text files are usually very small. But yes, there are certainly ways to reduce their size (truncate).