2

I have a question regarding running a shell command via PHP. My goal is to successfully run compass compile [project] via PHP. I have tried the following:

echo system('compass compile [project]', $s); // prints [31m[0m
echo $s; // prints 1

echo passthru('compass compile [project]', $p); // prints [31m[0m
echo $p; // prints 1

echo shell_exec('compass compile [project]'); // prints [31m[0m

echo exec('compass compile [project]', $e, $ee);
print_r($e); // Array ( [0] => [31m[0m )
echo $ee; // prints 1

I even tried running a shell command to an executable file that contained compass compile test and I still got the same results as the trials above.

My questions What does [31m[0m mean? Does this represent binary data? Do these represent bash colors as search engines suggest?

As far as I know, the output should be the following:

Sample output

For kicks, I tried to execute via system(/usr/local/bin/compass compile [project]); and I got the same result. I double checked my path so I know I can execute these commands as expected. Here is the output from echo $PATH:

/usr/lib/lightdm/lightdm:
/usr/local/sbin:
/usr/local/bin:
/usr/sbin:
/usr/bin:
/sbin:/bin:
/usr/games:
/usr/local/games:
/var/lib/gems/1.9.1/bin

Is there a way to compile compass projects using PHP?

djthoms
  • 3,026
  • 2
  • 31
  • 56

4 Answers4

2

I've seen a similar error before.

Typically it is due to the things being output in the bash startup scripts. For example, I had an echo in one of my bash startups that jacked up a lot of scripts till I realized what was causing the problem.

Or, perhaps the user (www-data ?) doesn't actually have a home dir and appropriate startup scripts in place?

You can try this to get a non interactive shell:

exec("/bin/bash -c \"compass compile [project]\"", $e, $ee);
print_r($e); 
echo $ee; 

If you still have issues, try redirecting the output to a tmp file, an checking it:

exec("/bin/bash -c \"compass compile [project] > /tmp/compass.compile.output\"", $e, $ee);
print_r($e); 
echo $ee; 

See also: What's the difference between .bashrc, .bash_profile, and .environment?

Community
  • 1
  • 1
bubba
  • 3,839
  • 21
  • 25
1

The issue was fixed by using sass --compass and redirecting the stderr to stdout via echo shell_exec("sass --compass [project] 2>&1");

It was a pretty long and arduous process figuring this out since it's been awhile since I've dabbled in command line programs. Remember that error streams and output streams might be on different outputs. The easiest way to test this is to shovel the output into a file via:

# do this once with a good file and once with a file that will give errors
sass --poll style.scss > output.txt

Stderr shoveled to text file yields no results

If output.txt is empty then the error output is on the stderr stream such as the case above. We can correct this by redirecting the stderr to the srdout. For example:

sass --poll > output.txt 2>&1

#shows results
cat output.txt

Stderr to stdout

I created a simple PHP script that redirects the output from one textarea to the other. The code can be found here.

djthoms
  • 3,026
  • 2
  • 31
  • 56
0

First guess would be a permissions issue. Odds are the user account running PHP (unless you're running this from the command line, I'm guessing that this is the user that the httpd apache daemon is running under) doesn't have the permissions to do what you're asking. The errors are extremely unhelpful, however.

From what I can tell, it looks like an attempt to have the error show up in red on the command line. My guess is that there are hidden (or somehow never printed out) characters in-between the codes. Check out some of your apache and/or PHP error logs to see if anything helpful is showing up there that never made it into the PHP variable. Or, for kicks, try copy and pasting the output with the bash colors into a basic text editor and first delete each character from the beginning one by one... see if anything magically appears. If that doesn't work, try the same in reverse, backspacing from the end. Either way, there's an error occurring, so important that it must show in bold red letters to you, it's just not getting to you.

If it does in fact turn out to be a permissions issue, and it's one you can't remedy through permissions wrangling, you could create an intermediary file that your Apache user has permissions to write to, and your cron user has permissions to read from. Instead of running the code directly from PHP, put it in the file, then run a cron on a frequent basis looking for anything in that file, CHECKING IT FOR VALIDITY, and then running it through the compiler and removing it from the file.

It'd be ugly, but sometimes pretty things don't work.

Jason
  • 13,606
  • 2
  • 29
  • 40
-1

You guessed it right it is colors but the way it is defined is not right. For more information regarding using colors in console please refer to this document. Also, for compiling SCSS via compass you can use shell_exec command in linux. For more information regarding shell_exec please refer to this document. Let us know how it goes.

nicholasnet
  • 2,117
  • 2
  • 24
  • 46