This OctoPrint plugin adds the ability to define GCode commands that execute local system commands.
Install the plugin using Plugin Manager from Settings