使用命令获取网卡
ip address show
创建python3脚本,作为服务
INTERFACE="ens5"
SLEEPTIME=60*10 # 10 minutes
from subprocess import getoutput
import os, time
cmdstr="cat /sys/class/net/%s/statistics/tx_packets" % INTERFACE
def worker ():
pre_cnt=int(getoutput(cmdstr))
cnt=int(getoutput(cmdstr))
time.sleep(SLEEPTIME)
while True:
cnt=int(getoutput(cmdstr))
if cnt - pre_cnt<1000:
os.system("shutdown -h now")
else:
pre_cnt=cnt
time.sleep(SLEEPTIME)
worker()
创建开机启动脚本
#sudo vi /lib/systemd/system/auto-off.service
[Unit]
Description=Auto-off Service
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
ExecStart=sudo /usr/bin/python3 /home/ubuntu/auto-off.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
启用服务
$ sudo systemctl daemon-reload
$ sudo systemctl enable auto-off.service
$ sudo systemctl start auto-off.service
服务状态查询
$ sudo systemctl status auto-off.service
$ sudo systemctl stop auto-off.service #To stop running service
$ sudo systemctl start auto-off.service #To start running service
$ sudo systemctl restart auto-off.service #To restart running service