Getting pid generated by exec in phing

I use phing and start the selenium server through ExecTask. Sometimes I need to stop the server from starting by killing its process.

Is there any way in phing to get the PID of the process spawned in ExecTask?

+7
source share
4 answers

No, ExecTask cannot directly pass the pid of spawned processes. It can only return exit status and exit.

Perhaps you can change the command that you run in ExecTask to save the pid of the spawned process. You can use $! to get the pid from the last background command.

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

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

 pkill pid_to_kill 

I'm not sure if changes made to the shell environment with ExecTask remain or not. If so, then you can use $p1 . Replace pid_to_kill with $ p1 to kill job1. Otherwise, you will have to echo the pid and use the value from its output.

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

+3
source

Perhaps you could execute the second parameter inside the exec command.

 exec("Script To Run", $output); 

The second variable gets the output of the current current script in array format. To show full and readable text from the output, I would use a foreach :

 exec("ifconfig", $output); // Presuming you are developing for a Linux server foreach ($output as $outputvar) { echo $outputvar . "<br>"; } 

After that I will use something like strpos to pull the information from $outputvar for the string you are looking for.

Hope this looks like what you are looking for.

+1
source

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

 #!bin/bash ./path_to_executable/process_to_run & echo $! 

Pay attention to the "&" this puts the process in the background and allows phing to continue building your project. The last line prints a pid, which can then be captured and saved to a file using the phing exec task. To save this pid, add the output parameter 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 the pid.txt file in the current directory. Please note that you may need this file (for the user performing phing) so that it can be read later.

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

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

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

Optional, but worth considering adding a task to delete the pid.txt file. This will prevent any chance of killing the wrong process (based on an obsolete pid). You can also check that the contents of the pid.txt file are correct, because in case of an error, it may contain something other than pid.

Although this may not be the most direct or optimal solution, it really works.

+1
source

In the end, I created a phing task that saves the pid of the running program and stops it when you ask for it. It uses Cocur \ BackgroundProcess to start the process in the background and can also return a pid.

 <?php require_once "phing/Task.php"; class BackgroundExecTask extends Task { protected $command = null; protected $executable = null; protected $id = null; protected static $pidMap = []; public function init() { if (!class_exists('\Cocur\BackgroundProcess\BackgroundProcess')) { throw new BuildException("This task requires the Cocur Background Process componente installed and available on the include path", $this->getLocation()); } } public function main() { switch ($this->command) { case "start": return $this->start(); case "stop": return $this->stop(); } } protected function start() { $process = new \Cocur\BackgroundProcess\BackgroundProcess($this->executable); $process->run(); // you can also return the pid //$this->project->setProperty($this->pidProperty, $process->getPid()); self::$pidMap[$this->id] = $process; } protected function stop() { self::$pidMap[$this->id]->stop(); } public function setCommand($command) { $this->command = "" . $command; } public function setExecutable($executable) { $this->executable = "" . $executable; } public function setId($id) { $this->id = "" . $id; } } 

Using:

 <backgroundexec id="myprogram" command="start" executable="somebinary" /> <backgroundexec id="myprogram" command="stop" /> 
0
source

All Articles