0

When running this from command line as root it works

unoconv -f csv $file

But when running it as www-data this error is returned

Traceback (most recent call last):
  File "/usr/bin/unoconv", line 1114, in <module>
    office_environ(of)
  File "/usr/bin/unoconv", line 203, in office_environ
    os.environ['PATH'] = realpath(office.basepath, 'program') + os.pathsep + os.environ['PATH']
  File "/usr/lib/python3.4/os.py", line 633, in __getitem__
    raise KeyError(key) from None
KeyError: 'PATH'

update

echo shell_exec('echo $PATH');
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
7
  • What is the value of www-data's $PATH and the root's $PATH? Try setting www-data's path before running the command. See dirtyvital.wordpress.com/2012/11/11/….
    – Jim K
    Commented Dec 21, 2015 at 19:58
  • what do you mean by www-data's path?
    – clarkk
    Commented Dec 21, 2015 at 20:46
  • I mean set the path to something so that it is not empty. That way the key will exist in the os.environ dict. For example you could set it to . or a path to an empty directory.
    – Jim K
    Commented Dec 21, 2015 at 21:17
  • have updated my question..
    – clarkk
    Commented Dec 21, 2015 at 21:19
  • Ok, it doesn't seem to be empty. What is shell_exec -- are you using PHP? If so then please add that tag, and post the actual PHP code that contains the call. I have not used PHP before but maybe I can help.
    – Jim K
    Commented Dec 21, 2015 at 21:28

3 Answers 3

1

centos 7.3 with php via php-fpm the env in php is cleaned by php-fpm

u can use putenv to set evn["PATH"] in php code, examples

putenv("PATH=/sbin:/bin:/usr/sbin:/usr/bin"); 
var_dump(shell_exec('unoconv -vvvv -f pdf -o 123.pdf 123.doc));

or u can set env use one line shell cmd

var_dump(shell_exec('PATH=/sbin:/bin:/usr/sbin:/usr/bin'.' unoconv -vvvv -f pdf -o 123.pdf 123.doc));

or u can change /etc/php-fpm.d/www.conf to pass the env to php, add this line

clean_env = no

and the restart php-fpm

systemctl restart php-fpm.service
0

The PHP call you used (pasted from chat):

exec("unoconv -f csv $file")

My guess is that exec() is giving you an environment that is too limited. To work around this, you could set up a polled directory. The PHP script would copy files to be converted into the polled directory and wait for the files to be converted.

Then create a bash script (either running as root or a somewhat more secure user) to run in an infinite loop and check the polled directory for any incoming files. See How to keep polling file in a directory till it arrives in Unix for what the bash script might look like.

When the bash script sees incoming files, it runs unoconv.

0

Found a solution myself by running libreoffice directly

sudo libreoffice --headless --convert-to csv --outdir $tmp_path $file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.