Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] howto start xscreensaver from commandline ?

Graphical Environments, Managers, Multimedia & Desktop questions.
Post Reply
Message
Author
oida
Posts: 58
Joined: 2019-02-04 15:27
Has thanked: 13 times

[Solved] howto start xscreensaver from commandline ?

#1 Post by oida »

Hello,
i was looking for a possibility to lock the screen and unlock it with a Yubico Key.I tried swaylock but give up because of swaylock startup error (Missing ext-session-lock-v1, wlr-layer-shell, wlr-input-inhibitor.

Then i switched from Wayland to X11 and installed xscreensaver, edited /etc/pam.d/xscreensaver and voila, screen locking and unlocking works better than expected. The only thing i am unable to figure out is how to start the screensver from a terminal. Executing xscreensver strats the xscreensaver settings and from there starts the process. I figured out it doesn't seem a "really" daemin but rather a background process.

So, i don't want to start the screensaver at startup, on demand. Ok, why ? Sometimes i am on the road and don't want to log off or shut down but instead have a short command to start the screensaver, i.e. probably in a shell script like :
#! /bin/sh
#load screensaver
"command to start the screensave......r"
#go to immediat lockscreen
xscreensaver-command -lock
Thank you in advance for any help
Last edited by oida on 2024-02-12 18:18, edited 1 time in total.

User avatar
ruwolf
Posts: 594
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 33 times
Been thanked: 19 times

Re: howto start xscreensaver from commandline ?

#2 Post by ruwolf »

I do not understand you in this part:
i don't want to start the screensaver at startup, on deman
This should activate it (and lock the screen, too).

Code: Select all

xscreensaver-command -activate -lock

User avatar
fabien
Forum Helper
Forum Helper
Posts: 564
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 57 times
Been thanked: 130 times

Re: howto start xscreensaver from commandline ?

#3 Post by fabien »

Barely tested but should work
man 1 xscreensaver-command wrote:--watch Prints a line each time the screensaver changes state: when the screen blanks, locks, unblanks, or when the running hack is changed. This option never returns; it is intended for use by shell scripts that want to react to the screensaver in some way.

Code: Select all

#!/usr/bin/env bash

pidof xscreensaver && {
 echo "xscreensaver already running, exit"
 exit 1; }

xscreensaver &                               ### run xscreensaver in the background
declare -i XSSPID=$?                         ### xscreensaver PID
sleep 1                                      ### wait xscreensaver initialisation

xscreensaver-command --lock                  ### lock the screen

while IFS='' read -r STATE; do               ### wait unlock
   [[ "$STATE" == UNBLANK\ * ]] && break
done < <(xscreensaver-command --watch)

kill $XSSPID                                 ### kill xscreensaver PID

oida
Posts: 58
Joined: 2019-02-04 15:27
Has thanked: 13 times

Re: howto start xscreensaver from commandline ?

#4 Post by oida »

ruwolf wrote: 2024-02-11 22:58 I do not understand you in this part:
i don't want to start the screensaver at startup, on deman
Well, i mean i do not want the screensaver to autostart at login, but instead i want to start the screensaver directly from command line
This should activate it (and lock the screen, too).

Code: Select all

xscreensaver-command -activate -lock
[/quote]

Sorry, that does not work.
reinhard@P14s:~$ xscreensaver-command -activate -lock
xscreensaver-command: extraneous: -lock
xscreensaver-command: try --help
reinhard@P14s:~$ xscreensaver-command -activate
xscreensaver-command: no screensaver is running on display :0
reinhard@P14s:~$

oida
Posts: 58
Joined: 2019-02-04 15:27
Has thanked: 13 times

Re: howto start xscreensaver from commandline ?

#5 Post by oida »

fabien wrote: 2024-02-11 23:17 Barely tested but should work
man 1 xscreensaver-command wrote:--watch Prints a line each time the screensaver changes state: when the screen blanks, locks, unblanks, or when the running hack is changed. This option never returns; it is intended for use by shell scripts that want to react to the screensaver in some way.

Code: Select all

#!/usr/bin/env bash

pidof xscreensaver && {
 echo "xscreensaver already running, exit"
 exit 1; }

xscreensaver &                               ### run xscreensaver in the background
declare -i XSSPID=$?                         ### xscreensaver PID
sleep 1                                      ### wait xscreensaver initialisation

xscreensaver-command --lock                  ### lock the screen

while IFS='' read -r STATE; do               ### wait unlock
   [[ "$STATE" == UNBLANK\ * ]] && break
done < <(xscreensaver-command --watch)

kill $XSSPID                                 ### kill xscreensaver PID
Well, that's what i was looking for, thanbks a lot for jumping in!. Still some errors in the script and as always the Setup XScreensaver Windows still pops up ....
The errors i received are :
reinhard@P14s:~/temp$ sh scrtest.sh
scrtest.sh: 8: declare: not found
xscreensaver-command: locking

xscreensaver: 12:35:25: couldn't grab keyboard: AlreadyGrabbed
scrtest.sh: 15: Syntax error: redirection unexpected

User avatar
fabien
Forum Helper
Forum Helper
Posts: 564
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 57 times
Been thanked: 130 times

Re: howto start xscreensaver from commandline ?

#6 Post by fabien »

Hello, could you please use code tags instead of quote tags for terminal outputs, thanks.
oida wrote: 2024-02-12 11:43

Code: Select all

scrtest.sh: 8: declare: not found
[...]
scrtest.sh: 15: Syntax error: redirection unexpected
This is unexpected. Could you please attach a copy of your script

Code: Select all

$> gzip -k /tmp/scrtest.sh
(the file to attach is /tmp/scrtest.sh.gz)

oida
Posts: 58
Joined: 2019-02-04 15:27
Has thanked: 13 times

Re: howto start xscreensaver from commandline ?

#7 Post by oida »

Hello Fabien,
sorry for the wrong tags. In pastebin i can't upload a gz file and i won't do something wrong so i will copy / paste the script here.

Code: Select all

#!/usr/bin/env bash

pidof xscreensaver && {
 echo "xscreensaver already running, exit"
 exit 1; }

xscreensaver &                               ### run xscreensaver in the background
declare -i XSSPID=$?                         ### xscreensaver PID
sleep 1                                      ### wait xscreensaver initialisation

xscreensaver-command --lock                  ### lock the screen

while IFS='' read -r STATE; do               ### wait unlock
   [[ "$STATE" == UNBLANK\ * ]] && break
done < <(xscreensaver-command --watch)

kill $XSSPID                                 ### kill xscreensaver PID

User avatar
fabien
Forum Helper
Forum Helper
Posts: 564
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 57 times
Been thanked: 130 times

Re: howto start xscreensaver from commandline ?

#8 Post by fabien »

OK, I see. You probably launched your script like this:

Code: Select all

$> sh scrtest.sh
This bypasses the shebang (the first line of the script) and therefore it is not interpreted by bash but sh.
You must do:

Code: Select all

$> bash scrtest.sh
or (preferably)

Code: Select all

$> chmod +x ./scrtest.sh      ### give execution rights to the file
$> ./scrtest.sh
oida wrote: 2024-02-12 13:57 In pastebin i can't upload a gz file and i won't do something wrong so i will copy / paste the script here.
The forum has a feature for attaching files, but that's probably not necessary now.

oida
Posts: 58
Joined: 2019-02-04 15:27
Has thanked: 13 times

Re: howto start xscreensaver from commandline ?

#9 Post by oida »

Hello Fabien,

yes, that makes the job. I would thank you so much for helping me to solve this "problem". Everything works now as it should (i think that's a Debian thing :mrgreen: ).

I use Debian, let's say, straight out of the box and so i am happy that everything is running well but on the other hand i am a little bit unhappy that, especially at this time, support for Hardware Keys is that bad and also it seems to me that this Wayland Dingi isn't that stable as it should despite it is the standard in Debian.

However, a big thumbs up for your help

All the best from rainy Austria

Post Reply