4

I have this script to shutdown my system after 30 seconds. I want to run this script by double clicking it (that option I changed in nautilus). This is the content of my script

#!/bin/bash
shutdown -h +30;
echo "succesfull"
read -p "Press any key to continue... " -n1 -s

to make sudo script executable without a password I followed this answer and I am able to execute this script from the terminal without using a password (sudo ~/test/test.sh). The problem is when I double click the above script it's again asking for root privileges:

shutdown: Need to be root
successful
Press any key to continue... 

What's the problem here?

  • 1
    make a .desktop file? askubuntu.com/questions/64222/… – Tim May 22 '15 at 17:34
  • @Tim thanks but Is it possible to execute shell script (double click) itself without creating a .desktop file? – Eka May 22 '15 at 17:38
  • possibly... But you may keep having issues. A launcher is the standard way to do it. The command can be ./path/to/script of course. I take it you've make the script executable? – Tim May 22 '15 at 17:39
  • yes i made it executable and if i want to add it in a startup application (with additional codes) should we have to call the desktop file or the shell script – Eka May 22 '15 at 17:41
  • 1
    To add to a startup application, you can either put the launcher in ./config/autostart or in another script, set to start by referencing the script. – Tim May 22 '15 at 17:43
6

You can make a conditional to relaunch the script as root if it's launched as a normal user.


To shutdown the computer:

#!/bin/bash

if [[ $USER == "eka" ]]; then       # If the script is ran as "eka" then...
    sudo $0                         # relaunch it as "root".
    exit 0                          # Once it finishes, exit gracefully.
elif [[ $USER != "root" ]]; then    # If the user is not "eka" nor "root" then...
    exit 0                          # Once it finishes, exit gracefully.
fi                                  # End if.

shutdown -h +30;
read -p "Press any key to continue... " -n1 -s

Simplified version:

#!/bin/bash

[[ $USER == "eka" ]] && { sudo $0; exit 0; }
[[ $USER != "root" ]] && exit 0

shutdown -h +30;

Very simplified version (not recommended):

#!/bin/bash

sudo $0          # Relaunch script as root (even if it's already running as root)
shutdown -h +30; # Shutdown the computer in 30 seconds.

To suspend the computer:

#!/bin/bash

if [[ $USER == "eka" ]]; then                 # If the script is ran as "eka":
    gnome-screensaver-command -a                  # Lock computer.
else                                          # Else:
    sudo -u eka gnome-screensaver-command -a      # Once it finishes, exit gracefully.
fi                                            # End if.

Simplified version:

#!/bin/bash

[[ $USER != "eka" ]] && { sudo -u eka gnome-screensaver-command -a; exit 0; }

Very simplified version:

#!/bin/bash

sudo -u eka gnome-screensaver-command -a

Note: $0 is a variable that holds the full path to the script.

  • Great thanks it works but can you just explain the if condition part a little bit for more clarity – Eka May 22 '15 at 17:51
  • 1
    @Eka: Check now. – Helio May 22 '15 at 17:54
  • I think you should add an extra security check - make sure the user running the script is the user who wants to double click. system accounts shouldn't be able to access or use the script, IMO. – Thomas Ward May 22 '15 at 23:26
  • 1
    @Helio is it possible to relaunch script as a user eka instead of root because i am also using this script gnome-screensaver-command -a to lock the computer but using root we cant lock a computer. – Eka May 23 '15 at 7:47
  • @Eka: Try now. ;-) – Helio May 23 '15 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.