---

blog: Don Marti

---

Prevent the system from sleeping when someone is logged in over ssh

08 October 2021

To prevent the Linux system from suspending or hibernating when a user is logged in, you can put this file, 10-no-sleep-ssh.sh into /etc/pm/sleep.d. Owned by root:root, mode 755.

#!/bin/bash

case "$1" in
  suspend|hibernate)
    if (who -u | awk '{print $2}' | grep -q pts/); then
    logger 'ssh user(s) logged in'
        exit 1
    fi
    ;;
esac

The who -u command lists the users who are logged in. A remote user will be on a pseudo-terminal and have "pts" in the second column of their line in the output.

If this script exits with an error (exit 1) the system will be prevented from sleeping.