PHP - shell_exec

Petit nouveau ! | 7 Messages

19 juin 2014, 15:33

Bonjour,
Je voudrais exécuter un programme exécutable sur ubuntu 12.04 via PHP5. Ce fichier exécutable nommé demo utilise des autres librairies .h.
Je connais la commande qui peut le faire, shell_exec/system/exec. Mais quand j'exécute, apparemment les librairies .h ne peuvent pas être exécutés.

Quel qu'un a une idée pour résoudre ce problème?

Je vous remercie.

Mammouth du PHP | 571 Messages

19 juin 2014, 16:49

visiblement ton problème n'est pas lié à php.
déjà pour faire fonctionner ce type de lib(écrit en C) il faut déjà s'assurer que t'as le compilateur gcc installé sur ton pc. Et le fichier .h n'est pas un exécutable c'est c'est un fichier d'en-tête d'un programme en C.as-tu un fichier nommé a.out ou main.c?

Petit nouveau ! | 7 Messages

19 juin 2014, 16:55

en effet je travaille sous ubuntu 12.04.
là j'écrit un fichier main.cpp qui utilise des autres fichiers .h. Ensuite, en utilisant le compilateur g++ pour compiler main.cpp et avoir un fichier demo exécutable sous linux. Je peux tout simplement exécuter le demo en tapant ./demo et il exécute sans aucun soucis.

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

19 juin 2014, 16:58

si l’exécution est correcte dans le shell, tu fait la même chose avec php ;)

pense à mettre le chemin absolus pour éviter les problèmes avec le relatif.

@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 7 Messages

20 juin 2014, 10:32

J'ai bien mis le chemin complet du fichier exécutable. Mais il connais toujours pas les fichiers .h....

Mammouth du PHP | 571 Messages

20 juin 2014, 10:52

c'est le fichier exécutable (./demo) qu'il faut passer à shell_exec et non le fichier .h.
//cas 1: demo situé dans le même repertoire que le script courant
$cmd = shell_exec=('./demo');
//pour afficher
var_dump($cmd);
//cas 2: demo situé dans un répertoire quelconque
$cmd = shell_exec=('/home/toto/demo');
var_dump($cmd);

Petit nouveau ! | 7 Messages

20 juin 2014, 11:29

C'est ça que j'ai mis dans ma page php

<?php
echo shell_exec('./serveurWeb');
?>
le fichier exécutable est bien dans le répertoire courant.

sauf que serveurWeb utilise les classes et les méthodes écrites dans différents fichiers de .cpp et .h

Petit nouveau ! | 7 Messages

20 juin 2014, 11:35

Je compile le fichier exécutable serveurWeb à partir du fichier .cpp suivant :

[cpp]

#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <term.h>
#include <sys/time.h>
#include <signal.h>
#include <libgen.h>
#include "LinuxDARwIn.h"
#include <math.h>

//#include <ctime>

using namespace Robot;
using namespace std;

#ifdef MX28_1024
#define MOTION_FILE_PATH "../../../Data/motion_1024.bin"
#else
#define MOTION_FILE_PATH "../../../Data/motion_4096.bin"
#endif

#define VERSION "1.000"
#define TCPIP_PORT 6501
#define ROBOPLUS_JOINT_MAXNUM 26
#define ARGUMENT_NAXNUM 30

int _getch()
{
struct termios oldt, newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}

string* string_split(string str_org, string str_tok)
{
int cutAt;
int index = 0;

string* str_result = new string[ARGUMENT_NAXNUM];
while((cutAt = str_org.find_first_of(str_tok)) != str_org.npos){
if(cutAt > 0)
str_result[index++] = str_org.substr(0, cutAt);
str_org = str_org.substr(cutAt + 1);
}

if(str_org.length() > 0)
str_result[index++] = str_org.substr(0, cutAt);

return str_result;
}

void sighandler(int sig)
{
exit(0);
}

int main(int argc, char *argv[])
{
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGQUIT, &sighandler);
signal(SIGINT, &sighandler);

// Initialization general parametres
int i,j,ch;
int iterFin,iterInit;
struct timeval tv;
char filename[128];

//////////////////// Framework Initialize ////////////////////////////
LinuxCM730 linux_cm730("/dev/ttyUSB0");
CM730 cm730(&linux_cm730);
if(cm730.Connect() == false){
printf("Fail to connect CM-730!\n");
return 0;
}

