0

I created a scriptA.sh on the desktop and make it executable then this was it content:

#!/bin/bash
sudo -u userA bash -c 'PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts'

I want to double click on this to open terminal and run the two commands in it and keep terminal opened. But what actually happens after double clicking it and choosing to run with terminal option, the terminal opens for a second and then closes itself. I don't know why this is happening, so I'd appreciate if anyone could please advise why this is happening and how to overcome it.

3
  • Are you logged in as 'userA'?
    – Dan
    Sep 23, 2014 at 2:22
  • you mean through the os or terminal? I'm logged in to the system using userA and by default when I open a terminal its logged in using userA as well.
    – Tak
    Sep 23, 2014 at 2:24
  • Then sudo -u userA bash -c seems unnessecary.
    – Dan
    Sep 23, 2014 at 10:48

2 Answers 2

2

You probably want to use gksudo if you want to start it from your Desktop. That will open a window asking you for your password.

#!/bin/bash
gksudo -u userA bash -c 'PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts'

Also to debug, you probably want to run that script in your terminal first. That way it will not close just after printing some error messages.


Update

One way to do that is to create a file as follow:

/home/user_name/.gnome2/panel2.d/default/launchers/script_name.desktop

Then put something like this in it:

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=gdu-category-peripheral
Name[en_US]=Receiving fax
Exec=gksu /usr/bin/gnome-terminal
Comment[en_US]=Some description...
Name=My Script
Comment=Start my script
Icon=gnome-panel-launcher

That works great for me, in general. The gnome-terminal accepts parameters so you can change various settings.

Use --working-directory=... instead of your cd ... command. To get specific variables, you probably want to use the --profile=... option.

Exec=gksu -u userA /usr/bin/gnome-terminal --profile=/home/userA/profile --working-directory=/home/userA/scripts

Then create the profile script for the variable:

# In /home/userA/profile
PACKAGE_PATH=/home/userA/package1

Obviously you could add any other alias or anything else you need in the profile script.

6
  • I've tried gksudo but still same thing happening. the script PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts is working find if I manually opened a terminal and run it.
    – Tak
    Sep 23, 2014 at 2:16
  • I'm not too sure what you are trying to do actually. That script doesn't do anything unless started in a terminal. If your point is to open a terminal in a specific folder and as root (sudo user), then you need to start a terminal and run that script in it. Sep 23, 2014 at 2:23
  • I want to double click on this to open terminal and run the two commands in it and keep terminal opened. (question edited)
    – Tak
    Sep 23, 2014 at 2:27
  • Okay, try the .desktop file. Before you could create those using a GUI interface, but in the latest versions of Ubuntu the GUI fails to work right so I have had to edit the file with a text editor. The result is the same though, once done, a double click works as expected. Sep 23, 2014 at 2:36
  • I can't find this location /home/user_name/.gnome2/panel2.d/default/launchers/ I can find only this /home/user_name/.gnome2/ I can't find panel2.d I find keyrings and nautilus-scripts
    – Tak
    Sep 23, 2014 at 2:42
2

By default, when you choose the option to "run in a terminal window", the (gnome-) terminal window closes after it is finished. It does open, since you mention it opens for a second. You can make it stay open by changing the setting it in your terminal profile:

From the (gnome-terminal) menu, choose "Edit" > "Profile Settings" > "Title and Command". In the option "When command exits" choose "Hold the terminal open".

That should work.

enter image description here

1
  • That's a good one to know about. Probably a lot easier to do than my solution of using a .desktop file. Sep 24, 2014 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .