How to NOT connect to a running instance

I want to convert files from a script using command line options, for example:

   libreoffice --headless --invisible --convert-to csv file.xls

This works only if Libre Office is not currently running interactively.

For example, if localc was run (on an unrelated document), then the above shell command silently fails to have any effect.

I suspect the command connects to the pre-existing interactive instance. How can I prevent this, forcing the command to use an independently started instance?

(or any other way to make the command work reliably, whether or not LO is already running)

This is unfortunately not directly possible thanks to Bug #37531.

The workaround is to run LibO with another profile. From that bug report then, comment 25 suggests using the not-so-well documented -env flag:

    libreoffice "-env:UserInstallation=file:///tmp/LibO_Conversion" --headless --invisible --convert-to csv file.xls

If security is a concern, note that LibO creates that directory with user-only permissions, and you can similarly tack on an rm (or the appropriate operating system variant) command:

    libreoffice [...] ; rm -rf /tmp/LibO_Conversion

Thanks, very useful. Small correction though, it should be:

-env:UserInstallation=“file:///tmpLibO_Conversion”

Heh. It actually doesn’t matter. The quotes are interpreted by the shell and are only required if there are “special characters” (e.g., spaces). They become more necessary if you want more robust behavior when interacting with unknown (user) input or variables, so using them as a matter of habit is just good practice. In this case, both methods will give LibreOffice the exact same string (with no quotes because the shell will remove them).

On several GNU/Linux systems /tmp is cleaned at each reboot, so you may not need to remove /tmp/LibO_Conversion

Even better is to create the temporary directory for every invocation, so that your script that calls libreoffice can be started multiple times in parallel, and starting out with a fresh slate every time:

tmpdir=`mktemp -d /tmp/libreoffice-XXXXXXXXXXXX`
trap "rm -rf $tmpdir" EXIT INT
libreoffice "-env:UserInstallation=file://$tmpdir" --headless --nolockcheck --convert-to pdf "$out"