Open several terminals from shell script and then close them - shell

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.

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

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

Related

gnome-terminal -- $SHELL fails to execute the simplest commands

gnome-terminal -v -- $SHELL launches a new bash shell just as expected.
gnome-terminal -v -- $SHELL -c 'cd /' does nothing at all. A window briefly flashes, and that's it. Tried many other commands, like echo with the same result.
journalctl -f reveals Failed to start VTE child process 11350 launched by gnome-terminal-server process 7182. and that's it.
How do I debug this further?
this may not be the exact solution you need, but if you make a new profile for the terminal, and change the property "when command exits" to "hold terminal open"
then include the following argument:
gnome-terminal --window-with-profile=[new profile name here]
the new window will not briefly flash, and may allow more detailed debugging from whatever processes are called in the new window

Open Multiple XTerm Windows Simultaniously

I am working on a Raspberry Pi powered Magic Mirror project and to start the program I execute a shell script that runs in the background continuously. To make the AI part of my project work I need to open a second shell script in the background that also runs continuously. My problem occurs when I try to execute my Xterm commands it waits for the first script to complete before it starts the second script. Since both scripts have no designated end point I am stuck. Is there a way to make both Xterm commands execute at the same time?
Here is my current code to start the Xterm sessions:
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh"
xterm -e "cd ~/MMStartAll; ./MMStart.sh"
$SHELL
Your script should end with a &. This means that both the xterms will run in a sepperate process id (pid).
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh" &
xterm -e "cd ~/MMStartAll; ./MMStart.sh" &
$SHELL

Start background process from .bashrc

I realize that this might be a stupid issue but I can't solve it for hours even if digged stackoverflow site and google throughly.
Here is the base code in .bashrc to start gkrellm once I am logged into shell
if ps ax | grep -v grep | grep gkrellm > /dev/null
then
echo "gkrellm is already running"
else
command gkrellm &
fi
I already used to try
...
else
nohup gkrellm &
fi
...
and
...
else
gkrellm
$GK_PID=`pidof gkrellm`
disown -h $GK_PID
fi
...
gkrellm is properly put as background job and I can use shell (as expected). BUT I still have gkrellm exiting once I press Ctrl-c even if I start other apps from that same shell. How do I prevent gkrellm from closing on Ctrl-c press?
Just in case. I am using PuTTY clone called KiTTY but believe that's not it's issue.
Thanks for help!
Almost forgot about this issue and answering my own question after found a working solution long ago ;) Follow works great in my .bashrc for years
mygkrellm()
{
if pidof -x "gkrellm" >/dev/null; then
echo "Gkrellm is already running. Go to shell!"
else
nohup "/usr/bin/gkrellm" &
fi
}
Try replacing "nohup gkrellm &" with this:
screen -S gkrellm -d -U -m gkrellm
This will start a detached screen session running gkrellm, and it won't care about the current shell session. I'm not sure if starting it from .bashrc is the best solution though, it would make more sense to use your window manager's autostart features.
Edit: Not sure if I read the question correctly, are you using KiTTY to connect to a linux host and running gkrell remotely through X forwarding? If that is the case, you obviously can't use window manager features. :)
using bash (disown and &>/dev/null)
you need to run the app in the bg (gkrellm &) and then disown it
if ps -C gkrellm -o user | grep "$LOGNAME" &>/dev/null
then echo "gkrellm is already running"
else gkrellm & disown
fi
if you want to be more portable and use posix sh
you will need to use nohup (part of coreutils and POSIX)
and also background it (nohup cmd &)
you'd also use .profile instead of .bashrc
if ps -C gkrellm -o user | grep "$LOGNAME" 2>/dev/null 1>&2
then echo "gkrellm is already running"
else nohup gkrellm &
fi
other approaches would include - as #Pontus answered - using tools like dtach, screen or tmux, where the command is executed in a detached environment.
by Pontus:
it would make more sense to use your window manager's autostart features
indeed :) as afaik gkrellm is a GUI app, it's better to autostart it, either using .xinitrc (if your login manager supports it), or your window manager's autostart facilities.

Output PID to file from process executed in bash script?

I've got this simple bash script that starts a server process. I want to output the pid of the server process to a file, pid.txt. After some quick searching on SO, I came up with this approach, but it seems to give me the pid of the bash script, not the server process executed from the script. Note: the --fork is required for my server process to run as a daemon to output data to a separate log file, and I suspect that's causing the issue here based on this previous SO question, hoping there's a way around this.
#! /bin/bash
./mongo-linux64-202/mongod --fork &
pid=$!
printf "%s\n" "$pid" > pid.txt
Might I suggest:
#! /bin/bash
./mongo-linux64-202/mongod --pidfilepath ./pid.txt --fork &
derived from Mongo help:
mongod --help
./mongo-linux64-202/mongod --fork &
pid=$(jobs -p | tail -n 1)
Though look first whether mongod is willing to report its pid somehow.

SSH doesnt exit from command line

I ssh to another server and run a shell script like this nohup ./script.sh 1>/dev/null 2>&1 &
Then type exit to exit from the server. However it just hangs. The server is Solaris.
How can I exit properly without hanging??
Thanks.
I assume that this script is a long running one. In this case you need to detach the process from the terminal that you wish to close when you terminate your ssh session.
Actually you already done most of the work by reassigning both stdout and stderr to /dev/null, however you didn't do that for stdin.
I used the test case of:
ssh localhost
nohup sleep 10m &> /dev/null &
^D
# hangs
While
ssh localhost
nohup sleep 10m &> /dev/null < /dev/null &
^D
# exits
I second the recommendation to use the excellent gnu screen, that will do this service for you, among others.
Oh, and have you considered running the script directly and not within a shell? I.e.:
ssh user#host script.sh
If you're trying to leave a command running remotely after you close your SSH link, I strongly recommend you use screen and learn to detach the screen. That's much better than leaving background processes around; it also lets you reconnect and see what the process is up to.
Since you haven't provided us with script.sh, I don't think we can know for sure why the command is hanging.
You can use the command :
~.
This command close the ssh session.
sh -c ./script.sh &

Resources