After running this command switch immediately to the tty where the screen is black, and after 5 seconds the screen should show up.
sleep 5 && xrandr -d :0 --output LVDS --auto
The xrandr command
will turn on your main screen. It only seems to work if you're currently on the tty where your display manager is run, hence the sleep
to give you some time to be able to switch.
The other question that muru has linked in the comments ( TTY[1-6]: Lock screen after delay (like a screensaver) ) offers us the tool to locking the tty , vlock
. At the simplest level , you can combine vlock -a
with pm-suspend
into a .bashrc
function or a script. Here's mine:
$ cat lockTTY.sh
#!/bin/bash
(sleep 3; sudo pm-suspend) &
vlock -a
What is happening here ? Basically we're launching pm-suspend
with delay in subshell , in background. Meanwhile we use vlock -a
to lock all the ttys. After 3 seconds, the laptop suspends.
The catch here however is the sudo pm-suspend
part. You have to prevent sudo
from asking you password for pm-suspend
. In order to do that, we add the following line at the end of /etc/sudosers
file
$USERNAME ALL = NOPASSWD: /usr/sbin/pm-suspend
Of course , replace $USERNAME
with your actual username. You might call sudo visudo
to open that file with your default text editor set in /etc/alternatives/editor
, just to be safe, but any editor called with proper permissions will do.
What does this script allows us to do ? Suspend and lock with processes still running. vlock -a
has big advantage in preventing switching to other consoles, so it's not just one console being locked, but all of them - you cannot just switch to another tty if one is locked.
You could also suspend first and lock second, i.e. call pm-suspend
first and vlock -a
second. But that means upon resume there is possibility someome may see your screen for a fraction of a second before vlock
kicks in.
What would be the simple and dirty solution in case you don't trust vlock
and don't want to install it ? Create a script /etc/pm/sleep.d/10_lockTTY
with the following contents:
#!/bin/bash
case "${1}" in
hibernate|sleep)
;;
resume|thaw)
for NUM in $(seq 1 6); do service tty$NUM restart; done
;;
esac
This will reset all ttys upon return from suspend, but mind - any processes you had there will be killed.
Best Answer
I know this is old, but I found you on google searching for the same question.
If you’re using bash, it’s no problem. Just use the environment-variable 'TMOUT' which is used by bash for a auto-logout (logout! not lock) in the given seconds.
You can just type ''TMOUT=5'' into any bash an see, what happens.
For configuration in my bashrc I’m using
so I don’t get logged out on a terminal-emulator which is normally on /dev/pty/# and already get locked by my screensaver as part of my desktop