CLI Ubuntu lock screen after X minutes

Solution 1:

Use:

gnome-screensaver-command --lock

So you could run, for example, ((sleep $[123*60]; gnome-screensaver-command -l)&) to run gnome-screensaver-command after 123 minutes (123*60 seconds).


But there is a more convenient way:

  1. Set Ubuntu to lock the screen after 5 minutes of inactivity.
  2. Configure the movie player to keep the session "active" while the movie is playing. Totem does it automatically; other players usually have some way or other. (The option might be called "inhibit screensaver" or similar.)

Solution 2:

Remember that you can simplify that command. gnome-screensaver-command --lock is quite a long command to write, so you can make it easier by creating an alias for it.

Simply run

gedit ~/.bashrc

Now scroll down to the end of the file and add the following line

alias lock='gnome-screensaver-command --lock'

After having saved the file, whenever you run the command lock it actually executes gnome-screensaver-command --lock. Remember that .bashrc isn't a file you would normally see in your home folder because it is hidden by default. If you want it to pop into view just browse to your home folder in nautilus and press Ctrl + H.