Scripting help needed - launchdaemon

jameson
Contributor II

I would like to create a launchdaemon that launch a script when finder and dock processes are existing.  I don´t know if anyone has something similar running they can try and show . 

the launchdaemon I can create on my own - but how to build in script that it only should run if those dock and finder process exist I struggle to find

46 REPLIES 46

bwoods
Valued Contributor

Script to install Rosetta

#!/bin/bash

# Installs Rosetta as needed on Apple Silicon Macs.

exitcode=0

# Determine OS version
# Save current IFS state

OLDIFS=$IFS

IFS='.' read osvers_major osvers_minor osvers_dot_version <<< "$(/usr/bin/sw_vers -productVersion)"

# restore IFS to previous state

IFS=$OLDIFS

# Check to see if the Mac is reporting itself as running macOS 11

if [[ ${osvers_major} -ge 11 ]]; then

  # Check to see if the Mac needs Rosetta installed by testing the processor

  processor=$(/usr/sbin/sysctl -n machdep.cpu.brand_string | grep -o "Intel")
  
  if [[ -n "$processor" ]]; then
    echo "$processor processor installed. No need to install Rosetta."
  else

    # Check Rosetta LaunchDaemon. If no LaunchDaemon is found,
    # perform a non-interactive install of Rosetta.
    
    if [[ ! -f "/Library/Apple/System/Library/LaunchDaemons/com.apple.oahd.plist" ]]; then
        /usr/sbin/softwareupdate --install-rosetta --agree-to-license
       
        if [[ $? -eq 0 ]]; then
        	echo "Rosetta has been successfully installed."
        else
        	echo "Rosetta installation failed!"
        	exitcode=1
        fi
   
    else
    	echo "Rosetta is already installed. Nothing to do."
    fi
  fi
  else
    echo "Mac is running macOS $osvers_major.$osvers_minor.$osvers_dot_version."
    echo "No need to install Rosetta on this version of macOS."
fi

exit $exitcode

bwoods
Valued Contributor

Script to enable location services automatically.

## configure ntp server

/bin/cat > /etc/ntp.conf << 'NEW_NTP_CONF'
server time.apple.com
NEW_NTP_CONF

## configure automatic timezone

/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1


## Set date and time automatically
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES
/usr/sbin/systemsetup -setusingnetworktime on
/usr/sbin/systemsetup -gettimezone
/usr/sbin/systemsetup -getnetworktimeserver

### Restart location services daemon (locationd)
/usr/bin/killall locationd

exit 0;		## Success
exit 1;		## Failure

Captainamerica
Contributor II

Overall the issue seems to be the launchdaemon. It doesn´t start for some reason. I can read that launchdaemons can be tricky and must be very accurate or else the system will not load them. 

bwoods
Valued Contributor

bwoods_2-1642084044897.png

 

Captainamerica
Contributor II

so in pre-stage you do not put in the creation of launchdaemon ? that is where I have it - I first launch depnotify when user desktop is loaded

bwoods
Valued Contributor

My launchdaemon creation script and DEPNotify scripts are always in the cloud so that I can edit them without creating a new package. 

Captainamerica
Contributor II

Think we found the issue now. The jamf binary does note exist in pre-stage when the launchdaemon executes - so somehow just have to build in that the launchdaemon first launch when the jamf binary exist in the path