Perl system() to run a script

Hello,

I'm trying to run "csso" (minify css) in a CGI script from the web panel.

That is not working: Returns error 0;

my $cmd = qq`csso stylesheet.css > stylesheet.min.css`;
system($cmd);

But that is working:

my $cmd = qq`echo 'blabla' > stylesheet.min.css`;
system($cmd);

I'm using full paths. It looks like "csso" doesn't have write permissions or something?
CentOS is the operating system.

Thanks.

Did it work when you run it outside the perl script on the shell ?

Yes. I even tried to run it as an apache (and it works):
su -s /bin/sh apache -c "[COMMAND]"

I dont think you need backticks. Just try with

$cmd = qq(/path/to/csso stylesheet.css > stylesheet.min.css)

---------- Post updated at 06:06 AM ---------- Previous update was at 06:01 AM ----------

And capture the exit code by assigning system($cmd) to a variable

Exit code:
32512

I think it means that it can't find the "csso" command.

which csso
points to
/usr/local/bin/csso
when
which echo
points to
/bin/echo

Maybe /usr/local/bin is not accessible for apache?

system() gives the actual exit code left shifted by 8. So 32512 means the actual exit code is 255 . You may need to refer csso man page to refer what this means.

No man page and nothing in the docs about error codes. Have to find a new solution.
Thank you very much anyway!

the fact that it works outside the perl script with apache user, I believe it should work inside the script as well. May be try pushing the STDERR to the file that you are writing and check if the file has any pointers to error. Try making file to /tmp/test.css to ensure that writing part if not failing.

$cmd = qq(/path/to/csso stylesheet.css > /tmp/stylesheet.min.css 2>&1)
system($cmd);
1 Like

Thanks, that was helpful!

/usr/local/bin/csso: line 13: node: command not found

It needs node.js, but it looks like it can't find it. I'll try to see how to fix it.

EDIT: I modified csso binary file and where it called:
node
I added:
/usr/local/bin/node

It's working perfectly. Thank you very much!

The correct thing to do in this case, instead of altering binary programs with a hex editor, is to add /usr/local/bin to your PATH environment variable so the system can find these things without hardcoded paths.

Thank you Corona688 for you reply.
This binary wasn't really a binary, it was just in the /bin folder, so I could just change the code without using hex editor.
/usr/local/bin is in the PATH when I run:
set
If my script calls perl system() from the web interface, and apache is the user who runs it, it doesn't see these PATH variable. Does it have it's own ENV variables? Can you tell me how I can change apache ENV variables?

Every process that runs has its own environment variables. Your perl script can alter its own environment variables via the ENV hash, i.e. $ENV{"PATH"}

1 Like

Thank you for your reply! I can make correct modifications now.