How to Install Selenium as a Unix daemon? - java

How to put the following commands to run in the background as a service on Ubuntu?
Start the hub
java -jar selenium-server-standalone-2.48.2.jar -role hub &
Start the nodes
java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://localhost:4444/grid/register &
Whenever I close my ssh session can not access the selenium grid service even putting '&' character at the end of each command. Would someone give me a help?

I've tried to make selenium-server-standalone running as a service, but it failed to launch browser (I've tried chrome and firefox).
So it is better to do as Mahsum Akbas says.
Here is an example of how you could make it as a service:
bash - Start Java jar by service (linux)...
But it will not launch real browsers.
I was using jenkins service to launch real browser, but it failed too.
I had success in launching tests using headless browser. But there was the problem with some tests were failing.
And also, you could try this
EDITED: I've achieved it in such way using systemd:
sudo vim /etc/systemd/system/selenium-server-hub.service
[Unit]
Description=Selenium Server Standalone hub
StartLimitIntervalSec=5
After=syslog.target
[Service]
Type=simple
Restart=always
RestartSec=8
User=spacer
ExecStart=/bin/bash -c "export DISPLAY=:10 && /usr/bin/java -jar /home/spacer/seleniumserver/selenium-server.jar -role hub"
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
sudo vim /etc/systemd/system/selenium-server-hub.service
[Unit]
Description=Selenium Server node
StartLimitIntervalSec=0
After=selenium-server-hub.target
[Service]
Type=simple
Restart=always
RestartSec=8
User=spacer
ExecStart=/bin/bash -c "export DISPLAY=:10 && /usr/bin/java -Dwebdriver.chrome.driver=/bin/chromedriver -jar /home/spacer/seleniumserver/selenium-server.jar -role node -hub 'http://192.168.0.101:4444/grid/register/'"
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
I connect to my linux server through RDP and it opens display :10. Your display could be different.
You could check your displays by command:
ps e | grep -Po " DISPLAY=[\.0-9A-Za-z:]* " | sort -u
PS: Chrome and Firefox are starting, even though chromedriver could not be started when I was launching selenium-server hub and node from terminal as usual.

you can use nohup command. so, you can redirect output to nohup file and there will not be kill session after disconnect ssh.
nohup java -jar selenium-server-standalone-2.48.2.jar -role hub &
nohup java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://localhost:4444/grid/register &

Related

Can't start tomcat as service in redhat

I'm new to Linux but having spent a whole day I Installed Java and Tomcat. My goal is to host an App with this Linux box. I know it all works fine from my windows based machine, but it is my laptop so I'm planning to use the Linux Box as my dedicated server.
I am following this tutorial . From this tutorial I have executed the following command :
cd /etc/init.d
vi tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jdk1.7.0_05
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
chmod 755 tomcat
chkconfig --add tomcat
chkconfig --level 234 tomcat on
chkconfig --list tomcat
service tomcat start
After this command , tomcat is started at port 8082 . But when I restart pc , the tomcat is not started with boot of PC .
How can I do this ?
Since you use Red Hat you can use systemd for services.
Create a file /etc/systemd/system/tomcat.service:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/java/jdk1.7.0_05
Environment=CATALINA_PID=/usr/share/apache-tomcat-7.0.29/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
Environment=CATALINA_BASE=/usr/share/apache-tomcat-7.0.29
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Duser.timezone=UTC -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/share/apache-tomcat-7.0.29/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
I specified the script to start after syslog and network are enabled.
As we can see systemd handles the tomcat as a daemon and kills the PID.
With User and Group we specify the user and the group that the process should be run as.
Systemd will handle the upstart process and kill it using the PID.
To enable it to run then issue:
systemctl enable tomcat
systemctl start tomcat
Try to use this command instead:
sudo systemctl enable tomcat

Ansible terminates my app with "nohup"

I have a task in Ansible playbook that executes a .jar file in the background, but after finishing that task, the (java) app terminates.
- name: Run Java app in the background
shell: nohup java -jar app.jar &
I need the app running for tasks further down in the playbook. Any ideas??
NOTE: When I run it in Putty ssh session it runs smoothly and the app stays in background.
The most likely reason is attached IO. Try:
- name: Run Java app in the background
shell: nohup java -jar app.jar </dev/null >/dev/null 2>&1 &
Ok So I had this issue today and my script is a tad different but essentially doing the same thing so this is what I have, running on Oracle, Centos and RHEL.
in script start_service.sh I have this, this script is executed not sourced.
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
nohup java -jar ${DIR}/$1 --spring.config.location=${DIR}/application.yml --logging.config=${DIR}/logging.xml > ${DIR}/log/output.log 2>&1 &
In my playbook I have:
- name: run service
shell: "{{ service_install_location }}/application_name/start-service.sh application_name-{{ application_version }}.jar"
args:
chdir: "{{ service_install_location }}/application_name"
executable: /bin/bash
Now, the BASH_SOURCE argument is specific to bash so if you are running plain old sh then you need $0. nohup is required so when the ssh connection terminates your background task does not get killed (turn on -vvv for ansible to see connections being made to understand how this happens on a remote machine).
I should point out we are doing this temporarily as this should really be installed as a service to be under the control of service/init.d but for now, this answers your question

can i use selenium grid on single machine

