Skip to content

Instantly share code, notes, and snippets.

@eahrold
Last active June 22, 2020 16:41
Show Gist options
  • Save eahrold/8343361 to your computer and use it in GitHub Desktop.
Save eahrold/8343361 to your computer and use it in GitHub Desktop.
Launch App At Login via LaunchAgent py (for munki)
#!/usr/bin/python
import subprocess
import plistlib
from os import seteuid
from ServiceManagement import *
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
installdir = 'Applications'
launchapp = 'AppName'
launchd = 'com.launchd.revdomain'
def main():
#write out the LaunchDaemon Plist
launchd_file = '/Library/LaunchAgents/'+launchd+'.plist'
program = '/'+installdir+'/'+launchapp+'.app/Contents/MacOS/'+launchapp
launchd_plist = {'Label':launchd,
'Program':[program],
'RunAtLoad':True,
'KeepAlive':{'SuccessfulExit':True},
}
plistlib.writePlist(launchd_plist,launchd_file )
#fix the permissions
subprocess.call(['/usr/sbin/chown','root:wheel',launchd_file])
subprocess.call(['/bin/chmod','0644',launchd_file])
cfuser = SCDynamicStoreCopyConsoleUser( None, None, None )
if cfuser[0]:
seteuid(cfuser[1])
SMJobSubmit(kSMDomainUserLaunchd,launch_agent,None,None)
if __name__ == '__main__':
main()
@amrset
Copy link

amrset commented Apr 22, 2014

I am wondering where the "launch_agent" variable was defined the one referenced in the SMJobSubmit command. Do you have an updated version of this script please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment