Start a program in the terminal from a launcher

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Start a program in the terminal from a launcher

Post by Cannondale »

New to LM and running Cinnamon 21.
I have a python script that I would like to execute from a desktop launcher.
Is this possible or would I have to wrap the python call in a bash script and call the bash script from the launcher?
I have not been able to find the correct launcher syntax.
Last edited by LockBot on Tue Sep 19, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Start a program in the terminal from a launcher

Post by rene »

You can execute it directly as say /where/ever/script.py (from a launcher or otherwise) if you make it executable, i.e., if you have it start with e.g.

Code: Select all

#!/usr/bin/env python3
and do a chmod +x /where/ever/script.py. You can alternatively also execute it explicitly as python3 /where/ever/script.py (which is what the above ends up doing anyway); certainly never an intermediary bash script is needed.
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

Thanks for the information rene. I have the following in my launcher and nothing happens when double clicked.

Command:
/home/jer/rsync_bk2_menu.py

Note that the python script runs fine when executed from the terminal. What am I missing?
Attachments
Screenshot from 2023-03-19 11-08-14.png
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
billyswong
Level 8
Level 8
Posts: 2187
Joined: Wed Aug 14, 2019 1:02 am

Re: Start a program in the terminal from a launcher

Post by billyswong »

Does your python script exit immediately after finish running? If so, the terminal window can't persist and will exit together too if you run from launcher or double click the file icon. Add an input() to the end of your script if you want to keep the terminal output in view.

Method 2: Keep the script as is. But in Terminal, Edit > Preferences > Command > When command exits: > "Hold the terminal open"
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

I created a test python script called test.py with only an input statement.
Created a new launcher to launch test.py.
Launcher still does nothing when double clicked.
Attachments
Screenshot from 2023-03-19 11-51-12.png
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Start a program in the terminal from a launcher

Post by AndyMH »

Code: Select all

gnome-terminal -- /home/jer/test.py -q
man gnome-terminal for more info.
This assumes your script starts with the shebang described by rene and you have made it executable.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

Thanks Andy! That was the syntax!
man page link also very helpful!
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Start a program in the terminal from a launcher

Post by rene »

A somewhat better -- since it uses the configured terminal application rather than hard-coded gnome-terminal -- alternative to Andy's above reply is making sure the launcher has Terminal=true.

It seems that choice is in Cinnamon not provided via checkbox in the supposedly rclick->Properties screen you get in Cinnamon/Nemo you're posting screenshots of? Certainly it is in Xfce (in the "Launcher" tab of said properties) but I've noticed Cinnamon to be wonky in similar respect before. Even then though I am pretty sure at least that you get that option while creating the launcher, so I suppose you won't have missed it. What in any cased works is rclick->"Open With" a text editor and adding Terminal=true manually.
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

Thanks for the additional information rene!
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Start a program in the terminal from a launcher

Post by AndyMH »

Yes, you don't get that option with cinnamon, so you can either follow rene's suggestion (probably the simplest), or a route to the same answer and bit of background...

Everything you see on the desktop is a .desktop file in /home/you/Desktop, they are text files following a defined format, you can go there and edit the files. A sample entry (for a HP15C calculator emulator I manually installed):

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=HP15C
Exec=hp15c
Comment=
Terminal=false
Icon=hp15c.png
Type=Application
Name[en_GB]=HP15C
GenericName=HP15C
StartupNotify=false
Categories=Utility
The same is true for everything you see in the mint menu, they live in /usr/share/applications or $HOME/.local/share/applications.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Start a program in the terminal from a launcher

Post by rene »

Cinnamon/Nemo doesn't even when creating the launcher provide for setting a working directory, which has been an issue quite a few times as well. Seems that launcher interface should be overhauled, dragging it out of the GNOME "possibilities are confusing" morass.
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Start a program in the terminal from a launcher

Post by AndyMH »

rene wrote: Sun Mar 19, 2023 1:11 pm setting a working directory, which has been an issue quite a few times as well.
Out of interest, what is the setting in a .desktop file for that?
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Start a program in the terminal from a launcher

Post by rene »

Path=; viewtopic.php?t=392839 (by the way; if someone wonders why I'd say "quite a few times" if I'd seem unaware until that recent thread: it has in fact been quite a few times but I only later remembered; searching the forum yields many more of those threads).
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

AndyMH wrote: Sun Mar 19, 2023 1:00 pm Yes, you don't get that option with cinnamon, so you can either follow rene's suggestion (probably the simplest), or a route to the same answer and bit of background...

Everything you see on the desktop is a .desktop file in /home/you/Desktop, they are text files following a defined format, you can go there and edit the files. A sample entry (for a HP15C calculator emulator I manually installed):

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=HP15C
Exec=hp15c
Comment=
Terminal=false
Icon=hp15c.png
Type=Application
Name[en_GB]=HP15C
GenericName=HP15C
StartupNotify=false
Categories=Utility
The same is true for everything you see in the mint menu, they live in /usr/share/applications or $HOME/.local/share/applications.
Thanks Andy! This additional information provided the context I needed to get the big picture. I added Terminal=true to my particular launcher, as rene suggested, and removed "gnome-terminal --" from the launcher properties. Works great!

Thanks to both of you again!
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
mikeflan
Level 17
Level 17
Posts: 7106
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Start a program in the terminal from a launcher

Post by mikeflan »

FWIW I use crazy commands like this in a launcher to run my Perl programs:

Code: Select all

gnome-terminal -- bash -c "cd /home/mike/Documents/00MikeF && sleep 0.2 && /usr/bin/perl drgopen.pl && sleep 0.2 && exit; exec bash -i"
User avatar
Cannondale
Level 3
Level 3
Posts: 114
Joined: Mon Jan 02, 2023 11:27 am

Re: Start a program in the terminal from a launcher

Post by Cannondale »

You're a wizard Mike!
First computer: Radio Shack Computer TRS-80 Model 3. Zilog Z80 2.03 MHz CPU, 16k RAM, Two floppy drives, TRS-DOS (BASIC included)
Locked

Return to “Beginner Questions”