Last active
October 10, 2022 03:05
-
-
Save jankal/cbaeddf46f6c152f5e0d to your computer and use it in GitHub Desktop.
PHP Process class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-----BEGIN PGP SIGNED MESSAGE----- | |
Hash: SHA256 | |
<?php | |
/** | |
* @compability: Linux only. (Windows does not work). | |
* @author: Alexander Jank <[email protected]> | |
*/ | |
class Process{ | |
private $pid; | |
private $command; | |
private $cmdHash; | |
public $output = ""; | |
public function __construct($cl=false){ | |
if ($cl != false){ | |
$this->command = $cl; | |
$this->cmdHash = substr(md5($cl), 0, 5); | |
$this->runCom(); | |
} | |
} | |
private function runCom(){ | |
$command = 'nohup '.$this->command.' > /tmp/nohup-'. $this->cmdHash .' 2>&1 & echo $!'; | |
exec($command ,$op); | |
$this->pid = (int) $op[0]; | |
var_dump($op); | |
} | |
public function setPid($pid){ | |
$this->pid = $pid; | |
} | |
public function getPid(){ | |
return $this->pid; | |
} | |
public function status(){ | |
$command = 'ps -p '.$this->pid; | |
exec($command,$op); | |
if (!isset($op[1]))return false; | |
else return true; | |
} | |
public function start(){ | |
if ($this->command != '')$this->runCom(); | |
else return true; | |
} | |
public function stop(){ | |
$command = 'kill '.$this->pid; | |
exec($command); | |
$this->readOutput(); | |
if ($this->status() == false)return true; | |
else return false; | |
} | |
public function readOutput() { | |
$this->output = file_get_contents('/tmp/nohup-'. $this->cmdHash); | |
return $this->output; | |
} | |
} | |
// Usage: | |
$ping = new Process("ping 8.8.8.8"); | |
-----BEGIN PGP SIGNATURE----- | |
Version: GnuPG v2 | |
iQIcBAEBCAAGBQJXQeTOAAoJEC4SA5pJ49cLx0kQAKPGANw6ZDA3r7p25o/Gnc/m | |
Nr4l+sOUO75FVV9PgoNB8SOr8W6bfszXZxULrhKzo+yjUiHZ+pc7N2pB0LAouN29 | |
M/Xg3iz2CmbSnPNF3WK/TahQ+6WXswpLD9X8PrS6HdRwoj1y2Ulk/TgF8ZO0dTDA | |
IttD+xNE31PqXZlUN54YwXTP6N9TBzu7OQlTxTM/SHPCu+/cn0KDuELGWN8bNtlY | |
DlcUwpOeAGHVL4dBSdD3XRzQXYEN+WqwUANdFrdoRiUdx3IKMJrgqgeMB7ARmf9t | |
CKRm7DvrcbgoDJKWkJ8OVwNvn0HHxDBQIGO2IXmV81xwKcnxdqV42MUK8EYhh9ad | |
n9aY8VtEHwdzkRO6OHQLl8r6T0QifOnKI0DhZM3/8M8dqEItGZLtUdh6jLW7d084 | |
TjJF2x77Isa6eWX+gTIDM1sH7bm/G/hOLcaGwfnnCSuTHXlbVC25IWZ+zZfiLCtK | |
CjLWhCrX+PcI2ENcG5lFDyjQp+WBk3AX1FjeSYhedzVomUgoexXV3X3962T1EKyh | |
wy0YA5JOZ8XKsyPWkUGqE3IRieLvzYvXOhs4/FCoSsQv9Brw5MLkysNL7EscGi9G | |
JmDO1OaSP1KQC/trp4PG9LTT9LU8STdxrxv2l/FdIU/LW1vRvDnxzGqXjFuFh9W8 | |
mJedWtCu3dSuxXetS1bm | |
=rbQS | |
-----END PGP SIGNATURE----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment