Ubuntu – Run shell script from launcher with root permissions

12.04command linepermissions

I have a shell script that runs a GUI application on Ubuntu 12.04.

The way I'm currently running the application is that I open the terminal and go to the directory that contains that file, then do sudo ./shellScriptFileName which runs just fine, however I have two problems, one is that this takes so much time so I would like to add a shortcut in the launcher, second is that the terminal needs to stay open, which is a pain (hopefully this won't be a problem after I'm able to run it from the launcher.

Clarification: I would like for the terminal to be closed when my application starts running.

Best Answer

Here are the steps:

1. Creating the interactive caller script

Since you want to execute the script and then have the terminal remain in an interactive mode, we need to use a special caller script, which in turn requires expect. We'll assume the script you want to execute is called test.sh

  • Install expect with sudo apt-get -y install expect
  • Create the caller script with this format:

    #!/usr/bin/expect -f 
    
    spawn -noecho bash           # create a shell
    expect "$ "                  # wait for prompt
    send "cd /path to script\n"  # your script directory
    send "./test.sh\n"           # your script name
    interact                     # switch to interactive mode
    
  • Make the caller script, say call_test.sh, executable with

    sudo chmod +x /path/to/call_test.sh

2. Creating the Launcher

  • Press Alt+F2, and type gksudo gedit /usr/share/applications/TestSH.desktop, where TestSH is whatever you want to call your launcher icon.
  • The file is of the following form:

    [Desktop Entry]
    Version=1.0          # your script version
    Type=Application
    Terminal=true        # you want a terminal, so...
    StartupNotify=true
    Icon=                # can be a built-in (/usr/share/icons)
                     # or a custom file, SVG or PNG
    Name=TestSH          # Whatever you want to call it
    Comment=           # description
    Exec=/path/to/call-test.sh # Path to caller script
    Categories=Application;
    
  • Finally, make sure the actual script is executable, with sudo chmod +x /path/to/test.sh

  • Close the editor, and search for "TestSH" (script name) in the HUD; you will see the icon, which you can click on, and also drag to the launcher or desktop to add it there.