Run shell script from launcher with root permissions

27,083

Solution 1

It seems that you haven't got this issue done yet, have you visited the "how to" link which I provided in the comment above? Click Me

If you check that you should get this problem solved in several ways, cool and nice. To be simple, I'd like to recommend a "dummy" way to make it works for you.

  1. Open your terminal and type sudo apt-get install gnome-panel
  2. When the installation is done, type gnome-desktop-item-edit --create-new ~/Desktop, this command will display a popup "Create launcher" dialog, shown as below enter image description here

    customize your settings(Name,Command,Icon etc.)

  3. After step 2, you will get a shortcut(it is actually named *.desktop, depends on your setting), feel free to put it in any folder you wish, then drag and drop it to the launcher.

That's it, enjoy your new baked shortcut :-), hope it helps.

Solution 2

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.

Solution 3

Making the terminal stay open afterwards is not a very smooth process, but here is what I suggest:

Make a new script with the following contents: gksudo /path/to/shell/script and save it to your desktop. Add the terminal to the launcher. To open the program, open the terminal from the launcher and clik'n'drag the script from the desktop to the terminal.

Or, if you don't necessarily need the terminal open after the program is finished, make a script as above, save it to any directory, and follow this guide to turn it into a launcher icon: http://ubuntuforums.org/showthread.php?t=1700605

I wish that process were a bit smoother, but hey, that's why I use Kubuntu...

Share:
27,083

Related videos on Youtube

Roronoa Zoro
Author by

Roronoa Zoro

Updated on September 18, 2022

Comments

  • Roronoa Zoro
    Roronoa Zoro over 1 year

    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.

    • vicd
      vicd almost 12 years
      you may create a shortcut in the launcher and with sudo ./ShellScriptFileName & to make it running in the background
    • Roronoa Zoro
      Roronoa Zoro almost 12 years
      How can I create the shortcut in the launcher?
    • vicd
      vicd almost 12 years
      @izx, nice answer though, but I'm afraid that he mentioned it's a pain with the terminal remains :P, he wants it's gone and silent.
    • vicd
      vicd almost 12 years
      let's make things easy, just see my potential answer for reference.
  • Roronoa Zoro
    Roronoa Zoro almost 12 years
    I apologize for the misunderstanding. I actually want the terminal to close. I was talking about the current method that I'm using, which keeps the terminal open...
  • Roronoa Zoro
    Roronoa Zoro almost 12 years
    I just tried to create a new script as you described (gksudo path/oldScript), I also changed its access and gave it execution permission (chmod +x newScript.sh), but I'm still having a problem. When I run the newScript, I type the password for the root, then my application starts then exits immediately. By the way, if I try to double-click oldScript, and I click on the option Run, I get the same result; my program starts then exits immediately, however, if I click the option Run in Terminal, it works fine...
  • Takkat
    Takkat almost 12 years
    gksu would be the right command to run a GUI app. Did you try to include this in your script?
  • Roronoa Zoro
    Roronoa Zoro almost 12 years
    @Takkat, I just tried gksu; same results. I'm wondering why my application works fine when I double-click the original script and choose Run in Terminal, and why it doesn't work when I double click the new script (gksudo or gksu /path/originalScript) and again, choose Run in Terminal... Running my application by double-clicking the original file won't be useful to me, first it's not root access, and second I need to add it to the launcher...
  • Roronoa Zoro
    Roronoa Zoro almost 12 years
    In case I wasn't clear in my last comment. My situation is the following: I created a new script gksudo path/oldScript, then I followed the guide to turn it into a .desktop file, but when I double-click on the .desktop file, I enter the password, then my application starts and immediately closes. I think I have a problem with the new script. Although I changed the access to execution permission (chmod +x newScript.sh), I'm still not able to run my application through the new script, whether I choose Run, or Run in Terminal. BTW Run in Terminal works on the old/original script.
  • Roronoa Zoro
    Roronoa Zoro almost 12 years
    Thank you but I still have some problems: 1) in the Type option, if I choose Application, the program runs then exits immediately, however, if I choose Application in Terminal, it works but the terminal opens and stays open - I'm not sure why this is happening, but if that's the only way to do it, I guess I'll just keep the terminal open :( 2) My second problem is that I don't think I'm in root mode when following your instructions, I can't save any files (through the application) when running it using your method
  • vicd
    vicd almost 12 years
    if you need to interact with your script, it's better to let the terminal remains, otherwise just let it running in the background(add the & at the end of your command) to get its tasks done. For the root permission, sudo your_command.