Start Selenium RC server automatically - bash

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"

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

pkill -n java works at console but not in a script

I'm trying to write a script that runs a java program "Loop", and then after 5 seconds, terminates that java program, however, it's not terminating the program when I use the "pkill" option, I'm sorry for asking such a basic question, and I have looked around the internet, but I can't find a solution. Here is my code:
#!/bin/bash
javac Loop.java
java Loop
sleep 5
pkill -n java
When I run the command pkill -n java from the terminal, as opposed to in a script, it does as I expected, why is this?
Your bash script is waiting for java to complete so you'll need to run it as a background process which will start your Loop code and then return immediately, allowing the rest of your script to run:
java Loop &
More information: http://tldp.org/LDP/abs/html/x9644.html
Since you run java Loop in foreground, the next line sleep 5 doesn't get executed until your JVM exits (which is probably never, if Loop is actually an infinite loop).
So you need to start that in background:
java Loop &
Also, for killing that specific background job (rather than killing the newest JVM), you can do:
kill $!

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

Run SSH command nohup then exit from server via Jenkins

So I've tried googling and reading a few questions on here as well as elsewhere and I can't seem to find an answer.
I'm using Jenkins and executing a shell script to scp a .jar file to a server and then sshing in, running the build, and then exiting out of the server. However, I cannot get out of that server for the life of me. This is what I'm running, minus the sensitive information:
ssh root#x.x.x.x 'killall -9 java; nohup java -jar /root/project.jar -prod &; exit'
I've tried doing && exit, exit;, but none of it will get me out of the server and jenkins just spins for ever. So the Jenkins build never actually finishes.
Any help would be sweet! I appreciate it.
So I just took off the exit and ran a ssh -f root#x.x.x.x before the command and it worked. The -f just runs the ssh command in the background so Jenkins isn't sitting around waiting.
Usual way of starting a command and sending it to background is nohup command &
Try this. This is working for me. Read the source for more information.
nohup some-background-task &> /dev/null # No space between & and > !
Example :
ssh root#x.x.x.x 'killall -9 java; nohup java -jar /root/project.jar -prod &> /dev/null'
no need exit keyword
source : https://blog.jakubholy.net/2015/02/17/fix-shell-script-run-via-ssh-hanging-jenkins/

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