I don't have multiple machines at my job. I have one window and one mac for script execution. I was wondering if i can use selenium grid for script execution on single machine.I never used selenium grid. Any article, links or suggestion is highly appreciated.
Yes, you can start a selenium gird with multiple node on single machine, but RAM should be at least 8GB because it will running test suites with more than 4 browser instance it need more RAM if not browser gets closed.
http://selenium-release.storage.googleapis.com/index.html Download standalone jar.
java -jar selenium-server-standalone-2.45.0.jar -role hub
It will start hub .
To start nodes open different cmd and type following command to start 'n' number nodes. The command is below:
java -jar lib/selenium-server-standalone-2.43.1.jar -role node -hub http://localhost:4444/grid/register -port 5555
java -jar lib/selenium-server-standalone-2.43.1.jar -role node -hub http://localhost:4444/grid/register -port 6666
java -jar lib/selenium-server-standalone-2.43.1.jar -role node -hub http://localhost:4444/grid/register -port 7777
If you want to run same test case in different browser download the browser drivers here
Run the following command to start different browsers:
For example:
java -jar selenium-server-standalone-2.45.0.jar -role webdriver -hub http://localhost:4444/grid/register -Dwebdriver.chrome.driver=C:\Users\xyz\Desktop\chromedriver.exe
java -jar selenium-server-standalone-2.45.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 6666 -Dwebdriver.chrome.driver=C:\Users\xyz\Desktop\chromedriver.exe.
This will start chrome browser and node.
Yes you can use set up Selenium Grid on a single machine. You can download the jar file from this download link
After download, start the hub with the following command:
java -jar selenium-server-standalone-2.46.0.jar -role hub
Then register nodes to it with the following command:
java -jar selenium-server-standalone-2.46.0.jar -role node -hub http://localhost:4444/grid/register
Refer the following link for more information, the example there is for the single machine scenario with Ruby but it is similar in java.
http://elementalselenium.com/tips/52-grid
You can also run a grid locally using Docker. Selenium provide images for a hub, Chrome and Firefox on Ubuntu.
You can but not sure why would you. If you want to execute in a single machine you can just go ahead and create multiple instances of the web driver for different browsers and achieve that. IMHO the whole purpose of grid is to distribute the load across nodes with different browsers,OS etc ..
But to answer your question yes you can. You can run the hub and node in the same machine and test it out if that's what you want to do.

Selenium hub and node issue

I am newbie to selenium. I am trying to run some test cases, for this I tried to start selenium hub and node..Following is batch file to start hub and node.
set path=%path%;C:\SeleniumConfig;C:\Program Files (x86)\Java\jdk1.7.0_21\bin;C:\Program Files (x86)\Java\jre1.6.0_45\lib;
set webdriver.ie.driver=C:\SeleniumConfig\IEDriverServer.exe
start java -jar selenium-server-standalone-2.42.2.jar -role hub -hubConfig config-hub.json
start java -jar selenium-server-standalone-2.42.2.jar -role node -nodeConfig config-node.json -hub http://localhost:4444/grid/register
When I tried to run hub and node using following command , I dont see any issue on command prompt. but dont see anythings is running on http://MyHost:4444/
"start java -jar selenium-server-standalone-2.42.2.jar -role hub -hubConfig config-hub.json"
With the above command some window opens and close immediately.
Please advice
selenium-server-standalone-2.42.2.jar file must be at same path . Or you can give jar file path. e.g. start java -jar C:/selenium-server-standalone-2.42.2.jar -role ......

Execute Selenium at the startup

I'm using Selenium to automate a browser in a Server from a client, but I want that the server execute selenium automatically at the startup.
I have 3 files in /etc/init:
proxyserver.conf:
respawn
start on runlevel [23]
script
exec java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111
end script
proxyserver2.conf and proxyserver3.conf that are the same thing and change only the content of "script":
exec java -Dwebdriver.chrome.driver=/home/marco/selenium-client/chromedriver -jar selenium-server-standalone-2.20.0.jar -role node -port 2222 -hub http://192.168.1.12:1111 -browserName=chrome,maxInstances=5
If I execute this commands at the startup with this method, when I execute Selenium on the Client, it give me this error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
But if I execute in the terminal the same commands that I use in /etc/init, all works perfectly... why?!
One last thing, if I execute:
ps aux | grep selenium
when the server the scripts at the startup it gives me:
root 746 0.0 12.1 677080 124468 ? Ssl Apr23 8:10 java -Dwebdriver...
root 749 0.0 12.7 685552 130280 ? Ssl Apr23 8.09 java -Dwebdriver...
root 755 0.0 1.9 680168 20240 ? Ssl Apr23 8.08 java -jar selenium...
when I execute in the terminal it gives me:
1000 9764 6.6 3.0 679236 30992 pts/0 Sl+ 10.33 0:01 java -jar...
1000 9783 14.0 3.0 677112 31752 pts/1 Sl+ 10.33 0:01 java -Dwebdriver...
1000 9792 12.6 3.0 675472 30944 pts/2 Sl+ 10.34 0:01 java -Dwebdriver...
Why it can't works?
thanks!!
I have seen this error when the chromedriver path is wrong. If you see the RC console it should have the chromedriver not found error message.
The chromedriver environment property should be passed as parameter to selenium jar file.
exec java -Dwebdriver.chrome.driver=/home/marco/selenium-client/chromedriver -jar selenium-server-standalone-2.20.0.jar -role node -port 2222 -hub http://192.168.1.12:1111 -browserName=chrome,maxInstances=5
should be changed to
exec java -jar selenium-server-standalone-2.20.0.jar -role node -port 2222 -hub http://192.168.1.12:1111 -browserName=chrome,maxInstances=5 -Dwebdriver.chrome.driver=/home/marco/selenium-client/chromedriver

Categories