Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3557

Programming • [Bash] Script to auto shutdown/wake no longer works after move from Ubuntu.

$
0
0
For some background, I've recently moved from Ubuntu server to Debian, something I've wanted to do for some time.

The server is purely for home use, and runs Jellyfin, TVheadend and Samba. Running Debian 12 on the same hardware (TS150 Lenovo)

On my previous install of Ubuntu server, I was running the following script (not written by me), which monitors for activity on various user defined ports, and shuts down after a pre-determined time idle, at which point it also writes to the time to wake for the next scheduled recording to the bios.

Code:

#!/bin/bash# BASIC SETTINGS change at wish# Settings you have to look at# TvHeadend login and passwordtvh_login=YOUR_LOGIN_NAMEtvh_password=YOUR_PASSWORD# (post recording) process activity to be checked, leave blank to deactivateprocess_check=(PROCESS_A PROCESS_B)# Settings you might look at# portnumbers to be checked for user activity (tvheadend 9981 + 9982 and samba 445)port_numbers=(9981 9982 445)# minimum time in seconds needed for consecutive shutdown AND startupsafe_margin_shutdown=600# minimum time in seconds needed to start up the computer properlysafe_margin_startup=180# minimum time in minutes not to shutdown after last user activity (watching tv/recording, up-/downloading files, booting the system)idle_time_shutdown=30# maximum time in hours not to wake up for updating EPGepg_hours=48# interval in seconds the script will check if the system should be shutdownscript_int=60# END OF BASIC SETTINGS keep out from here ;-)# retrieve IP_address tvheadend servertvh_ip=$(hostname -I | awk '{print $1}')# set languageexport LANG=C# set session startuptime_boot_sec=$(cat /proc/uptime | awk -F ' ' '{print$1}' | awk -F '.' '{print$1}')boot_time=$(($(date +%s)-uptime_boot_sec))# initial value do not changerecording=1# check for shutdownuntil [ $recording -eq 0 ];do# initial values do not change    recording=0    shutdown_timer=$idle_time_shutdown    ping_array=()# countdown to shutdown    until [ $shutdown_timer -eq 0 ];do        shutdown_timer=$((shutdown_timer-1))# check for server connection tvheadend samba or any other server according to settings above        status_netstat=1        until [ $status_netstat -eq 0 ];do            status_netstat=0            sleep $script_int            for i in $( echo "${port_numbers[*]}"); do                port_no=$i                for i in $(echo $(netstat -n | grep -i "ESTABLISHED" | grep "$tvh_ip:$port_no" |  awk -F ':' '{print ":"$2}' | sed 's/:'$port_no'\|[ ]//g')); do                    shutdown_timer=$idle_time_shutdown                    status_netstat=1                    ping_array[$(echo "${#ping_array[*]}")]=$i                done            done            ping_array=($(echo "${ping_array[*]}" | sed 's/[ ]/\n/g' | awk '!a[$0]++' ))        done        for i in $(echo "${ping_array[*]}"); do            if [ $( ping -c1 $i | grep "received" | awk -F ',' '{print $2}' | awk '{print $1}' ) -eq 0 ]; then                ping_array=($(echo "${ping_array[*]/$i/}"))            fi        done        if [ $(echo "${#ping_array[*]}") -eq 0 -a $boot_time -lt $(($(date +%s)-idle_time_shutdown*60)) ]; then            shutdown_timer=0        fi        if [ $(curl -s --user $tvh_login:$tvh_password http://127.0.0.1:9981/status.xml | grep "subscriptions" | awk -F '>' '{print $2}' | awk -F '<' '{print $1}') -ne 0 -a $shutdown_timer -eq 0 ]; then            shutdown_timer=1        fi    done# check for active post recording processes    total_processes=0    for i in $( echo "${process_check[*]}"); do        counter=$( ps -A | grep "$i" | wc -l )        total_processes=$((total_processes+counter))    done    if [ $total_processes -ne 0 ]; then        recording=$((recording+1))    fi# check for active users    if [ $(who | wc -l) -ne 0 ]; then        recording=$((recording+1))    fi# retrieve and calculate wake up data    if [ $(curl -s --user $tvh_login:$tvh_password http://127.0.0.1:9981/status.xml | grep "subscriptions" | awk -F '>' '{print $2}' | awk -F '<' '{print $1}') -eq 0 ]; then        wake_after_min=$((epg_hours*60))        if [ $(curl -s --user $tvh_login:$tvh_password 127.0.0.1:9981/status.xml | grep "next" | awk -F '>' '{print $2}' | awk -F '<' '{print $1}' | wc -l) -gt 0 ]; then            wake_after_min_temp=$(curl -s --user $tvh_login:$tvh_password 127.0.0.1:9981/status.xml | grep "next" | awk -F '>' '{print $2}' | awk -F '<' '{print $1}')            if [ $wake_after_min -gt $wake_after_min_temp ]; then                wake_after_min=$wake_after_min_temp            fi        fi    else        wake_after_min=0    fi    wake_after_secs=$((wake_after_min*60))# check safe margin shutdown    if [ $safe_margin_shutdown -gt $wake_after_secs ]; then        recording=$((recording+1))    fidone# set RTC wake up timestop_date=$(date +%s)wake_date=$((stop_date+wake_after_secs-safe_margin_startup))echo 0 > /sys/class/rtc/rtc0/wakealarmecho $wake_date > /sys/class/rtc/rtc0/wakealarm# shutdown computersudo shutdown -h now
I've used Webmin to setup the script as a systemd service to run from boot. I can confirm it's running, but it doesn't seem to work anymore. The output from "systemctl status wake.service" returns the following:

Code:

Loaded: loaded (/lib/systemd/system/wake.service; enabled; preset: enabled)     Active: active (running) since Mon 2024-04-01 19:32:50 BST; 8min ago   Main PID: 443 (wake.sh)      Tasks: 2 (limit: 9345)     Memory: 4.1M        CPU: 452ms     CGroup: /system.slice/wake.service             ├─ 443 /bin/bash /usr/local/bin/wake.sh             └─1698 sleep 60Apr 01 19:32:50 debian systemd[1]: Started wake.service - Auto shutdown and wake.Apr 01 19:34:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:35:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:36:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:37:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:38:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:39:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many argumentsApr 01 19:40:51 debian wake.sh[443]: /usr/local/bin/wake.sh: line 76: [: too many arguments
The script is quite old, but definitely worked on the most recent version of Ubuntu sever LTS. I'm not asking for somebody to just fix this for me, but maybe explain what the differences might be between Ubuntu and Debian in terms of the way the script is executed. I've got a bit of programming experience, but not with bash, and not for some time now.

Statistics: Posted by heyho — 2024-04-01 19:03 — Replies 0 — Views 23



Viewing all articles
Browse latest Browse all 3557

Trending Articles