Sudo in script not working

Hi all,

I know this is probably the wrong place to ask, but I figured if anyone would know, it’ll be you. And according to m e, it is a relatively simple problem.

My challenge:

I’m trying to build a little script for locking my screen, which that is easy enough, and works flawlessly.

The problem I’m running into is running the command

sudo --reset-timestamp

from the script before the screen is locked. I’ve also tried:

sudo --reset-timestamp --non-interactive

…to no avail.

The strange thing is, running the script from the terminal, with:

sudo --reset-timestamp

works fine, but running it with:

sudo --reset-timestamp --non-interactive

does nothing. But it makes kinda sense, though.

The big problem is, whenever I set the script to be run with a keypress shortcut, the

sudo --reset-timestamp

…or

sudo --reset-timestamp --non-interactive

…doesn’t work. Instead, it goes straight to the qdbus command for locking the screen.

Here is my complete, and simple script up to this point:

#!/usr/bin/env bash

[[ $(sudo --reset-timestamp --non-interactive) ]] && echo "Successfully reset sudo timestamp before locking screen." | systemd-cat --identifier=Mirdarthos --priority=info ||  echo "Failed to reset sudo timestamp before locking screen." | systemd-cat --identifier=Mirdarthos --priority=err
[[ $(qdbus org.kde.ksmserver /ScreenSaver org.freedesktop.ScreenSaver.Lock) ]] && echo "Successfully locked workstation." | systemd-cat --identifier=Mirdarthos --priority=info || echo "Failed to lock workstation." | systemd-cat --identifier=Mirdarthos --priority=err

I’ve also tried /usr/bin/sudo instead of just sudo but it made no difference.

I’ve also set the !requiretty default for my user:

$ cat /etc/sudoers.d/mirdarthos
[...]
Defaults:mirdarthos timestamp_timeout=-1
Defaults:mirdarthos !requiretty

Does anybody know why this is happening?

This doesn’t work, you can’t have non-interactive in conjuction with reset-timestamp. (You should see the help message).

Why do you wrap it here:

You could just use
sudo --reset-timestamp && echo "Successfully reset"

How do you confirm that it doesn’t work?

3 Likes

sudo --reset-timestamp doesn’t return anything upon success, so if the command succeeded the test fails because it’s testing an empty string.

Use sudo --reset-timestamp && like @mithrial said, it relies on the command succeeding.

Because, as per usual, my silly brain over-complicates thing. Thank you!

It didn’t give any, not when running the script from thee terminal, and not from the shortcut.

As you can see:

…I already made it once-login only, disabled the timeout. So I so a simple:

sudo ls /etc

…and enter the password, after which everything is displayed, so it’s working fine. When running:

sudo -k

as my user, not root, and running:

sudo ls /etc

…again, it asks for the password again. So it does seem to work.

I tried, my mind just over-complicated it, like I mentioned earlier. But I’ll do this now and see where that leaves me.

Edit:

Thank you @mithrial!!! I changed it accordingly, read: madee it simpler and uncomplicated it, and now it’s working like a charm!

@dmt I chose his answer as the solution, because you pointed there as well.

Thee finished script, for those interested:

#!/usr/bin/env bash

sudo --reset-timestamp && echo "Successfully reset sudo timestamp before locking screen." | systemd-cat --identifier=Mirdarthos --priority=info ||  echo "Failed to reset sudo timestamp before locking screen." | systemd-cat --identifier=Mirdarthos --priority=err
qdbus org.kde.ksmserver /ScreenSaver org.freedesktop.ScreenSaver.Lock && echo "Successfully locked workstation." | systemd-cat --identifier=Mirdarthos --priority=info || echo "Failed to lock workstation." | systemd-cat --identifier=Mirdarthos --priority=err
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.