shell scripting, run in parallel processes - bash

#!/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

Related

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

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 Script Start a Server and return

I have a shell script that starts a server. I actually ssh into my server and run the shell script. As soon as it starts, it logs everything to the console and the console does not return. The problem starts when I close my Machine, the ssh connection is disconnected and the server that I started is shutdown. I guess I need to start the server and return from the shell. Here is what I have so far:
#!/bin/bash
java -Xmx1G -Dhttp.port=8080 -Dconfig.file=MyProject/conf/application.conf -cp ".:MyProject/lib/*" play.core.server.NettyServer .
exit 0
Any suggestions on how to return after calling this shell script?
After ssh to the server Just backgrounding your script (./myscript &) will not daemonize it. You must disconnect stdin, stdout, and stderr, and make it ignore the hangup signal (SIGHUP).
nohup ./myscript 0<&- &>/dev/null &
will do the job. Or, to capture all output:
nohup ./myscript 0<&- &> my.admin.log.file &
To avoid script termination on ssh session close use nohup (No hangup) with output redirection to a log file:
nohup bash /path/to/startScript.sh > script.log 2>&1 &
You can redirect stdout and stderr to files, background and disown the process (or nohup it) and then exit the script.
However, the correct way to do this is to use some kind of process manager daemon like upstart.

BASH script suspend/continue a process within script

In my bash script I am writing, I am trying to start a process (sleep) in the background and then suspend it. Finally, the process with be finished. For some reason through, when I send the kill command with the stop signal, it just keeps running as if it received nothing. I can do this from the command line, but the bash script is not working as intended.
sleep 15&
pid=$!
kill -s STOP $pid
jobs
kill -s CONT $pid
You can make it work by enabling 'monitor mode' in your script: set -m
Please see why-cant-i-use-job-control-in-a-bash-script for further information

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