電池電量低自訂規則(休眠)

電池電量低自訂規則(休眠)

我喜歡濫用筆記型電腦電池,即使電池電量顯示為 0% 後,我仍會長時間使用筆記型電腦(最多 25 分鐘)。

然而,有時我會忘記這一點,或者我只是離開了。這會導致硬關閉,從而有可能損壞檔案系統。

讓電腦在電池報告電量耗盡 15 分鐘後自動休眠的最佳方法是什麼?我的想法是編寫一個 Ruby 或 Bash 腳本來定期輪詢適當的/proc/子系統,但我想知道是否有任何內建的東西。

答案1

我不會給你一個關於電池的講座,因為你自己使用了「濫用」這個詞:)。

這樣做的一種方法是這樣的:

#!/usr/bin/env bash

while [ $(acpi | awk '{print $NF}' | sed 's/%//') -gt 0 ]; do
    ## Wait for a minute
    sleep 60s
done

## The loop above will exit when the battery level hits 0.
## When that happens, issue the shitdown command to be run in 15 minutes
shutdown -h +15

您可以將其新增/etc/crontab為由 root 運行。

答案2

對於任何想要類似功能的人,這裡有一個 Ruby 腳本。

它支援連續多次掛起(耗盡、掛起、充電、耗盡、掛起…),並且盡可能穩健。

現在它還支持libnotify,因此您每分鐘都會收到通知。

#!/usr/bin/ruby
    require 'eventmachine'
    require 'libnotify'

    period = 40 # poll evey N seconds
    limit = (ARGV[0] || 20).to_i # allow usage N minutes after depletion

    def get(prop)
        File.read("/sys/class/power_supply/BAT0/#{prop}").chomp
    end

    def capacity
        get(:charge_now).to_i
    end

    def onBattery?
        get(:status) != 'Charging'
    end

    def action!
        `sync`
        `systemctl suspend`
    end

    puts 'Starting battery abuse agent.'

    EM.run {
        ticks = 0
        EM.add_periodic_timer(period) {
            if capacity == 0 && onBattery?
                ticks += 1
                if ticks % 5 == 0
                    Libnotify.show summary: 'Baterry being abused',
                        body: "for #{period*ticks} seconds.", timeout: 7.5
                end
            else
                ticks = [ticks-1, 0].max
            end
            if ticks*period > limit*60
                action!
            end
        }
    }

相關內容