logoalt Hacker News

clash • today at 1:22 AM • 1 reply • view on HN

Can your one line terminal invocation automatically put the MacBook to sleep when you are connected via ssh and the connection is lost?


Replies

nomel • today at 2:00 AM

Technically one line ;)

    sudo bash -c 'trap "pmset -a disablesleep 0" EXIT; s=; while :; do if lsof -nP -iTCP:22 -sTCP:ESTABLISHED >/dev/null 2>&1 || pgrep -xq ssh;
  then n=1; else n=0; fi; [ "$n" != "$s" ] && pmset -a disablesleep $n && s=$n; sleep 10; done'