Launching and stopping appium server programmtically

It works just fine with me … Are you sure you are not doing something wrong??

Hey, Guys

I use processBuilder for that. It has method destroy().
You could check my example in the thread:

1 Like

I thought about using it at first, but it turned out that it doesn’t destroy any spawned processes started by your application. You will end up with memory leaks and probably some errors when you try to open it again.

It perfectly closes child processes. As you can see, there is a process tree and when cmd.exe (with PID 6320) get closed - all child processes get closed as well

As I understood from the image you shared, you are starting a new JVM inside the original one. That’s why when you call the destroy function, the Java process and all of its child processes are destroyed. In my case, I was starting the Appium server from the original JVM which means that all of its spawned processes won’t be destroyed until the original Java JVM terminates.

P.S: I am running more than one server instance simultaneously and I can’t wait to clear up memory at the end when the JVM terminates.

Parent java process from the screenshot above - is IDE:-)
Just test run via “mvn test” command-line and got the same result: all processes closed perfectly!

Looks like it’s working :smile: with the destry method and there is no need to taskill the process … I am not sure why it wasn’t working with me when I tried it?
Anyway, I will give it another try. Thanks :smile:

Hi Everyone,

When I tried to launch the server, I am geeting this below error:
" /Applications/Appium.app/Contents/Resources/node/bin/node: cannot execute binary file "

My code is this, Can anyone please help on this ?
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
command.addArgument("–address");
command.addArgument(“127.0.0.1”);
command.addArgument("–port");
command.addArgument(“4723”);
command.addArgument("–no-reset");
command.addArgument("–log");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);

Thanks in advance.

Try using

command.addArgument(“argument”, false);

@Hassan_Radi : Still getting the same error.
I am using Appium version : 1.3.1 (Ophiuchus)

below is my entire code-
public static void main(String[] args) throws ExecuteException, IOException {
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
command.addArgument("–address");
command.addArgument(“127.0.0.1”);
command.addArgument("–port");
command.addArgument(“4723”);
command.addArgument("–no-reset");
command.addArgument("–log");
command.addArgument(“argument”, false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
}

I saw this line in your post,
“command.addArgument(”/c");"
what does it do exactly ?

By that, I didn’t mean adding it explicity :smile: … I meant to convert each line that uses addArgument function and make it take an argument and a false value :smile:.

for example:

command.addArgument("–address");

should be

command.addArgument("–address", false);

@Hassan_Radi: LOL !!

But still getting the same error, do you think its because of any device incompatibility ?
Because all I get in searching internet about this is 32-bit vs 64-bit issues only.

OS X : Version 10.8.5
Appium version : 1.3.1 (Ophiuchus)

It might be due to compatibility issues as it’s working fine with me…

I am running below code pn my mac no error but appium is not started as well.
Where as if i involve from terminal this command : /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --port 4723 --no-reset --local-timezone

code::

CommandLine command = new CommandLine("/bin/sh -c");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node",false);
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js",false);
command.addArgument("–address",false);
command.addArgument(“127.0.0.1”);
command.addArgument("–port",false);
command.addArgument(“4723”);
command.addArgument("–no-reset",false);
command.addArgument("–log");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I guess I missed “-c” while invoking CommandLine. Its not showing the error, but like @Sachin_Chauhan on running the code nothing is happening.

I am using code at 10.10.1 with java 1.7

on windows with windows format command its working. But on mac its doing nothing not even error. Now I am stuck. I appreciate if someone guide me, what I am doing wrong?

Should be :

CommandLine command = new CommandLine("/bin/sh");

As soon as I removed -c, this exception started coming
/Applications/Appium.app/Contents/Resources/node/bin/node: /Applications/Appium.app/Contents/Resources/node/bin/node: cannot execute binary file

1 Like

I left the hope with command line. Now I am using process builder and its working ok.

List list = new ArrayList();
list.add("/Applications/Appium.app/Contents/Resources/node/bin/node");
list.add("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
list.add("–address");
list.add(“127.0.0.1”);
list.add("–port");
list.add(“4723”);
list.add("–no-reset");
// create the process builder
ProcessBuilder pb = new ProcessBuilder(list);
try {
pb.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}