Make my app quit and reopen when updating it with .pkg

Context:

Our app has a network system extension. After updating it via MDM sometimes the app and the extension have different versions until you restart the app. I suspect that it is because the app is not quit before installing.

As a fix I am trying to create a .pkg with preinstall and postinstall script, that would close the app before installation and open it after installation.

My code:

FileUtils.mkdir_p("install_scripts")
File.open('install_scripts/preinstall', 'w') do |file|
  file.puts "#!/bin/bash\nosascript -e 'quit app \"#{options[:app_name]}\"'\nexit 0"
end

File.open('install_scripts/postinstall', 'w') do |file|
  file.puts "#!/bin/bash\nopen -a '#{options[:app_name]}'\nexit 0'"
end

sh "chmod a+x install_scripts/preinstall"
sh "chmod a+x install_scripts/postinstall"

sh(
  "pkgbuild",
   "--scripts", "install_scripts",
   "--identifier", ***_identifier(options),
   "--component", "../#{options[:build_path]}/#{options[:app_name]}.app",
   "--install-location", "/Applications/#{options[:app_name]}.app",
   "Distribution.pkg"
)
sh(
  "productbuild",
  "--synthesize",
  "--package", "Distribution.pkg", 
  "Distribution.xml"
)

sh(
  "productbuild",
   "--distribution", "Distribution.xml",
   "--sign", "Developer ID Installer: *** Inc. (#{ENV["APPLE_TEAM_ID"]})",
   "--package-path", ".",
   "../artifacts/#{options[:app_name]}.pkg"
)

Issue:

I haven't got it to actually install the app or to close or open the running app. I've tried several changes. The code above fails with this error. Do you see an issue in my code, or could you point me to alternative implementation?

installd[1101]: PackageKit: Install Failed: Error Domain=NSCocoaErrorDomain Code=516 ""Myapp Dev.app" couldn't be moved to "Applications" because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app/Myapp Dev.app, NSUserStringVariant=( Move ), NSDestinationFilePath=/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app, NSFilePath=/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app/Myapp Dev.app, NSUnderlyingError=0x117e304b0 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}} { NSDestinationFilePath = "/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app"; NSFilePath = "/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app/Myapp Dev.app"; NSSourceFilePathErrorKey = "/Library/InstallerSandboxes/.PKInstallSandboxManager/8E84CB81-4724-4D4E-BE76-3A24DECC60A6.activeSandbox/Root/Applications/Myapp Dev.app/Myapp Dev.app"; NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=17 "File exists""; NSUserStringVariant = ( Move ); }

Accepted Reply

Resolved. Had to read the logs /var/log/install.log - I had the app renamed and placed in downloads folder, it detected it and that's why didn't install anything in Applications

Replies

I changed file.puts "#!/bin/bash\nopen -a '#{options[:app_name]}'\nexit 0'" to file.puts "#!/bin/bash\nopen -a '#{options[:app_name]}'\nexit 0"

And "--install-location", "/Applications/#{options[:app_name]}.app", to "--install-location", "/Applications",

Result: postinstall now works preinstall does not close opened app the actual install also doesn't happen, the above scripts were tested on the app that already exists

Resolved. Had to read the logs /var/log/install.log - I had the app renamed and placed in downloads folder, it detected it and that's why didn't install anything in Applications