Close Libreoffice gracefuly from command line

Hi,

I’m running LibreOffice 6 on multiple remote controlled linux (debian) computers.
Now I’d like to close LibreOffice on every machine without manually closing it on multiple machines.

So I wrote as script that logs in to this machines using SSH and kills LibreOffice. This however has the problem that the lock files of the saved documents stays on our network share and other users aren’t able to edit them afterwards. I tried several kill levels like “HUP” (nothing happens), “INT”, “QUIT”, “KILL” or “TERM”. Everything kills LibreOffice immediately and won’t remove the lock files. When you kill LibreOffice on Windows using the TaskManager it works as expected. No lock file afterwards, but on Linux it wont’t.

To sum up the problem:
I need to close LibreOffice without leaving a lock file with remote control. If the document is unsaved it doesn’t matter if it get lost. It’s intended in this case. Just disabling the lock file isn’t an option.

How to solve this problem?

  • I tried using wmctrl and xdoctrl, but this isn’t really reliable, because you don’t know in which state the user leave the computer.
  • As mentioned kill wont’t work. Maybe somebody has another way to solve it using kill?

I managed it to close the document using a Macro and the code:

ThisComponent.Close(true)

This worked, but I can’t do it remotly. So one hacky solution would be to write an extension that is able to communicate with the underlying system and closes LibreOffice gracefully so it won’t leave a lock file.’
But this is very hacky and I don’t like to do it this way.

So does anybody has a better solution how to solve this problem?

I can’t help in other way but give you a code pointer. In Windows-specific code, there’s a ImplHandleShutDownMsg() function (in vcl/win/window/salframe.cxx) that is called when Windows shuts down itself; the function executes handler of SalEvent::Shutdown event which closes the application.

I only can advise you to prepare a patch for LibreOffice UNX-specific code that would handle some appropriate signal doing something similar. To do that, you’d need first to file an enhancement request; then prepare a development environment; prepare a patch (with its commit message first line starting with "tdf#XXXXXX: … " which refers to the enhancement request # you created); send it for review; and send your license statement.

Thank you very much. However I think that is to complicated for this use case, but maybe I can create an enhancement request if somebody else would like to implement it. I’ve know written an Python UNO script.

I solved this by writing a simple python uno script which communicates with the libreoffice process and closes all documents. This will remove the lock files:

I know that the code isn’t perfect. So help from anybody is very appreciated.

import uno

# Connect to LibreOffice
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

# Create new XDesktop instance
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

# Load all documents Components
componentsEnumerator = desktop.getComponents().createEnumeration()

# Default value if any component isn't able to close
closeDenied = False

# Iterate through all Components
while componentsEnumerator.hasMoreElements():
    # Load document
    document = componentsEnumerator.nextElement()

    # Is close supported?
    if not hasattr(document, "close"):
        continue

    # Try to close it regulary
    try:
        document.close(True)   
    except:
        # Dispose the frame of this component
        document.getCurrentController().getFrame().dispose()
    
ctx.ServiceManager