How to check a geckodriver is running on a remote machine? - java

I am writing automatic tests using Selenium / Maven / testng.
Tests are performed on a virtual machine Windows Server 2016 Standard.
I would like to check if the tasklist is running geckodriver. I do:
String line;
String pidInfo ="";
Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
pidInfo+=line;
}
input.close();
if(pidInfo.contains("geckodriver.exe"))
{
// do what you want
}
After running the tests, the code executes them on the local computer.
How to perform such a check on a Virtual machine

Your question is not fully defined.
A virtual machine has a operating system. And this is missing in your query.
But to give you a answer which is probably fitting, usually VMs are Linux based.
you can execute on Windows on command line manually to get the correct path
where tasklist
this will retrun you the right path to the command. If this is missing search for the command and check why it is missing on the PATH-variable (not a standard windows installation)
in my case it was
C:\Windows\System32\tasklist.exe
you execute on linux
ps aux | grep geckodriver
on MAC you can try also
ps aux | grep geckodriver
but maybe this is better
ps -jef | grep geckodriver
to get the correct full path to ps command on linux and MAC execute
which ps
hope it helps
Other point:
You should not use "\" path separators manually. I always would use
File.separator which will give you the correct OS-path character

Related

open terminal failed: missing or unsuitable terminal: unknown when running shell script from Java program

I had a Java program running a shell script with a Process, but for some reason when I try to run it it throws an error open terminal failed: missing or unsuitable terminal: unknown. From other SO questions, I think this is a tmux problem, but I'm not really sure how to solve it. Here's the code calling the script:
ProcessBuilder pb = new ProcessBuilder("/Users/user/eclipse-workspace/project/start.sh");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("output: ");
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
And here's the shell script:
#! /bin/sh
ssh -tt -i ~/.ssh/ssh-key.key opc#___._.___.___ tmux attach -d << END
./run.sh
END
exit 0
I have tried running the script from terminal, and it works from the terminal but it doesn't work when I run the Java program.
The problem is that you are attaching to an interactive tmux session, where you need to have a terminal which supports cursor movement codes etc.
The easy workaround is to not attach; just submit the command you want to run into the session.
ssh -tt -i ~/.ssh/ssh-key.key opc#___._.___.___ tmux send-keys './run' C-m
This obviously requires that whatever is running inside the remote tmux session is in a state where you can submit a shell command, i.e. at a shell prompt or similar. For robustness you might want to take additional measures to make sure this is always the case, or refactor your solution to avoid running inside tmux e.g. by having the command redirect its output to a file where you can examine it from any terminal at any time (though this assumes you don't also need to interact with it subsequently).

How do I execute shell script with java in the same shell the jar file is executed in runtime?

I am trying to run a shell script to mount a USB when I detect it through my java program.
My USB detection program/class works, mounting script works too, but when run the script via Java, It is executed in a different shell and does not mount the USB for my runtime usage of the usb files through java.
My Script is this:
#!/bin/bash
sudo mount /dev/sda1 /mnt/usb -o uid=pi,gid=pi
echo "USB drive mounted"
And my java program is so complex to just put it in here, but it essentially changes if " /dev/disk/by-id " had any changes and decides if a USB stick was inserted and then runs the shell script to mount it, It echoes "USB drive mounted" but when I try to interract with the files in /mnt/usb/ it is always empty, when I run the script by itself through the console, it mounts it to /mnt/usb and I can acces files
EDIT:
My java command to execute the shell is this:
Process p = new ProcessBuilder("/usr/local/bin/usb-connect.sh").start();
BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) System.out.println(line);
usbfh.transferFiles();//transfers files from usb to device
}catch(IOException e){ e.printStackTrace}
This is giving me the error of:
mount: /mnt/usb: special device /dev/sda1 does not exist.
Which is false since when the same script is executed via cmd prompt, it mounts with no errors

ProcessBuilder cannot find python3 in Mac OS

