Run Script When an Application is Launched

buckeye_nut27
New Contributor II

I am trying to create a script that displays a message when a specific application is launched to alert the user it is not supported and they're using it at their own risk. I was able to get the base script I want to trigger together, but cannot seem to find a way outside of constant loop checking running processes to trigger it (below). I thought maybe a daemon would be my best bet but I can't seem to find a way to know if the application is launched.

 

#!/bin/bash

# Infinite loop to continuously monitor processes
while true; do
    # Check if 'Microsoft OneNote' process is running
    if pgrep -x "Microsoft OneNote" > /dev/null; then
        echo "Microsoft OneNote process has started!"
    fi
    # Adjust sleep time as needed to avoid high CPU usage
    sleep 5  # Check every 5 seconds, adjust as necessary
done

 

Anyone have a way to launch a script only at application launch, not by checking if it is running/open?

 

2 ACCEPTED SOLUTIONS

AJPinto
Honored Contributor III

Unfortunately, no there is not a way to do this.

  • Jamf runs policies (scripts are a payload of a policy) on checkin, and you cannot force that to happen when an application launches. 
  • As you noted the only way to do this locally is to run the script on loop as a daemon which is not the best of ideas.

Instead of using a script, you could make a Restricted Software Policy targeting OneNote. Don't Check the Kill Process box, and instead just put a message with what you want to say. When the user launches the App, the Jamf Binary will almost instantly run the Restricted Software Policy and display the chosen message, but it won't close the app.

View solution in original post

jamf-42
Valued Contributor II

you probably want a launch agent..  more here https://launchd.info/

View solution in original post

3 REPLIES 3

AJPinto
Honored Contributor III

Unfortunately, no there is not a way to do this.

  • Jamf runs policies (scripts are a payload of a policy) on checkin, and you cannot force that to happen when an application launches. 
  • As you noted the only way to do this locally is to run the script on loop as a daemon which is not the best of ideas.

Instead of using a script, you could make a Restricted Software Policy targeting OneNote. Don't Check the Kill Process box, and instead just put a message with what you want to say. When the user launches the App, the Jamf Binary will almost instantly run the Restricted Software Policy and display the chosen message, but it won't close the app.

Of course! This is what I get for overcomplicating things. Thank you.

jamf-42
Valued Contributor II

you probably want a launch agent..  more here https://launchd.info/