Bash - kill process when other process dies - bash

My bash script looks something like this:
#!/bin/bash
java -jar my_app.jar &
python -m my_module
How do I kill the python process when the java process dies?

Like this maybe:
#!/bin/bash
{ java -jar my_app.jar ; pkill -f my_module; } &
python -m my_module
You may want to further limit the selection of your Python process to be killed with additional switches.

here is a simple way
#!/bin/bash
python -m my_module &
pypid=$!
java -jar my_app.jar
kill $pypid

Related

Open several terminals from shell script and then close them

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

kill background process spawn in codeship

To kill background process inside Codeship we need to use following command:
#!/bin/bash
nohup bash -c "YOUR_COMMAND 2>&1 &"
The bash -c is needed in codeship but because this killing YOUR_COMMAND fails when storing it inside variable using PID_COMMAND=$!. It could be I am missing/doing something (wrong). But I have googled a lot and could not find correct answer so I hope you guys can help me.
FWIW:
I started a flask app with: flask run &
To kill the background process, I use pgrep & kill to find and terminate the flask instance:
kill $(pgrep -f flask)
Replace 'flask' with whatever name your command/program was

Start a process in background, do a task, then kill the process in the background

I have a script that looks like this:
pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
popd
The selenium servers needs to be running for the tests, however after the phpunit command finishes I'd like to kill the selenium-server that was running.
How can I do this?
You can probably save the PID of the process in a variable, then use the kill command to kill it.
pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
serverPID=$!
cd web/code/protected/tests/
phpunit functional/
kill $serverPID
popd
I haven't tested it myself, I'd like to write it on a comment, but not enough reputation yet :)
When the script is excecuted a new shell instance is created. Which means that the jobs in the new script would not list any jobs running in the parent shell.
Since the selenium-server server is the only background process that is created in the new script it can be killed using
#The first job
kill %1
Or
#The last job Same as the first one
kill %-
As long as you don't launch any other process in the background - which you don't - you can use $! directly:
pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
kill $!
popd

Start Selenium RC server automatically

How can I automatically start and stop the Selenium RC server when I run my phpunit tests?
I thought I could create a little bash script that does something like this (does not work though):
java -jar ~/bin/selenium-server-standalone-2.0b3.jar &
phpunit --configuration suite.xml &&
killall java
Surely there's a way to do that right? To make the first line run in the background, and the second block execution until completion.
Or is there another good way of doing this? Does phpunit have a facility for running a process first?
I feel like I need to completely automate this because if I forget to start the server, phpunit doesn't even throw any errors, it just skips the tests!
Are you want to run shell script, java or php code ?
php code : exec("/path to file/script.sh");
java code : Process p = Runtime.getRuntime().exec(/path to file/script.sh);
same for bat file.
and that script contains the command launching of selenium server or directly execute the command for launching server. Please be clear about your question and in which language??????
(Just for fun)
TMPFILE=`mktemp`
SELENIUMJAR=~/bin/selenium-server-standalone-2.0b3.jar
bash -c "echo $$ && java -jar '${SELENIUMJAR}'" > "$TMPFILE" &
sleep 0.1
pid=`head -1 < "$TMPFILE"`
phpunit --configuration suite.xml
kill "$pid" ; sleep 2
kill -9 "$pid" ; sleep 0.1
rm "$TMPFILE"

Can this script wait for an external process to complete?

I have written this shell script as wrapper to a JAR file. The script launches the JAR without problem but completes without waiting for the JAR to finish its job.
#!/bin/bash
export WORK=/opt/conversion
export LOG=$WORK
export XMAIL=me#email.com
export JAVA_BASE=/usr/java/jdk1.6.0_06
export JAVA_HOME=/usr/java/jdk1.6.0_06/bin
export JAR=$WORK/conversion.jar
export CLASSPATH=$JAVA_BASE/lib/tools.jar
export CLASSPATH=$CLASSPATH:$WORK/lib/ojdbc14.jar
export CLASSPATH=$CLASSPATH:$JAR
$JAVA_HOME/java -Xms256M -Xmx512M -classpath $CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &
echo $! > $WORK/job.pid
mail -s "Conversion" $XMAIL < $WORK/user_message
exit 0
Is there a way to have the script wait on my JAR file to complete?
Thanks for your input.
As others have said, remove the & and bash will wait till the command finishes. If you insist on running your process in the background, here is what you could do:
pid=`pidof java`
if [ "$pid" ]; then
while kill -0 "$pid"; do
sleep 1
done
fi
The kill command is normally used to send signals to a particular process, but by using 0 as a signal you are only checking whether a process with such a pid exists without sending a signal.
You have a & at the end of the command:
$JAVA_HOME/java -Xms256M -Xmx512M -classpath $CLASSPATH com.myapp.cam.conversion >>$WORK/job.out 2>&1 &
which makes it run in background.
Remove the & to wait for the java process to complete before you proceed in the script.
If you want to run it in the background, add a wait command at the end of the script.
Don't run the conversion Java application in the background. Or, run ps repeatedly until you don't see the pid. This will allow you to do stuff while waiting.

Resources