I am trying to execute "python3 --version" (this is just an example) from Java using ProcessBuilder. python3 is located in /usr/local/bin. I have configured the working directory. Here is my code snippet :
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "python3 --version");
pb.directory(new File("/usr/local/bin"));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
But it gives the error : /bin/bash: python3: command not found. Any way to resolve this?
PS : It can execute python --version as it is located in /usr/bin. Rather it executes successfully all commands pertaining to /usr/bin but none of the ones located in /usr/local/bin. python3 is just an instance of the general problem I am facing.
We also have to configure the environment (more so the PATH variable) and append /usr/local/bin as well to it. It will work fine then. I use the Eclipse IDE and I configured the PATH under environment in Run Configurations. It works fine now.

Shell script working fine on linux system but not running on windows machine

I am running a java project on windows machine which reads shell script file for getting the authorization token but getting following error :
java.io.IOException: Cannot run program "./token.sh": CreateProcess error=193, %1 is not a valid Win32 application
Java program for reading the shell script:
private static String execCommand(String username){
String line;
Process p = Runtime.getRuntime().exec("./token.sh -u " + username + " -p password123");
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
StringBuilder output = new StringBuilder();
while ((line = input.readLine()) != null) {
output.append(line);
}
How can i run the same code on windows machine.
It will not. You see the commands used in Windows CMD and Shell are different since they are completely different platforms. Even-though you use java to execute, it will not execute due to underlying fundamental difference. It is quite clear from the exception you are getting.
What can you do?
Read through the token.sh. Most probably the internal implementation
can be implemented in Windows. Then create an if condition which
checks System.getProperty("os.name") Then if its windows then
call the bat file and if the OS is unix based call the sh file. For
every other OS throw a valid exception.
Other probable way is that, if the token generated in machine
independent, you can use SSH(JSch or similar) to remote connect to a
UNIX server and get the token. If the token is machine dependent (if
its an auth token, then probably is), try using Cygwin interpreter
,which itself does not assure you the every shell file will run in
it.
Change the sh file and its implementation to python or ruby.Then respective interpreter may be installed on machines (which it might actually have,except for production machines).
Write the sh logic in Java itself rather than keeping a script file, since platform independence is actually a requirement here and you already has JRE up and running in both machines.
You cannot do that since the commands that the Linux bash script requires is a lot different than the windows commands.
For example -
To list the contents in a directory in Linux ls
To list the contents in a directory in Windows dir
You have to write a machine/architecture/OS independent code to run across all the operating systems.
Maybe, you can try using Python scripting for that.
Or else, you can ssh from windows machine to Linux machine and run that script from windows in Linux server.
You have to make a Windows specific implementation as well of this script. The most common and easy approach would be to use powershell.
If you want a version that works on both Windows and Unix, perhaps you should look into python.

Runtime.getRuntime().exec(commandStr) is not working on Mac OS X 10.12(Sierra)

I am trying to create a directory and with created directory doing mount operation of selected network drive. The code is working on other OS X version like OS X Lion or Caption but not working on OS X 10.12(Sierra)
Please suggest how this code will work.
proc=Runtime.getRuntime().exec(new String[]{"/bin/mkdir","/Volumes/Library"+count});
int exitCode = proc.waitFor();
System.out.println("Exit code : "+exitCode);
and then mounting the drive
String[] commandArr = new String[]{"/sbin/mount","-t","smbfs","//username:password#IP-Of-System/library"," /Volumes/Library"+count};
proc = Runtime.getRuntime().exec(commandArr);
This command is running with terminal but not executing using Runtime.getRuntime().exec().
The important thing here is that getRuntime().exec does not give you a full shell.
You can't assume that you can just do anything with it that you can do from a shell (the "command line").
You can try ProcessBuilder.start() (recommended in the Runtime.exec documentation), but that likely won't help either.
To make this work, you can create a shell script that does what you want, and then use Runtime.exec or ProcessBuilder.start to execute the script, passing the appropriate parameters to it.

Categories