I made this script when executed will shutdown my system (after some time) and should enable a screensaver.
#!/bin/bash
sudo shutdown -h +30
gnome-screensaver-command -a
There are mainly two problems in this script.
- It asks for password (which I don't want)
- After executing
sudo shutdown -h +30
its not runninggnome-screensaver-command -a
that is, it activates shutdown but not the screensaver
How to solve these problem?
Solution to problem 1 and 2:
You need to edit your
/etc/sudoers
(sudo visudo
) file to add the relevant entry to grant the passwordlesssudo
permission for the user to run this script (not theshutdown
command) :Here
foo
is the username,spamegg
is the hostname, replace the/path/to/script.sh
accordingly. Now run the command as (make the script executable):Note that the
gnome-screensaver-command -a
command will also run asroot
, if you don't want that you need to grant the permission for passwordlesssudo shutdown
command for the user:The second problem is due to the fact that in your current script the
gnome-screensaver-command -a
will only run after the completion ofshutdown
command. As theshutdown
command will actually be executed after 30 minutes, hence its in a blocking state. To solve this you can put theshutdown
command in the background :In a nutshell, you can make the script as following :
Run it as :
Before using
sudo
, store the script in a secure place, it must only be accessible to you and you must be sure of the contents of the script.When you have two commands on two separate lines of a shell script, the second will only run when the first has finished. To avoid this, you need to add a
&
to the end of the first command to have it run in the background:As for not asking for the password, you'll need to tell
sudo
that your user has the right to run/sbin/shutdown
with no password. Runsudo visudo
and add this line to/etc/sudoers
:Change
eka
to whatever your username is. Save the file and you will now be able to runsudo shutdown
without being prompted for a password.