35

I would like to know how to write the Exec command of a .desktop file to open a new terminal and execute a shell script in it. The shell script is working and accessible by all users. When launching the script from the terminal everything works, but it doesn't when trying to launch the script from a .desktop file.

Here are some combinations I have already tried:

Exec=gnome-terminal -x sh -c 'echo hello'
Exec=sh -c 'gnome-terminal echo hello'
Exec=sh -c 'echo hello'
Exec=echo hello

The .desktop terminal option is set to true.

41

The content of your desktop file should look like (see how to create a .desktop file using a text editor):

[Desktop Entry]
Version=1.0
Name=Test        
Comment=Test the terminal running a command inside it
Exec=gnome-terminal -e "bash -c 'echo hello;$SHELL'"
Icon=utilities-terminal
Terminal=false
Type=Application
Categories=Application;

Or:

[Desktop Entry]
Version=1.0
Name=Test        
Comment=Test the terminal running a command inside it
Exec=bash -c 'echo hello;$SHELL'
Icon=utilities-terminal
Terminal=true 
Type=Application
Categories=Application;

In the first case, the Terminal field is set to false (perhaps contrary to your expectations) and in second case is set to true, but the result is in both cases the same.

  • 1
    The first solution works for me, so I stick with it. Do you know why there has to be $SHELL? – Paradiesstaub Mar 24 '14 at 13:28
  • 1
    @Thibaut Yes, of course. – Radu Rădeanu Mar 24 '14 at 13:54
  • It worked for me , however i want to launch a command with sudo , then when use launcher created it opens terminal asking-me for password . How do i launch a command and prevent this behavior ? – E_Angel Dec 13 '16 at 13:04
  • 1
    @EliasAlves A bit late, but for anyone else reading this: you cannot expect to run commands that require root privileges without being required to authenticate yourself. (Unless you explicitly configure your system to do that, which is generally a bad idea.) – code_dredd Mar 29 '18 at 20:56
  • I think the question is: how to make sure the terminal appears so that the terminal doesn't disappear so that we can enter the credentials – RockScience Apr 8 '18 at 8:21
2
!#/bin/bash

gnome-terminal -e YOUR_COMMANDS

Make the above file. Don't forget to sudo chmod +x filename.sh

  • 1
    The problem with this solution is, that the terminal disappears immediately and a user is not capable to enter something. – Paradiesstaub Mar 20 '14 at 15:47
  • i forgot what it is called but I i think there is a option to keep the terminal up. It might be hold – Mr.Lee Mar 20 '14 at 18:37
1

Simply add

;$SHELL 

at the end of your commands.

Like for me snapd isn't something using full bandwidth of system to refresh snaps anytime almost I work at night.

So this worked for me to create a .sh file linked to a .desktop file.
Contents for .sh file were

echo <your password> | sudo -S systemctl stop snapd.service
sudo systemctl disable snapd.service;$SHELL

-S in the first line of the .sh file is used to send STDINPUT to the sudo command meaning direct execute without entering password.

Contents for the .desktop file were:

[Desktop Entry]
Version=1.0
Name=Test
Comment=Test the terminal running a command inside it
Exec=gnome-terminal -e "/scripts/disable_snap.sh"
Icon=terminal
Terminal=true
Type=Application
Categories=Application;
0

I do this. First line of the text file is !#/bin/bash

Subsequent lines of the text file are the commands (the shell script).

Save the file as something.sh

Open the properties of the file and enable run file as a program.

Now, when the file is double clicked, I get the option to run it.

  • 2
    Same here, the terminal disappears immediately. – Paradiesstaub Mar 20 '14 at 15:47
  • It works for me no problems. Create the shell script, add the line !#/bin/bash as the first line. Allow the file to be run as an executable. Double click and select run in terminal. It just works. – hatterman Mar 21 '14 at 21:21
  • 1
    Are you sure the terminal isn't just closing because the shell script has finished ? At the beginning of your script put sleep 30, does the terminal stay open for 30 seconds ? – hatterman Mar 23 '14 at 18:57

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.