0

I am new to shell scripting and I am not sure how to approach this problem. I have looked all over Google but I couldn't find a relative answer.

My problem: I have a shell script that executes two gnome terminals and then do some work. When the work is done I would like to close both gnome terminals that were opened at the beginning of the shell script.

#!/bin/sh

gnome-terminal -x sh -c "./manage.py runserver; bash"
gnome-terminal -x sh -c "yarn start; bash"

... 
Some other work
...

kill gnome-terminal 
kill gnome-terminal 
kill shell script

I have tried looking for the child processes id of the shell script and kill them but it did not work. Any help would be appreciated

Note: it needs to be done only with the default Linux packages since this is part of a project that many people use and I cannot enforce installation of different libraries.

tripleee
  • 175,061
  • 34
  • 275
  • 318
antpngl92
  • 494
  • 4
  • 12
  • 2
    use `tmux` or `screen` instead of a graphical applications – KamilCuk Apr 23 '21 at 07:56
  • I need to follow what is going on both servers while executing the "some other work" part – antpngl92 Apr 23 '21 at 07:57
  • 1
    `tmux` and `screen`are terminal multiplexers. It allow you to [split a single terminal window](https://www.perl.com/article/an-introduction-to-tmux/) instead of opening a new one – Aserre Apr 23 '21 at 08:01
  • Does this answer your question? [How to get process ID of background process?](https://stackoverflow.com/questions/1908610/how-to-get-process-id-of-background-process) – Aserre Apr 23 '21 at 08:01
  • Programmatically opening terminal windows is a bad habit. Have your scripts write their output to a log file, and separately open a new terminal to view that log file if you want to. – tripleee Apr 23 '21 at 10:41
  • @tripleee I have been given the green light to run the servers in the back without the need of ``gnome terminal``. So I did something like: ``./manage.py runserver > log1`` ``some other comands here`` ``yarn start > log2 `` However, in this example only log2 is ffilled with logs and in the main terminal I get the execution of first command ``./manage.py runserver`` – antpngl92 Apr 28 '21 at 11:43
  • 2
    Sounds like the first command is logging to standard error; the proper way to redirect is `command >log 2>&1` and of course add `&` to run in the background if desired – tripleee Apr 28 '21 at 11:49
  • I can see no way for `$!` to be empty if you have successfully started a background process. Even if `foo` is not a valid command, the shell will start a background process and report its PID in `$!` in every scenario I can think of. – tripleee Apr 28 '21 at 11:50
  • Anyway, your edit to piggyback in a new and basically unrelated question is invalid and I have rolled it back; please ask a separate question if you have two topics. – tripleee Apr 28 '21 at 11:51

2 Answers2

0

If you insist on using Gnome Terminal for this and not some sub-shell or multiplexer as suggested in comments, you can keep the PID of each executed terminal by saving the value of $! (last command's PID), and then kill them by PID:

#!/bin/sh

gnome-terminal -x sh -c "./manage.py runserver; bash" &
TERM1_PID=$!
gnome-terminal -x sh -c "yarn start; bash" &
TERM2_PID=$!

#... 
#Some other work
#...

kill $TERM1_PID 
kill $TERM2_PID 
shevron
  • 3,463
  • 2
  • 23
  • 35
  • This does not work for some reason. I get an output: # Use “-- ” to terminate the options and put the command line to execute after it. In addition, on echo-ing the TERM2_PID i see the variables are empty, there is no pid in them – antpngl92 Apr 23 '21 at 08:19
  • it does not woik for me in gnome-terminal buster( Just return another PID. – Alexy Khilaev Sep 02 '21 at 10:49
0

I have been given a green light to drop the usage of gnome terminal and have implemented @tripleee's suggestion:

python manage.py runserver > ../log 2>&1 & 

some other commands here 

yarn start:3030 > ../log2 2>&1 &

It seems to work and the output from each of the 2 commands it directed to log and log2 respectively.

For killing those 2 applications I am using:

fuser -k <app1 port number>/tcp
fuser -k <app2 port number>/tcp
antpngl92
  • 494
  • 4
  • 12