Auto poweroff server after inactivity

Hello all,

Is there a way to automatically poweroff the server after x hours of inactivity? I’m guessing a simple cronjob running a script curling jellyfin using an API key should work, but I can’t seem to find an up-to-date, exhaustive list of API endpoints. Can you point me in the right direction?


I found the plugin API here, though I don’t know if that’s going to be of much help for what you want (perhaps query the current sessions and wait for an empty list to be returned?).

It might be easier to just monitor the logfile for a cessation in activity.

Thanks for your answer. I had found the link but thought it was only plugin related. After digging a little bit, it seems that this is also the API I can use with simple curl calls.

The logfile has a lot of output: I would like to shutdown if there was no playback for x hours. Trying to find what request I have to write for that. :slight_smile:

Ah, you must be a youngster :face_with_monocle:… Back in my day, we couldn’t even afford the luxury of parsing massive log files; CPU power and storage speed just made it impractical.

What I’d do in this situation is to check for the last-modified timestamp of the log file, then act based on that - this has the added benefit of not killing the server mid-way through a media scan or other scheduled process, it’ll simply kill it x hours after it’s completed the last thing it was doing.

Try something like this…

sFile=$(ls /var/log/jellyfin/jellyfin*.log | tail -1)
tCurr=`date +%s`
tFile=`stat -c "%Y" $sFile`
if [ $tIdle -gt $tDoze ]; then 
    echo "Server has been idle for $tIdle seconds, shutting down..."
    shutdown -P 0
    echo "Server has been idle for $tIdle seconds."

Change the value assigned to tDoze at the beginning to suit your taste; the sample is two hours.

I’m worried that checking the logfile’s timestamp might lead to false “activity” because of scheduled jellyfin actions.

Thanks a lot for your script! In fact I just wrote my own after digging a little in the plugin API. It’s fish shell because I a youngster :wink:

#!/usr/bin/env fish

set JELLYFIN_URL $argv[1]
set API_KEY $argv[2]

set LAST_ACTIVITY (curl -s "{$JELLYFIN_URL}/Users?api_key={$API_KEY}" | jq .[].LastActivityDate -r)

set NOW (date +"%s")

    set TIME (date --date={$ACTIVITY} +"%s")
    set DELAY (math $NOW - $TIME)
        set DO_SHUTDOWN 0

if test $DO_SHUTDOWN -eq 1
    sudo systemctl poweroff

Touché! At my age I’m no longer pursuing fishy things; I just spend time bashing my head against the wall :rofl:

Glad you got it worked out :+1:

1 Like