Script to launch on every user login MacOS

mmajewski
New Contributor

I am looking ultimately to launch an application for every user that logs into a Mac. I tried running a script and the policy log shows it ran with an exit code of 0.

#!/bin/bash
open -a /Applications/myapp.app
exit 0

I can make that script on the client device and execute it and it works. Any ideas as to what I am missing?

2 REPLIES 2

cbrewer
Valued Contributor II

Jamf scripts run as root. You probably want your open command to run as the logged in user.

loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
su -l $loggedInUser -c "open /Applications/myapp.app/"

Another issue you're probably running into is that jamf login scripts are usually going to run before the user's login environment has fully loaded. Look into Launch Agents. Launch Agents load when the user environment loads.

bentoms
Release Candidate Programs Tester

This is really something that a LaunchAgent should be used for.

LaunchAgents & LaunchDaemons are important things to learn when it comes to macOS administration.

As such, have a look at: https://www.launchd.info