Jellyfin Forum
Wake Jellyfin server for scheduled recordings - Printable Version

+- Jellyfin Forum (https://forum.jellyfin.org)
+-- Forum: Support (https://forum.jellyfin.org/f-support)
+--- Forum: Guides, Walkthroughs & Tutorials (https://forum.jellyfin.org/f-guides-walkthroughs-tutorials)
+--- Thread: Wake Jellyfin server for scheduled recordings (/t-wake-jellyfin-server-for-scheduled-recordings)



Wake Jellyfin server for scheduled recordings - Coyote - 2023-10-08

One big feature I miss from Windows Media Center is the ability to wake the server from standby/hibernate for scheduled recordings. WMC did it by creating a scheduled task set to trigger a few minutes before the start of each recording. Jellyfin makes it easy to do the same using PowerShell, as the timers are saved in plain JSON.

Save the following code as WakeToRecord.ps1 or similar. You'll need to configure the following params:

TASK_NAME is whatever you want the tasks to be called. TASK_NAME-update and TASK_NAME-wakeup will be registered.

TIMERS is the location of your Live TV timers JSON. Change this if you have moved your server's data dir.

Set USERNAME and PASSWORD to your credentials. You must use an interactive user here (not SYSTEM) as PowerShell cannot run as a non-interactive user.

WAKE_BEFORE is how long before each scheduled recording the server should wake up. The default is 2 minutes to ensure tuners/connections will be ready, but not long enough to allow the system to sleep.

Now right-click and "run with PowerShell" or launch it from a PowerShell terminal if you want to see the output. This will scan your timers.json and register two scheduled tasks. The update task will run every hour but will not wake the server if it is asleep. The wakeup task will wake the server before the next scheduled recording. Each time the update or wakeup task runs it will run the script to find any new scheduled recordings.

Code:
$TASK_NAME = 'Jellyfin-WakeToRecord'

$TIMERS = "C:\ProgramData\Jellyfin\Server\data\livetv\timers.json"

$USERNAME='your_username'
$PASSWORD='your_password'

$WAKE_BEFORE = New-TimeSpan -Minutes 2
$UPDATE_INTERVAL = New-TimeSpan -Hours 1

$action = New-ScheduledTaskAction –Execute "powershell.exe" -Argument $MyInvocation.MyCommand.Path
$settings = New-ScheduledTaskSettingsSet -WakeToRun
$update_task_name = $TASK_NAME+'-update'
$wakeup_task_name = $TASK_NAME+'-wakeup'

$task = Get-ScheduledTask -TaskName $update_task_name
if (!$task){
    Register-ScheduledTask -TaskName $update_task_name -User $USERNAME -Password $PASSWORD -Action $action
    Register-ScheduledTask -TaskName $wakeup_task_name -User $USERNAME -Password $PASSWORD -Action $action -Settings $settings
}
$start_time = (Get-Date) + $UPDATE_INTERVAL
Set-ScheduledTask -TaskName $update_task_name -Trigger (New-ScheduledTaskTrigger -Once -At $start_time) -User $USERNAME -Password $PASSWORD

$triggers=@()
$timers = Get-Content -Path $TIMERS | ConvertFrom-Json
foreach ($timer in $timers) {
    $waketime = (Get-Date -Date $timer.StartDate) - $WAKE_BEFORE
    $triggers += New-ScheduledTaskTrigger -Once -At $waketime
}
$triggers
Set-ScheduledTask -TaskName $wakeup_task_name -User "$USERNAME" -Password "$PASSWORD" -Trigger $triggers