1

I want to write a shell script for my Ubuntu machine, that starts a few server applications. I need this script in order to execute it from my java application. But I need an easy way to group all these server processes, in order to be able to kill all the server processes from my Java application later.

My idea is to set the parent process id from my shell script and to kill them on a later point in time with the pkill command.

How can I do this all from a shell script?

  • An example for what you are looking at will be good to provide the answer – SHW Sep 15 '15 at 11:37
  • Im sorry I thought I stated what I want to do clearly. To be more precise I want to run a few instances of Appium Server at the start of my Java automation script. But also I want to provide a <myautomationscript> --stop command. That kills both the automation and the appiumservers that where created from this java application. – flxh Sep 15 '15 at 11:41
  • 1
    Killing by process group id (kill -TERM -${someId}) should do it unless some of the descendant processes create new process groups. – PSkocik Sep 15 '15 at 11:43
1

You can run instances of Appium server by noting down their PID, which you can used at a later point to kill. e.g.

Appium_server_1 & instance1=$!
Appium_server_2 & instance2=$!
Appium_server_3 & instance3=$!
.
.
.

and then you can kill all instances variable with kill command.

1

If your shell supports the PPID built in variable, you could use that variable in case you are really interested in the PID of the process that called the shell.

If you are rather interested in the PID of that shell and like to give it to others, use $$.

1

If I've understood your question correctly, the answer is you cannot do this. The java program starts, calls a shell script which starts more processes. The shell script then ends and you want the control to be passed to the java process which should be able to shut down all the other processes.

The short answer is you cannot do this. When the shell script dies, all processes started by it, become owned by process number 1 (init) and not the java process.

Is there a reason why you're using the shell script to launch the extra processes?

Why not just use java to launch all the processes (using the Process libraries), or use something like supervisord (http://supervisord.org/). You can control supervisord to start/stop processes from the java program then.

If all processes are named the same, you should also be able to use the killall command to kill them by name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.