배경 명령을 추적 할 수있는 한 가지 방법은 파일의 정보를 어딘가에 저장하는 것입니다. 명령 이름은 시스템 전체에서 고유하지 않을 수 있으므로 검사 할 수는 없습니다.
{
"processes": [
{
"command": "missilecomm launch -v",
"pid": 42792
}
]
}
(필자는 JSON "설득"의 생각 : 당신은 설정 파일에 프로세스 ID를 저장하고, 문자열 명령을 확인할 수 있습니다 : config 파일은 다음과 같이 보일 것이다
function execInBackground($cmd)
{
$running = false;
// get the state of our commands
$state = json_decode(file_get_contents("state.json"));
// check if the command we want to run is already running and remove commands that have ended
for ($i = 0; $i < count($state->processes); $i++)
{
// check if the process is running by the PID
if (!file_exists("/proc/" . $state->processes[$i]->pid))
{
// this command is running already, so remove it from the list
unset($state->processes[$i]);
}
else if ($cmd === $state->processes[$i]->command)
{
$running = true;
}
}
// reorder our array since it's probably out of order
$state->processes = array_values($state->processes);
// run the command silently if not already running
if (!$running)
{
$process = proc_open($cmd . " > /dev/null &", array(), $pipes);
$procStatus = proc_get_status($process);
$state->processes[] = array("command" => $cmd, "pid" => $procStatus["pid"]);
}
// save the new state of our commands
file_put_contents("state.json", json_encode($state));
}
하지만 원하는 형식을 사용할 수 있습니다.)
가끔에 동일한 명령 문자열을 두 번 이상 실행하려는 경우이 방법은 작동하지 않습니다.
execInBackground()
은 완료된 명령을 지우는 방법 때문에 Linux에서만 작동합니다. Windows에 프로세스 ID가 있는지 확인하는 다른 방법을 찾아야합니다. 이 코드는 테스트되지 않았으므로 내 proc_*
호출이 올바른지 확실하지 않습니다.
db 또는 파일에 로그하고 –
@ 대각선을 확인하십시오 : 이것이 내 첫 번째 본능 이었지만 구현 방법은 확실하지 않았습니다. 주 스크립트는 백그라운드에서 명령이 완료되기 전에 종료되고 백그라운드의 명령은 스스로 로깅을 수행하지 않습니다 (나는 둘 중 하나를 변경할 수 없음) 명령을 기록 할 때를 알 수있는 확실한 방법을 제공하지 않습니다 실행을 마쳤습니다. – DudeOnRock