I wrote these two scripts and they are in my $PATH:
gsudo:
#! /bin/bash
sudo -n true &> /dev/null
if [ $? -eq 0 ]
then
sudo "$@"
else
upass=$(zenity --password --title "Enter your password" 2> /dev/null)
[[ ! -z "$upass" ]] && echo $upass | sudo -S -p "" "$@"
fi
This script essentially does the same thing what gksu does. It looks if sudo needs password or not and according to the result of that it will ask password or not.
run-something:
gsudo command1
gsudo command2
If I run the second script from a terminal window, I get the expected behavior. It simply asks for my password only one time. But if I call it from a graphical environment, like a task runner(I tried i3wm dmenu and rofi launcher) it asks for my password two times. So why is this happening, how can I fix it? I believe sudo -n true should return the same thing both times, because it's getting called in same script so sudo session should persist. I'm not trying to save sudo session between different script calls, it's enough that it persists only in one call.