Scripting help: updating python bits

AJPinto
Honored Contributor III

We have been using this script likely stolen from somewhere without source for a few years now to configure the nomad launch agent. The python bits need to be updated and unfortunately I don't know python which is making it a tad bit complicated to update. Any advice would be much appreciated. 

 
loggedInUserPid: looks to be printing the user ID which is simple enough to translate to bash.
launchctlCmd: I have not the foggiest what this does to try to translate it.

**We are moving to JAMF connect later this year.

 

 

#!/bin/sh
## postinstall


#*=============================================================================

#*==============================å===============================================
#* REVISION HISTORY
#*=============================================================================
#* Date: 
#* Author: 
#* Solution: 
#*=============================================================================
 
pathToScript=$0
pathToPackage=$1
targetLocation=$2
targetVolume=$3
 
loggedInUserPid=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; username = SCDynamicStoreCopyConsoleUser(None, None, None)[1]; print(username);')
launchctlCmd=$(python -c 'import platform; from distutils.version import StrictVersion as SV; print("asuser") if SV(platform.mac_ver()[0]) >= SV("10.10") else "bsexec"')
 
## Delete old preference files, is any
sudo rm -rfv "/Library/LaunchAgents/com.trusourcelabs.NoMAD.plist"
sudo rm -rfv "/Users/$ActiveUser/Library/Preferences/com.trusourcelabs.NoMAD.plist"

## Create launchdaemon
read -d '' launchAgent <<"EOF"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>KeepAlive</key>
        <true/>
        <key>Label</key>
        <string>com.trusourcelabs.NoMAD</string>
        <key>LimitLoadToSessionType</key>
        <array>
        <string>Aqua</string>
        </array>
        <key>ProgramArguments</key>
        <array>
        <string>/Applications/NoMAD.app/Contents/MacOS/NoMAD</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>
EOF
echo "$launchAgent" > /Library/LaunchAgents/com.trusourcelabs.NoMAD.plist
 
## Grant appropriate permissions for daemon
/usr/sbin/chown root:wheel /Library/LaunchAgents/com.trusourcelabs.NoMAD.plist
/bin/chmod 644 /Library/LaunchAgents/com.trusourcelabs.NoMAD.plist
 
## Load daemon
#log "Loading LaunchAgent..."
/bin/launchctl "$launchctlCmd" "$loggedInUserPid" /bin/launchctl load /Library/LaunchAgents/com.trusourcelabs.NoMAD.plist

open /Applications/NoMad.app
 
exit 0      ## Success
exit 1      ## Failure

 

 

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

@AJPinto I'm pretty sure you can safely swap out the "$launchctlCmd" for asuser in your script, assuming you don't have some Macs in your environment sporting some really old Mac OS versions.

All that Python call is doing is determining whether the /bin/launchctl syntax should use asuser or bsexec based on the operating system version. This section of the python command:

print("asuser") if SV(platform.mac_ver()[0]) >= SV("10.10") else "bsexec"'

Is saying, if the OS version is 10.10 or higher, use asuser, otherwise fall back to bsexec

But bsexec isn't typically used these days. The launchctl manpage has this to say about these two flags

 

bsexec PID command [args]
              This executes the given command in as similar an execution context
              as possible to the target PID. Adopted attributes include the Mach
              bootstrap namespace, exception server and security audit session.
              It does not modify the process' credentials (UID, GID, etc.) or
              adopt any environment variables from the target process. It
              affects only the Mach bootstrap context and directly-related
              attributes.

     asuser UID command [args]
              This executes the given command in as similar an execution context
              as possible to that of the target user's bootstrap. Adopted
              attributes include the Mach bootstrap namespace, exception server
              and security audit session. It does not modify the process'
              credentials (UID, GID, etc.) or adopt any user-specific
              environment variables. It affects only the Mach bootstrap context
              and directly- related attributes.

 

As you can see, they both basically do the same thing, but asuser is the more up to date flag that appeared at some point in a much older OS version (Mac OS X 10.10 I guess)

 

View solution in original post

3 REPLIES 3

emily
Valued Contributor III
Valued Contributor III

Hey @AJPinto , there are definitely a few ways to get the logginedInUser that doesn't rely on python. I'd check out this post for some options and see what works best for you.

For using launchctl as the logged in user user, @arminBriegel has another great post on the topic that includes getting the information needed for the command in this script. He provides a function that you may be able to use here to get you where you need to be.

mm2270
Legendary Contributor III

@AJPinto I'm pretty sure you can safely swap out the "$launchctlCmd" for asuser in your script, assuming you don't have some Macs in your environment sporting some really old Mac OS versions.

All that Python call is doing is determining whether the /bin/launchctl syntax should use asuser or bsexec based on the operating system version. This section of the python command:

print("asuser") if SV(platform.mac_ver()[0]) >= SV("10.10") else "bsexec"'

Is saying, if the OS version is 10.10 or higher, use asuser, otherwise fall back to bsexec

But bsexec isn't typically used these days. The launchctl manpage has this to say about these two flags

 

bsexec PID command [args]
              This executes the given command in as similar an execution context
              as possible to the target PID. Adopted attributes include the Mach
              bootstrap namespace, exception server and security audit session.
              It does not modify the process' credentials (UID, GID, etc.) or
              adopt any environment variables from the target process. It
              affects only the Mach bootstrap context and directly-related
              attributes.

     asuser UID command [args]
              This executes the given command in as similar an execution context
              as possible to that of the target user's bootstrap. Adopted
              attributes include the Mach bootstrap namespace, exception server
              and security audit session. It does not modify the process'
              credentials (UID, GID, etc.) or adopt any user-specific
              environment variables. It affects only the Mach bootstrap context
              and directly- related attributes.

 

As you can see, they both basically do the same thing, but asuser is the more up to date flag that appeared at some point in a much older OS version (Mac OS X 10.10 I guess)

 

Mithrandir
New Contributor III

Sorry to necro update, but I just came across this thread.

Following shell commands worked for me on Sonoma 14.5:

loggedInUserPid=$(stat -f "%Su %p" /dev/console)
stat -f "%Su %p" /dev/console
macOSVersion=$(sw_vers -productVersion)
sw_vers -productVersion
if [ "$(echo "${macOSVersion}" | cut -d'.' -f2)" -ge 10 ] && [ "$(echo "${macOSVersion}" | cut -d'.' -f1)" -ge 10 ]; then
launchctlCmd="asuser"
else
launchctlCmd="bsexec"
fi
echo "${macOSVersion}" | cut -d'.' -f2

echo "${launchctlCmd}"