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

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

Related

Terminal doesn't close after process kill

I run a Spring Boot app with the following command,
java -jar myapp-1.0.0.jar & echo $! > "myapp.pid"
and killing the process with the following command
kill `cat "myapp.pid"` or kill -9 `cat "myapp.pid"`
and when I checked process is killed but terminal doesn't return, it becomes hanged. When I press a key, it returns normal. What is the problem here ?
As you are running it in background and executing one more command echo it is not coming back to $ prompt actually. Just try like below it will work:-
java -jar myapp-1.0.0.jar
And kill the process from different terminal
kill `cat "myapp.pid"` or kill -9 `cat "myapp.pid"`

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

shell script running infinitely

i have a shell script run.sh.
cd elasticsearch-1.1.0/
./bin/elasticsearch
cd
cd RBlogs/DataFetcher/
mvn clean install assembly:single;
cd target/
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Here if second line(./bin/elasticsearch) executes it runs infinite time, so the next lines will not execute. So what i need is to perform the next lines after 10 seconds. But
cd elasticsearch-1.1.0/
./bin/elasticsearch
sleep 10
cd
cd RBlogs/DataFetcher/
mvn clean install assembly:single;
cd target/
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar
This also will not execute next lines because ./bin/elasticsearch will not complete its execution in 10seconds. So how can i solve this problem? Please help.
Adding & at the end of ./bin/elasticsearch will cause the process to run in a subshell, freeing the current shell up for the next commands.
./bin/elasticsearch &
Change this in your second version of the script and things should run like you want them too.
More information can be found from man bash
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell.
The shell does not wait for the command to finish, and the return status is 0.
you may try to put it in background
& can do this for you
./bin/elasticsearch &
If you simply want elasticsearch to run in the background while the rest of the script progresses, just use &:
cd elasticsearch-1.1.0/
./bin/elasticsearch &
sleep 10
cd
cd RBlogs/DataFetcher/
However, if you want to run elasticsearch for at most 10 seconds, killing it if necessary, then proceeding with the rest of the script, you need something a little more complicated:
cd elasticsearch-1.1.0/
./bin/elasticsearch &
pid=$!
sleep 10
kill -0 $pid && kill $pid
cd
cd RBlogs/DataFetcher/
As other answers mentioned, you can
use ./bin/elasticsearch & to run the command in
background.
record the process id of the
command run in background by using child_pid=$! and then stop the
process by using kill $child_pid after some time to implement a
timeout mechanism.
Meanwhile, you also can synchronize another operation with the command run in background by using wait command. An example bellow:
./bin/elasticsearch &
# do something asynchronously here
wait # wait for accomplishment of ./bin/elasticsearch
# do something synchronously here

shell scripting, run in parallel processes

#!/bin/ksh
##########################################################################
$JAVA_HOME/bin/java -jar SocketListener.jar 8182
run_something_else
exit 0
SocketListener is started, and shell is waiting while SocketListener don't die.
How can I run run_something_else and SocketListener at the same time
$JAVA_HOME/bin/java -jar SocketListener.jar 8182 &
add an ampersand(&) at the end.this will give control of the terminal to the next line and makes your SocketListener run in the background.
nohup can be used to run the process in the background as daemon.
nohup runsomethingelse &
You could background something else:
nohup run_something_else &
Nohup will guarantee that sumething_else will run even if your terminal closes. So it will make it ignore sighup

Why is this command in the bash script not running in the background?

I have a bash script that includes the following lines:
.
.
if [ -z "$(pgrep mplayer)" ]; then
/usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe http:/www.musicserveraddress.com/ &
fi
.
.
Other things to execute
.
exit
what happens is that mplayer connects to the streamingserver and start playing the stream. However, the script never moves on. I added an ampersand to move this process to the background so that the script should continue to run and then exit itself (keeping the audio stream playing).
How should I do to achieve that?
Thanks in advance/J
Edit: It runs as planned when I run the script from the command line, but it is intended to be run as a cron job (and the pgrep is intended to start mplayer only if it has crashed since last cron job). When run as a cron job, nothing happens...
Try with a nohup at the beginning of the command :
nohup /usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe http:/www.musicserveraddress.com/ &
The command you've used works perfectly fine for me. The only thing that can confuse people is that you don't get the bash prompt when the script finishes, but it's actually there, try pressing Enter.
As you want the player to be controlled via a pipe and be in the background, I'd recommend to redirect standard streams from the console as well and send all the output to a logfile:
(
exec </dev/null
exec >/dev/null
exec 2>/dev/null
umask 0
cd /
exec setsid mplayer -slave -idle -input /home/user/control.pipe http://server.com > /var/log/mplayer.log 2>&1
) &
You may want to use setsid as well. That worked for me.

Resources