How to give a name to a command inside shell_exec()? - php

I am working in the Laravel framework where I am using shell_exec(). I am creating the string of command as in the following example.
$cmd = "php artisan serve:setup";
$resp = shell_exec($cmd);
How can I put a name to the above command? It shows in the system monitor as only "php"; I have multiple commands like this. I need to access the PID of that command and also the status of that command.
The command is running successfully, and in the system monitor, it shows the process name as "PHP".
I need process name with exec() or shell_exec() from PHP.
I have tried like this:
$cmd = "bash -c exec ServerCPP -a cd $path && php artisan serve:setup 2>&1";
$resp = shell_exec($cmd);
dd($resp);
It gives the error: "Could not open input file: artisan."

Your last attempt needs to be corrected:
cd first and quote the path in case the $path variable contains spaces
quotes around the command executed with bash -c
exec -a ServerCPP if you want to give the name ServerCPP to your process
$cmd = "cd '$path' && bash -c 'exec -a ServerCPP php artisan serve:setup' 2>&1";
$resp = shell_exec($cmd);
dd($resp);

The best idea would be to create a command.
php artisan make:command ServeSetup
Once the file is generated, set the name of your command in app/Console/Commands.
protected $signature = 'serve:setup';
Hope this helps.

Related

Mount remote file system using sshfs with a PHP script

I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;
This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh

PHP exec doesn't return LFTP output when run from crontab

I have a PHP file (/path/to/file.php) containing an exec-command:
$result = exec('lftp -u USER,PASS sftp://USER#IP:PORT -e "cd FOLDER; mput -E FILE; quit;"');
When I run the command "php /path/to/file.php" in the terminal, $result has a value "X bytes transferred"
When I create a cron task for the same user using the exact same command, the $result is always an empty string. The command still works though, the file can be found on the FTP-server.
How can I get the cron version to output something so I can confirm that the transfer was succesfull?
So the solution was to add the following setting to a LFTP config file (for example /etc/lftp.conf):
set cmd:interactive yes

Execute Bash Script from PHP

I am calling a bash script from PHP, but I have experienced a strange issue that only specific parameter value passed to bash can be successfully executed:
My PHP code is simple:
$result = shell_exec("/scripts/createUser.sh $uname");
Bash script code:
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
usrname=$1
echo -e "Username: $1"
deploy="/wordpress.tar.gz"
dest="/data/$usrname"
mkdir -p $dest
cd $dest
tar zxvf $deploy -C $dest >/dev/null 2>&1
ls $dest
However this script can only successfully mkdir, extract the wordpress.tar.gz and list the folder when $uname == 'test', otherwise nothing happen (even cannot mkdir).
I chown the script to www user and grant execution permission, no help. And already tried run these commands via console as root, they run fine:
/scripts/createUser.sh admin
php deploy.php // in this script $uname == 'admin'
How could this happen? Thanks for your idea!
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
$return1 = $shell->exeCmd("/scripts/createUser.sh " . $uname);
//the return will be a string containing the return of the command
echo $return1;
But since you are simply triggering a bunch of bash commands, just issue them one by one using this project. That way you can also handle exceptions and see exactly which command returns something unexpected, i.e. permissions issues.
For commands that run for a long time you will have to change the timeout:
//is one hour enough?
$timeout = 3600000;
$return1 = $shell->exeCmd("/scripts/createUser.sh " . $uname, null, $timeout);

bash script does not work when called from PHP file?

I am trying to run a bash script from the web using php exec() method. The same bash script when ran from the terminal ./test.sh works fine executes all the command without any error. But when i try to run it from the command line it would absolutely not work. They both are in the same location. I have even give chmod 777 permission to that file.
Commnads like ls , pwd etc work but cf isn't working ?
test.sh
/usr/bin/cf login -a https://api.stage1.ng.bluemix.net -o jetmak#ca.blue.com -s dev -u jenk#ca.blue.com -p pass22
/usr/bin/cf api
/usr/bin/cf
executeshellfromphp.php
header('Content-Type: application/json');
$bluemixid = $_POST['bluemixid'];
$bluemixpwd = $_POST['bluemixpswd'];
$env = $_POST['env'];
$restart = $_POST['action'];
$result =shell_exec('sh /var/www/shellscriptphp/test.sh '.$env.' '.$restart.' '.$bluemixid.' '.$bluemixpwd.' ');
echo json_encode(array("result"=>$result));
exit();
Error:
FAILED
Config error: Error writing to manifest file:.cf/config.json
open .cf/config.json: no such file or directory

Pass cmd commands via php script

Here is my php code
$command = "C:\Program Files\ClustalW2>clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln";
exec($command);
When i run the command using the cmd, it generates the desired file. However when i try passing the same command via my php code it generates no result. How do i fix this problem?
Probably it's because of the > symbol before executable's filename? Also, try with single quotes:
$command = 'C:\Program Files\ClustalW2\clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln';
exec($command, $output, $retval);
var_dump($output);
var_dump($retval);

Categories