1

What I would like to achieve is a bash script or even better a single bash line that can run two terminal based apps on parallel. I am aware of the commands & and ; but in my case they are not applicable because both my commands keep the terminal open and need each other to run properly. It might seem like an edge case but my specific use case is quite simple and I think it might be help full in many similar cases.

What I am trying to do is to parse a message from a usb port that uses swo protocol ,so my quite obnoxious workaround is :

  1. Open terminal one , run openocd -f connect_swo.cfg (terminal1 hangs )
  2. Open terminal two , run python3 swo_parser.py (terminal2 hangs and terminal1 prints values)
  3. Then terminating both commands with two separate ctrl+c signals

Expected solution would be something like:

  1. Run magic command that open two connected sessions and both my commands on the seperate sessions
  2. One single ctrl +c terminates both commands

PS:Comment me if I should move the question to superuser

  • "both my commands keep the terminal open" ... Do they really need the terminal? What happens if you do openocd -f connect_swo.cfg& python3 swo_parser.py? Does either command fail to work? – muru Mar 23 at 9:23
  • @muru Yes it fails as openocd needs the terminal to print the resaults and swo_parser need the terminal for error (To be honest I don't care about the second terminal and I could supress it with something like 2&>1 /dev/null but even then the first command's terminal closes to open the second command's, another workaround could be to change the order of the commands but then command one fails as there is no open usb connection and it autoterminates) – Spyros Mourelatos Mar 23 at 9:27
  • 1
    What about something like (sleep 5 ; python3 swo_parser.py) & openocd -f connect_swo.cfg? – muru Mar 23 at 9:30
  • @muru Yey ,This works perfect ,thanks,How can I accept a comment as an answer though? – Spyros Mourelatos Mar 23 at 9:32
  • Actually not that perfect ,now ctrl +c terminates only first app and second app is out of control (even with multiple ctrl c) on the vs codes terminal ( on the default it works) – Spyros Mourelatos Mar 23 at 9:38
1

With GNU Parallel you should be able to do this:

parallel --tty -j0 ::: 'openocd -f connect_swo.cfg' 'python3 swo_parser.py'

If GNU Parallel is not already installed look at: https://oletange.wordpress.com/2018/03/28/excuses-for-not-installing-gnu-parallel/

| improve this answer | |
  • All three solutions are fine but I will accept this one as it is the more elegant,It's the only one that closes both programs with one button and it gives me the choice to multithread (if I am correct and j2 means what I think it means).My only question is :parallel is not a default linux command ,when I typed this command my terminal complained that I should install moreutils or parallel ,which of the packages is more relevant for my job?(I guess the second by the name) – Spyros Mourelatos Mar 27 at 7:06
  • GNU Parallel is not part of moreutils. – Ole Tange Mar 27 at 8:08
  • Out of topic:That's a pitty they should package your parallel with moreutils ,from now on I will declare myself a fan of you <3 – Spyros Mourelatos Mar 27 at 8:26
  • 1
    If you look at the history, I actually tried to get that to happen in 2009: gnu.org/software/parallel/history.html – Ole Tange Mar 27 at 8:32
1

If openocd absolutely needs TTY connected to stdin, and the Python script needs to be run after openocd, you can reverse the order of execution by using sleep to delay the start of the script execution:

(sleep 5 ; python3 swo_parser.py) & openocd -f connect_swo.cfg

A single CtrlC will not terminate both, but there might be a way around that using traps. Or something like:

(sleep 5 ; python3 swo_parser.py) & openocd -f connect_swo.cfg
kill %1
| improve this answer | |
  • Nice with the kill %1 it even works in the nested terminal of vscode even though I don't have an idea what it does – Spyros Mourelatos Mar 23 at 9:50
  • It kills the first background job (%1). – muru Mar 23 at 9:52
1

Use a program like concurrently

| improve this answer | |

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.