How to emit battery data in a one-liner on MacOS

In MacOS the command to emit data about battery status is:

pmset -g batt

If you want to record the battery percentage level in a file at regular intervals, here is how to record the data in a one-liner:

while true; do \
    echo "$(date "+%Y-%m-%d %H:%M:%S") " \
        "$(pmset -g batt | grep "%" | cut -f 2 | cut -f 1 -d ';')" \
        >> battery.txt; \
    sleep 60; \
done &

which does fit in a single line in Terminal šŸ˜‰


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: