System() function of php not working - php

$output = system('SET PATH=%PATH%;C:\xampp\GCC G++ compiler\bin && cd/ && c: && cd xampp\htdocs\College Coding\lessons\HTML && g++ -o temp temp.cpp && temp.exe > output.txt',$ret);
temp.cpp is not getting compiled in my machine. I tried on another and it is working.
it is not creating output.txt

What the output.txt should contain ? Are you trying to get the command output in $output var ?
You can use passthru
passthru("your_command", &$output);
Then $output will contain all outputs of your commands
Anas

Related

How can I verify if a file exist inside container docker?

I can copy it with this code:
exec('docker cp selenium:"/home/seluser/Downloads/'.$fileName.'" '.$filePath.'.pdf');
but I need verify before if it exist
You could use: exec(ls <your file> > /dev/null 2>&1 && echo 0 || echo 1) which will output 1 if the file <your file> does not exist check the output or replace echo 0 with the command to execute in case the file exist

echo exec("sudo top -n1 | awk '/30100/ {print $9}'"); not printing anything

The title is pretty self explanatory. I'm trying to get the CPU usage of a process by PID in PHP so I can display it in a webpage. The code works perfectly when written in the terminal, but doesn't print anything when done via PHP.
Tried
$cmd = "sudo top -n1 | awk '/30100/ {print $9}'";
echo exec($cmd);
where 30100 is the pid
and
$cmd = "sudo sh -c \"top -n1 | awk '/30100/ {print $9}'\" ";
echo exec($cmd);
I've also tried to $var = exec() and then var_dump($var) , the result was string(0)
I'd like to add that I'm using other commands on the system similar to this and they work fine. An example would be
$cmd = 'sudo -u server' . $sid . ' sh -c "pidof hlds_i686"';
$pid = exec($cmd);
which returns process pid
It might be an sderr / stdout issue , try using
$cmd = "sudo sh -c \"top -n1 | awk '/30100/ {print $9}'\" ";
echo shell_exec($cmd." 2>&1");

Bash how to get full script name from PID

I have this php function that checks the script's name from the given PID, and compares it to itself.
function isRunning($pid) {
$filename = exec('ps -p '.$pid.' -o "%c"');
$self = basename($_SERVER['SCRIPT_NAME']);
return ($filename == $self) ? TRUE : FALSE;
}
From what I know, I usually use this command to get the script name from the PID:
ps -o PID -o "%c"
It returns me the filename, but only the first 15 characters.
Since my script's name is
daily_system_check.php
the function always returns FALSE, because it's comparing itself with
daily_system_ch
Is there another bash command for Centos 6 that will return me script's full name?
You didn't specify what is your OS, but in Ubuntu Linux I can see full name of the script with adding --context to the ps call:
# ps -p 17165 --context
PID CONTEXT COMMAND
17165 unconfined /bin/bash ./testing_long_script_name.sh
#
read the the proc cmdline file:
cat /proc/$pid/cmdline | awk 'BEGIN {FS="\0"} {print $2}'
There seems to be no flag or collumn in "ps" command to show the whole filename without the filepath or it being cutoff. PHP's basename() gets the job done.
function isRunning($pid) {
$filename = basename(exec('ps -o cmd= '.$pid));
$self = basename($_SERVER['SCRIPT_NAME']);
return ($filename == $self) ? TRUE : FALSE;
}

Getting script exit status code in php

Is there anyway to get the exit status code for a php script run in the background via exec($cmd, $output, $exitCode)?
For example:
exec('php script.php &', $output, $exitCode);
If the trailing '&' isn't included then $exitCode works as expected, it's always 0 otherwise.
For anybody that finds themselves here, my solution was to make a php script that takes a script as an argument. The new script is called to the background and handles exit statuses appropriately.
For example:
$cmd = 'php asynchronous_script.php -p 1';
exec("php script_executor.php -c'$cmd' &");
The second script looks something like
$opts = getOpt('c:');
$cmd = rtrim($opts['c'], '&');
$exitCode = 0;
$output = '';
exec($cmd, $output, $exitCode);
if ($exitCode > 0) {
...
}
I found something that is probably quite equivalent to Pradeep's solution. Someone posted about this on the function's documentation page.
http://www.php.net/manual/en/function.exec.php#101506

Is there a better way of writing a git pre-commit hook to check any php file in a commit for parse errors?

What I have so far is
#!/bin/sh
php_syntax_check()
{
retval=0
for i in $(git-diff-index --name-only --cached HEAD -- | grep -e '\.php$'); do
if [ -f $i ]; then
output=$(php -l $i)
retval=$?
if [ $retval -gt 0 ]; then
echo "=============================================================================="
echo "Unstaging $i for the commit due to the follow parse errors"
echo "$output"
git reset -q HEAD $i
fi
fi
done
if [ $retval -gt 0 ]; then
exit $retval
fi
}
php_syntax_check
If the commit is a partial commit (not all the changes in the working tree are committed), then this make give incorrect results since it tests the working copy and not the staged copy.
One way to do this could be:
git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ --
find $TMPDIR -name '*.php' -print | xargs -n 1 php -l
Which would make a copy of the staged images into a scratch space and then run the test command on them there. If any of the files include other files in the build then you may have to recreate the whole staged image in the test tree and then test the changed files there (See: Git pre-commit hook : changed/added files).
I'm sorry if it's offtopic, but aren't you supposed to run some kind of automated tests (which would imply that the code has no syntax errors) before doing a commit?
If you've got the php5-cli installed you can write your pre-commit in PHP and use the syntax your more familiar with.
Just do something more like.
#!/usr/bin/php
<?php /* Your pre-commit check. */ ?>
My PHP implementation of the pre-commit hook checks whether the modified files in git are 'error free' and are as per PSR2 standard using either 'php-code-sniffer' or 'php-cs-fixer'
#!/usr/local/bin/php
<?php
/**
* Collect all files which have been added, copied or
* modified and store them in an array - output
*/
exec('git diff --cached --name-only --diff-filter=ACM', $output);
$isViolated = 0;
$violatedFiles = array();
// $php_cs_path = "/usr/local/bin/php-cs-fixer";
$php_cs_path = "~/.composer/vendor/bin/phpcs";
foreach ($output as $fileName) {
// Consider only PHP file for processing
if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {
$psr_output = array();
// Put the changes to be made in $psr_output, if not as per PSR2 standard
// php-cs-fixer
// exec("{$php_cs_path} fix {$fileName} --rules=#PSR2 --dry-run --diff", $psr_output, $return);
// php-code-sniffer
exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return);
if ($return != 0) {
$isViolated = 1;
$violatedFiles[] = $fileName;
echo implode("\n", $psr_output), "\n";
}
}
}
if ($isViolated == 1) {
echo "\n---------------------------- IMPORTANT --------------------------------\n";
echo "\nPlease use the suggestions above to fix the code in the following file: \n";
echo " => " . implode("\n => ", $violatedFiles);
echo "\n-----------------------------------------------------------------------\n\n\n";
exit(1);
} else {
echo "\n => Committed Successfully :-)\n\n";
exit(0);
}

Categories