我在运行 Xenial 的 Intel Nuc 上运行 HTPC。当我按下电源按钮时,它会运行 powerbtn.sh,从而启动关机,但我的树莓派能够唤醒它,正如我希望的那样。
问题是,我如何让 ubuntu 在空闲 5 分钟时运行它。我担心的两个因素是 MythTV 和 ssh 不应该在那 10 分钟内提供服务。
我认为我可以使用此处的解决方案来阻止 SSH防止 SSH 连接开启时机器进入睡眠状态
我相信 MythTV 应该自动被操作系统考虑在内。
但是我怎样才能把所有这些结合起来并让其发挥作用呢?
感谢您的时间!
编辑:经过深思熟虑,我制定了这个脚本,计划每 15 分钟在 cron 上运行一次。任何建议都将不胜感激
#!/bin/bash
#check for SSH sessions, and prevent suspending:
if [ "$(who | grep -cv "(:")" -gt 1 ]; then
echo "SSH session(s) are on. Not suspending."
exit 1
fi
#check for MythTV sessions, and preventing suspending:
if [ "$(netstat -tun | grep :6543 | grep -i established | wc -l)" -gt 0 ]; then
echo "MythTV is still streaming. Not suspending."
exit 1
fi
sleep 5m
#check for SSH sessions, and prevent suspending:
if [ "$(who | grep -cv "(:")" -gt 1 ]; then
echo "SSH session(s) are on. Not suspending."
exit 1
fi
#check for MythTV sessions, and preventing suspending:
if [ "$(netstat -tun | grep :6543 | grep -i established | wc -l)" -gt 0 ]; then
echo "MythTV is still streaming. Not suspending."
exit 1
fi
echo "Safe to shutdown from MythTV and SSH"
/etc/acpi/powerbtn.sh
答案1
我无法让 MythTV 系统事件正常工作。我尽了最大的努力尝试权限,但都徒劳无功。这是我当前的工作代码。用您的设置替换数据库变量。将其保存为/etc/cron.daily/idle_htpc.py
如果任何步骤失败,程序将退出。sudo crontab -e
每5-59/15 * * * * /etc/cron.daily/idle_htpc.py /var/log/htpc_out 2> /var/log/htpc_err
15 分钟就这样做一次
步骤 1:查看是否有任何 SSH 连接
第 2 步:检查当前是否正在播放直播或录制的视频。如果没有,则休眠 5 分钟
步骤 3:再次检查当前正在播放的是直播视频还是录制视频
步骤 4:检查过去 5 分钟内是否有直播,以防在步骤 2 中脚本处于休眠状态时出现少于 5 分钟的直播。但是,如果有录制的回放,则这里没有办法检测。
步骤 5:检查下一个即将进行的录音,如果有,并且距现在还有 5 分钟,则在开始时间前 3 分钟设置 ACPI 唤醒并关机,否则清除 ACPI 唤醒和关机。
希望这对某人有帮助。
#!/usr/bin/python3
import pymysql
import pymysql.cursors
from datetime import timedelta,datetime
import subprocess
import time
import sys
print ( datetime.now().strftime("%Y-%m-%d %H:%M:%S"), file=sys.stdout)
def check_Logins():
#Returns the number of SSH connections
#result= subprocess.run('who | grep -cv "(:"', stdout=subprocess.PIPE, shell=True)
result= subprocess.run("netstat -n |grep tcp |grep ':22' |wc -l", stdout=subprocess.PIPE, shell=True)
print ('Logins:'+result.stdout.decode('UTF-8'), file=sys.stdout)
return (int(result.stdout.decode('UTF-8')))
def check_if_InUse():
#Checks if a live stream or a previosuly recorded program is being served at this moment. If yes, non zero value is returned
InUse=1
connection = pymysql.connect(host=$host,
user=$username,
password=$password,
db=$db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "select count(*) from inuseprograms where recusage !='jobqueue' and recusage !='flagger'"
cursor.execute(sql)
result = cursor.fetchone()
InUse = result["count(*)"]
finally:
connection.close()
print ("Count: "+str(InUse), file=sys.stdout)
return InUse
def check_live_5min():
#Checks if there were any live streams served in last 5 minutes
RecInUse_5min=1
connection = pymysql.connect(host=$host,
user=$username,
password=$password,
db=$db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "select max(starttime) from recordedseek"
cursor.execute(sql)
result = cursor.fetchone()
print ('Recent: ', result['max(starttime)'], file=sys.stdout)
print ('Current: ',datetime.utcnow(), file=sys.stdout)
if datetime.utcnow()-result['max(starttime)']>=timedelta(minutes=5):
RecInUse_5min =0
finally:
connection.close()
return RecInUse_5min
def get_next_rec():
#Returns a tuple indicating if there is any scheduled recording if yes, how far into the future
retdata = (True, timedelta(minutes=1))
connection = pymysql.connect(host=$host,
user=$username,
password=$password,
db=$db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "select MIN(next_record) from record where recordid!=1 and next_record IS NOT NULL;"
cursor.execute(sql)
result = cursor.fetchone()
if result['MIN(next_record)'] is None: #no scheduled recordings
retdata= (False,timedelta(minutes=1))
else:
retdata= (True,result['MIN(next_record)']-datetime.utcnow())
finally:
connection.close()
return retdata
if check_Logins()==0:
print ('First check passed', file=sys.stdout)
if check_if_InUse() == 0:
print ('Second check passed', file=sys.stdout)
time.sleep(5*60)
if check_if_InUse() == 0:
print ('Third check passed', file=sys.stdout)
if check_live_5min()==0:
print ('Fourth check passed', file=sys.stdout)
(valid,upcoming) = get_next_rec()
# Clear any previously set wakeup time
result = subprocess.run('echo 0 > /sys/class/rtc/rtc0/wakealarm',stdout=subprocess.PIPE,shell=True)
print ('Clear:'+result.stdout.decode('UTF-8'), file=sys.stdout)
if valid is True:
# Generate wakeup time string
upcoming = upcoming - timedelta(minutes=3)
wakeup_string="'+"+str(upcoming.days)+" days + "+str(max([int(upcoming.seconds/60)-3,0]))+" minutes'"
print ('Setting for '+wakeup_string, file=sys.stdout)
wakeup_command="echo `date '+%s' -d " +wakeup_string+ "` > /sys/class/rtc/rtc0/wakealarm"
#Setup wakeup time
result = subprocess.run(wakeup_command,stderr=subprocess.PIPE, shell=True)
print ('Set:'+result.stderr.decode('UTF-8'), file=sys.stdout)
#Check if alarm is set. It should show the unix time stamp
result= subprocess.run('cat /sys/class/rtc/rtc0/wakealarm', stdout=subprocess.PIPE, shell=True)
print ('Check:'+result.stdout.decode('UTF-8'), file=sys.stdout)
print ('Shutting down', file=sys.stdout)
subprocess.Popen(['/sbin/shutdown', '-h', 'now'])