Open mate-terminal and launch python program

  • Raspberry Pi4
  • Ubuntu Mate 20.10(fully up to date)

I need to launch 10-12 python programs each in their own terminal. I been trying to find a way(other than manually opening a terminal and typing the command) to launch these. I've seen this thread(among many others) but it still kicks out errors.

https://ubuntu-mate.community/t/open-mate-terminal-and-do-command/11200

Based on the previous thread I've tried:

mate-terminal -e /home/tester/python/test1.sh

The test1.sh shell file contains:
#! /bin/bash
'cd /home/tester/python'
bash

This is the error screen

The shell file properties are set to allow execution. What am I missing?

Thanks.

Are you sure the file /home/tester/python/test1.sh actually exists? I'd hate to say it, but on Linux all filenames are case-sensitive.

Also, a particularly infuriating (but uncommon) problem is where the line endings on the shell script are not a plain linefeed like UNIX systems expect; sometimes, when you save the file wrong or when you create the file on a Windows system, the lines of the shell script will end with a carriage return followed by a linefeed. The kernel, which reads the first line in the shell script (#! /bin/bash) gets confused by the carriage return and attempts to run a shell called /bin/bash[carriage return]. Try re-saving the file with UNIX line endings and see if that works.

Thanks Gordon,
The suggestion on the line ending was correct. I have the Pluma text editor using Windows line endings because I share text files between the server and my Windows machine. So I re-saved the test1.sh with 'Unix/Linux' line endings and I can now open a shell window. The bad news is that when the terminal opens I get the following error:

/home/tester/python/test1.sh: line 2: cd /home/tester/python/: No such file or directory

I've played around with the script and it will run commands like 'pwd' or 'ls', but the 'cd' command seems to crash and burn(it does not seem to matter where I cd to.) Currently the test1.sh file is:

#! /bin/bash
'cd /home/tester/python/'
bash

The other weird thing is when I try to exit the newly created terminal window(using the close window in the upper right corner) it has a pop up that asks if I wish to close the terminal. If I select close terminal, every shell window currently running on the desktop closes.

As for the cd command: Do not surround the command in quotes.

The reason: The actual command is called cd; the parameter to that command is /home/tester/python/. By putting the whole command line in quotes, you're basically telling the shell to run a program named cd /home/tester/python/, which obviously doesn't exist; only a program called cd exists, and that program can accept /home/tester/python/ as a parameter. (I hope I made that clear enough.) So to summarize: Delete the quotes.

As for the unexpected popup: When you go to close a terminal window or tab, the MATE Terminal is smart enough to check if the program you're running in the terminal (usually a shell, though in this case a shell interpreting your script) has executed any currently-running processes. That means that if you close a terminal window with just bash siting there doing nothing, waiting for your input, the terminal program will not prompt you before closing the window; on the other hand, if you're running another program in the shell; the terminal will prompt you before closing the terminal window. (This test is done since there's always the possibility of messing something up if you abruptly close the terminal and kill running commands; if you were running apt dist-upgrade to upgrade your Ubuntu MATE system, would you be a happy camper if you interrupted the upgrade by accidentally clicking on the little 'X' button on the terminal window? Chances are, no.) So what second program are you running in the terminal? Answer: A second copy of bash, like line 3 of your script says.

Short answer: If you want to avoid the unexpected behavior of the terminal prompting you before closing the window, prepend an exec command to the last command in the script, in this case bash. (Presumably, you'll eventually change the bash at the end to something like python script1.py, but prepending an exec to that will work too.) The exec replaces the shell that is running the active shell script with a program of your choosing. Because the shell running the active shell script gets replaced, the terminal doesn't notice any "child" processes and assumes it's OK to close the window without prompting.

To summarize, your script should look like this after following all of my directions:

#! /bin/bash
cd /home/tester/python/
exec bash

Side note: If this is supposed to be a "wrapper script" and you want to pass all parameters to the wrapper script on to the last command in the script (the "wrapped" command), you can change the last line to read:

exec bash $@

I wish you luck on this script.

Weird that the threads I used as a reference for this .sh file used quotes. So removing the quotes fixed the last issue with running the python script from the .sh file. Adding the 'exec bash' to the end of the shell script does remove the warning but when the terminal closes it still closes the original terminal that was used to launch the mate-terminal in the first place.
This behavior seems so different from using Cntrl-Alt-T to open a terminal and execute a python program. Why would closing the launched terminal not only close that one but the original as well? Is this some parent child relationship in mate-terminal where the child also kills off the parent?

So let me get this clear: You open an ordinary terminal with a shell prompt, and execute your script using mate-terminal -e /home/tester/python/test1.sh, which runs; then you close the terminal window that contains the shell script, and doing so also closes the terminal where you ran mate-terminal -e /home/tester/python/test1.sh?

Weird. I'm not sure there's an explanation for that, unless you actually run exec mate-terminal -e /home/tester/python/test1.sh.

For clarity:

  • Open new terminal using Cntr-Alt-T.
  • Run mate-terminal -e /home/tester/python/test1.sh
  • You now have two terminals open with one running a python script from the python script there is a tk window showing.
  • Close the tk window.
  • The new terminal now sits at a command prompt.
  • Close the new terminal by clicking on the upper right close icon of the terminal frame.
  • The new terminal closes as well as the original terminal which was opened using Cntrl-Alt-T.

In fact if you have any other terminals running via Cntrl-Alt-T, they will all close(I had nine open running python scripts for the server.) Sometimes I end up with this:

Maybe I'll try and dig around the logs and see if there is some type of system error message occurring. But as of this morning I'm able to launch all the needed python scripts from a single paste into a open terminal. Thank you for all the help.

For the future: If you're going to post the crash window, please at least wait the 5-10 seconds for the spinner in the middle of the window to settle down; after the spinner disappears, all manner of debugging information appears in the window that could be very useful.

For the record, there should be at least one file whose name contains the string mate-terminal in the directory /var/crash. This log file is a gold mine for useful debugging information, to someone who knows what to look for (like me, not to sound narcissistic). Here's the deal: If you upload the log file so that I can see it, I'll see if I can fix the problem.

Finally, if all terminal windows come crashing down because one was closed, it's clear that there's a terminal bug. It's unlikely to be your fault in any way, shape or form.

Thanks in advance.

Unfortunately that screenshot was taken after about a minute of waiting. I've tried uploading the 3.5Meg file as a zip but the system is not happy. Do I need to be a uber-user?

ls -l /var/crash
total 27020
-rw-r----- 1 tester whoopsie 24150425 May 4 16:56 _usr_bin_caja.1001.crash
-rw-r----- 1 tester whoopsie 3507244 May 6 08:37 _usr_bin_mate-terminal.1001.crash
-rw-rw-r-- 1 tester whoopsie 0 May 6 08:37 _usr_bin_mate-terminal.1001.upload
-rw------- 1 whoopsie whoopsie 37 May 6 08:37 _usr_bin_mate-terminal.1001.uploaded

Thanks again Gordon for the advice/guidance.

upload_error_1