Created
April 22, 2023 19:08
-
-
Save CodeZombie/76e03b60c2e55dae0b6e95c2c2089618 to your computer and use it in GitHub Desktop.
plexpowerd.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Plexhometheater closes itself after 30 minutes of inactivty | |
#This script checks to see if plexhometheater is off, and if it is, | |
#checks for any major network traffic (to indicate movie stream, or file xfer) | |
#if no network activity is detected, the computer shuts down. | |
#if network activity IS detected, plexhometheater is started again | |
def plexIsRunning() | |
command = `ps -ef|grep -v grep|grep /opt/plexhometheater/bin/plexhometheater` | |
if command == "" then | |
return false | |
end | |
return true | |
end | |
def totalBandwidth() | |
command = `ifconfig eth0|grep "RX bytes:"` | |
m = Regexp.new('\\d+') | |
rx_bytes = m.match(command).to_s.to_i | |
tx_bytes = m.match(command.to_s.split(')')[1]).to_s.to_i | |
return rx_bytes + tx_bytes | |
end | |
def checkTimer(t) | |
if t - Time.now.to_i <= 0 then | |
return true | |
end | |
return false | |
end | |
#set initial variables: | |
#time to shutdown = (time it takes plex to shutdown) + (_MAX_TICKS * _NETWORK_TIMER_INTERVAL) | |
_MAX_TICKS = 5 | |
_PLEX_TIMER_INTERVAL = 5 * 60 | |
_NETWORK_TIMER_INTERVAL = 3 * 60 | |
plex_timer = Time.now.to_i + _PLEX_TIMER_INTERVAL | |
network_timer = Time.now.to_i + _NETWORK_TIMER_INTERVAL | |
previous_bandwidth = totalBandwidth() | |
network_activity_counter = _MAX_TICKS | |
while(true) | |
if checkTimer(network_timer) then | |
if totalBandwidth() - previous_bandwidth >= 256000 then | |
network_activity_counter = _MAX_TICKS | |
else | |
network_activity_counter -= 1 | |
end | |
previous_bandwidth = totalBandwidth() | |
network_timer = Time.now.to_i + _NETWORK_TIMER_INTERVAL | |
end | |
if checkTimer(plex_timer) then | |
if plexIsRunning() then | |
plex_timer = Time.now.to_i + _PLEX_TIMER_INTERVAL | |
else | |
if network_activity_counter > 0 then | |
`plexhometheater.sh` | |
plex_timer = Time.now.to_i + _PLEX_TIMER_INTERVAL | |
else | |
`sudo poweroff` | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment