Launching and stopping appium server programmtically

Thanks for providing this useful info, helped me a lot.

This is what worked for me…
CommandLine command = new CommandLine(“cmd”);
command.addArgument("/c");
command.addArgument(“C://Users//…//Appium//node.exe”);
command.addArgument(“C://Users//…//Appium//node_modules//appium//bin//appium.js”);
command.addArgument("–address");
command.addArgument(“0.0.0.0”);
command.addArgument("–port");
command.addArgument(“4724”);
command.addArgument("–no-reset");
command.addArgument("–log");
command.addArgument(“C://Users//…//log//appiumLogs.txt”);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);

1 Like

How do we stop the server and free the port in the end?

As I mentioned in the main post:

You will end up with something like this:

CommandLine command = new CommandLine(“cmd”);
command.addArgument(“/c”);
command.addArgument(“taskkill”);
command.addArgument(“/F”);
command.addArgument(“/IM”);
command.addArgument(“node.exe”);

This does not work for me.

When i try to re-run the test, I see the error:
e[31merrore[39m: Couldn’t start Appium REST http interface listener. Requested port is already in use. Please make sure there’s no other instance of Appium running already.

I have to manually kill the process from Windows Task Manager and the re-run the test.

1 Like

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