fprintf(stderr, "\n***********************************************************************\n");
fprintf(stderr, "* Communiquons !!! *\n");
fprintf(stderr, "***********************************************************************\n\n");

/*change_current_dir();
if(argc < 2)
strcpy(filename, MOTION_FILE_PATH); // Set default motion file path
else
strcpy(filename, argv[1]);*/

//////////////////// Framework Initialize ///////////////////////////
if(MotionManager::GetInstance()->Initialize(&cm730) == false){
printf("Fail to initialize Motion Manager!\n");
return 0;
}

MotionManager::GetInstance()->AddModule((MotionModule*)Action::GetInstance());
LinuxMotionTimer *motion_timer = new LinuxMotionTimer(MotionManager::GetInstance());
motion_timer->Stop();

cout << "[Running....]\n";
try{
// Create the socket
LinuxServer new_sock;
LinuxServer server (TCPIP_PORT);

while (true){
cout << "[Waiting..]" << endl;
server.accept (new_sock);
cout << "[Accepted..]" << endl;

try {
while (true){
string data;
Action::PAGE page;
new_sock >> data ;

// Initialization specific variables & parameters
string* p_str_tok = string_split(data, " ");
int ID_Dynamixel = atoi(p_str_tok[0].c_str());
int nbrPeriode = atoi(p_str_tok[1].c_str());
int Tperiode = atoi(p_str_tok[2].c_str());
int Fe = atoi(p_str_tok[3].c_str());
int positionInit = atoi(p_str_tok[4].c_str());
int positionFin = atoi(p_str_tok[5].c_str());
int punch = atoi(p_str_tok[6].c_str());
int correcteurP = atoi(p_str_tok[7].c_str());
int correcteurI = atoi(p_str_tok[8].c_str());
int correcteurD = atoi(p_str_tok[9].c_str());

int Te = 1000000/Fe; //calcul du temps d'échantillonage
int nbrTe = Tperiode*Fe;
int nbrTeTotal = nbrTe*nbrPeriode; //calcul du nombre d'échantillonage total

int addr;
unsigned char DataTable[6];
int position[nbrTeTotal];
uint64_t timestamp[nbrTeTotal];
uint64_t tc,old_tc,old_timestamp,old_transtimestamp,transtimestamp_init,transtimestamp_fin;
int velocity[nbrTeTotal];
int load[nbrTeTotal];
int stepPoint[nbrTeTotal];
int testTime;
int baud_rate = 1;
int return_delay_time = 1;

//Programme ...

for (j=0;j<nbrTeTotal;j++){
new_sock << "" << timestamp[j] << "," << stepPoint[j] << "," << position[j] << "," << velocity[j] << "," << load[j] << ";"<<"\r\n";
}
// End of loop
cout << "Simulation done!!!" << endl;
}
}

catch ( LinuxSocketException& ){
cout << "[Disconnected]" << endl;

if(Action::GetInstance()->IsRunning() == 1){
Action::GetInstance()->Stop();
while(Action::GetInstance()->IsRunning() == 1)
usleep(1);
MotionManager::GetInstance()->SetEnable(false);
//motion_timer->Stop();
}
}
}
}
catch ( LinuxSocketException& e )
{
cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}

return 0;
}


[/cpp]

quand je l'exécute à partir de page php, il me donne cette erreur Fail to connect CM-730!.
cm730 est une classe déclarée dans LinuxDARwIn.h ...
Modifié en dernier par moogli le 21 juin 2014, 23:21, modifié 1 fois.
Raison : correction bbcode (php => cpp)

Mammouth du PHP | 571 Messages

20 juin 2014, 14:36

sauf que serveurWeb utilise les classes et les méthodes écrites dans différents fichiers de .cpp et .h
si serveurWeb est un exécutable il ne dépend plus d'un quelconque fichier .cpp ou .h puisque après compilation ,tout le fichier exécutable se retrouve en un seul fichier.y aurait-il une spécificité propre à C++ dont j'ignore vu mes faibles connaissances en C++?

Que retourne ton exécutable sur un terminal? a-t-il besoin de communiquer avec l'utilisateur genre tapez votre nom?