Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting pid of spawned exec in phing

I am using phing and running selenium server via ExecTask. Sometimes I need to stop running server by killing its process.

Is there a possibility in phing of getting PID of process spawned in ExecTask ?

like image 820
ts. Avatar asked Dec 02 '12 15:12

ts.


2 Answers

No, ExecTask cannot give the pid of spawned processes directly. It can only return it's exit status and output.

Maybe you can modify the command that you run in ExecTask itself to save the pid of spawned process. You can use $! to get the pid of the most recent background command.

job1 &                     //start job1 and run in background, end command with &
p1=$!                      //stores the pid 
echo $p1                   //gives pid of job1

When you want to kill the selenium server you can call this in another ExecTask :

pkill pid_to_kill

I am not sure if the changes made in shell environment with ExecTask stay or not. If yes then you can use $p1. Replace pid_to_kill with $p1 to kill job1. Else you will have to echo the pid and use the value from its output.

Otherwise you will have do pgrep name_of_program. It will give all processes containing the name. Then you can kill it with pkill.

like image 60
user568109 Avatar answered Sep 19 '22 11:09

user568109


Instead of launching the process you want to kill from the exec task (selenium server in your case). Use the exec task to launch a script (I used bash but ruby, python etc. will work too). this script will start the desired task and echo the pid. Substitute the required path and executable you want to run in the below.

#!bin/bash

./path_to_executable/process_to_run &
echo $!

Note the "&" this sends the process to the background and allows phing to continue building your project. The last line outputs the pid which can then be captured and saved to a file by the phing exec task. To save this pid add the output option to the phing exec task:

<exec command="your_script" spawn="true" output="./pid.txt" />

the output option will save the output of the exec task to a pid.txt file in the current directory. Note you may need to chown this file (to the user running phing) to enable it to be read later.

In a separate task, you can read the pid from the file and then use an exec task to kill the process.

<loadfile property="pid" file="./pid.txt" />
<exec command="kill ${pid}" dir="./" />

Note: in the above you may need to prepend sudo to the kill command (depending on who owns the process, and how it was started.

Optional but worth considering is adding a task to remove the pid.txt file. This will prevent any possibility of killing the wrong process (based on a stale pid). You may also want to sanity check the content of the pid.txt file since in the event of an error it could contain something other than the pid.

While this may not be the most direct or optimal solution it does work.

like image 27
Steve Robillard Avatar answered Sep 20 '22 11:09

Steve Robillard