[PHP] Shell_exec geeft continue output

Pagina: 1
Acties:
  • 104 views sinds 30-01-2008
  • Reageer

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
Hey, ik heb een Shoutcast server draaien op mijn Linux bak en daar heb ik een auto-dj bij (sc_trans_linux). Nu wil ik die auto-dj webbased stoppen en starten. Dat gaat allemaal goed. Alleen, als ik de auto-dj start via shell_exec dan geeft hij een continue output (soort van log) waardoor de shell_exec dus nooit afgerond word en de pagina blijft 'laden' en niet kan doorgaan naar de volgende actie in mijn script (een redirect).

Heeft iemand een idee hoe ik dit tussentijds kan stoppen zonder de process te killen of hoe ik kan meegeven dat hij die logging niet moet opslaan o.i.d. ?

Bijbehorende PHP code:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?
if(!isset($_GET['kill'])) {
$output = shell_exec("ps -A | grep sc_trans_linux");
    if(isset($output)) { 
        echo "<a href=\"kill.php?kill=1\">Klik hier om de Auto-DJ te <strong>stoppen</strong>.</a>"; 
    }
    else { 
        echo "<a href=\"kill.php?start=1\">Klik hier om de Auto-DJ te <strong>starten</strong>.</a>"; 
    }
}

if(isset($_GET['kill']) && $_GET['kill'] == 1) {
    if(shell_exec("pkill sc_trans_linux")) { echo "Auto-DJ gekilled."; } else { echo "Er ging iets fout."; }
}

if(isset($_GET['start']) && $_GET['start'] == 1) {
    if(shell_exec("/sc_trans_040/sc_trans_linux /sc_trans_040/sc_trans.conf &")) { echo "Auto-DJ gestart."; } else { echo "Er ging iets fout."; }
}

?>

[ Voor 40% gewijzigd door Peedy op 06-04-2007 15:35 ]


Acties:
  • 0 Henk 'm!

  • DirkT
  • Registratie: Juli 2002
  • Niet online

DirkT

toet

Je kan het makkelijk oplossen met deze functie:
http://de.php.net/manual/nl/function.passthru.php

iRacing profiel - FanaLEDs voor je racesimulatie displays en meer!


Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.
Dat moet juist niet... ik heb het getest en hij doet precies hetzelfde als shell_exec().

Acties:
  • 0 Henk 'm!

  • GlowMouse
  • Registratie: November 2002
  • Niet online
Je kunt proberen een & achter het commando te hangen. Wanneer dat niet werkt, staat in de handleiding een aantal reacties met tips hoe je iets in de achtergrond kunt draaien.

Bij exec staat ook dit:
Opmerking: If you start a program using this function and want to leave it running in the background, you have to make sure that the output of that program is redirected to a file or some other output stream or else PHP will hang until the execution of the program ends
Dit doe je door achter het commando >output.txt te plakken.

[ Voor 47% gewijzigd door GlowMouse op 06-04-2007 15:44 ]


Acties:
  • 0 Henk 'm!

  • Marcj
  • Registratie: November 2000
  • Laatst online: 15:16
als je nu als commandfunctie doet:
ps -A | grep -c sc_trans_linux
, dan geeft grep het aantal regels terug. Dan kun je dit getal gewoon testen (dus if($output == 0) etc..) :)

edit: verkeerd gesnapt: zie GlowMouse, met een & er achter werkt als het goed is wel :)

[ Voor 20% gewijzigd door Marcj op 06-04-2007 15:47 ]


Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
Een & erachter doe ik al, zoals je kan zien in mijn PHP code.

@hieronder; haalt ook niets uit...

[ Voor 23% gewijzigd door Peedy op 06-04-2007 15:54 ]


Acties:
  • 0 Henk 'm!

  • Marcj
  • Registratie: November 2000
  • Laatst online: 15:16
Probeer eens in plaats van shell_exec gewoon exec te gebruiken

Acties:
  • 0 Henk 'm!

  • Gonadan
  • Registratie: Februari 2004
  • Laatst online: 21:31

Gonadan

Admin Beeld & Geluid, Harde Waren
Als die commando's werken in principe hetzelfde.

Je zult toch echt de output moeten redirecten.
bijvoorbeeld > /dev/null

Zoals GlowMouse al aangaf is dat ook wat er op de PHP site staat. :)

Look for the signal in your life, not the noise.

Canon R6 | 50 f/1.8 STM | 430EX II
Sigma 85 f/1.4 Art | 100-400 Contemporary
Zeiss Distagon 21 f/2.8


Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
Ok, thanks, dat had ik nog niet gelezen. Zal het morgen eens proberen, ben nu aan het werk. Denk dat het dan wel gaat lukken :)

Acties:
  • 0 Henk 'm!

  • eghie
  • Registratie: Februari 2002
  • Niet online

eghie

Spoken words!

Marcj schreef op vrijdag 06 april 2007 @ 15:46:
als je nu als commandfunctie doet:
ps -A | grep -c sc_trans_linux
, dan geeft grep het aantal regels terug. Dan kun je dit getal gewoon testen (dus if($output == 0) etc..) :)

edit: verkeerd gesnapt: zie GlowMouse, met een & er achter werkt als het goed is wel :)
Omdat ps zelf soms ook grep zelf meeneemt als process, kun je of het volgende doen:
ps -A | grep sc_trans_linux | grep -v grep | wc -l

of gewoon
pidof sc_trans_linux

Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
Ok ik heb nu het volgende:
PHP:
1
2
3
4
if(isset($_GET['start']) && $_GET['start'] == 1) { 
    exec("/sc_trans_040/sc_trans_linux /sc_trans_040/sc_trans.conf  > /var/www/output.txt &");
    echo "Auto-DJ gestart.";
}

Even zonder een if-statement want dat zat me toch een beetje dwars. Maar op deze manier werkt het ook niet :( Gooi ik exact dezelfde regel via putty erin dan start hij hem wel :?

Acties:
  • 0 Henk 'm!

  • eghie
  • Registratie: Februari 2002
  • Niet online

eghie

Spoken words!

Peedy schreef op zaterdag 07 april 2007 @ 11:40:
Ok ik heb nu het volgende:
PHP:
1
2
3
4
if(isset($_GET['start']) && $_GET['start'] == 1) { 
    exec("/sc_trans_040/sc_trans_linux /sc_trans_040/sc_trans.conf  > /var/www/output.txt &");
    echo "Auto-DJ gestart.";
}

Even zonder een if-statement want dat zat me toch een beetje dwars. Maar op deze manier werkt het ook niet :( Gooi ik exact dezelfde regel via putty erin dan start hij hem wel :?
Wat dacht je van deze functie: http://nl2.php.net/manual/nl/function.proc-open.php

Dan krijg je bijvoorbeeld de volgende code:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (isset($_GET['start']) && $_get['start'] == 1) {
  $descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("file", "/var/www/output.txt", "a"),  // stdout is a file to write to with append options
   2 => array("file", "/var/www/error-output.txt", "a") // stderr is a file to write to with append options
  );
  
  $cwd = "/sc_trans_040";
  $process = proc_open('/sc_trans_040/sc_trans_linux /sc_trans_040/sc_trans.conf', $descriptorspec, $pipes, $cwd);

  if (is_resource($process)) {
    echo "process draait";
  } else {
    echo "process draait niet";
  }
  
  $return_value = proc_close($process);
}

Acties:
  • 0 Henk 'm!

  • Peedy
  • Registratie: Februari 2002
  • Laatst online: 06-11-2024
Het is gelukt :) Bedankt allemaal!
Pagina: 1