display log file in terminal window

Maclife
New Contributor III

Hey I want to achieve the following. If a certain local user logs in to open a terminal window and show the jamf.log file.

What I have done so far create a policy. with the trigger Login that executes a script that looks the following 

 

#!/bin/bash
# Check if the user is sadmin
if [ $(whoami) = "admin" ]; then
# Open a new Terminal window and run 'tail -f /var/log/jamf.log'
osascript -e 'tell app "Terminal" to do script "tail -f /var/log/jamf.log"'
fi

 

the policy is executed when user "admin" logs in and gives no errors but there is no terminal window being opened and displays the jamf.log file

I dont know where the error is or why it won't work!? How can I achieve this?

1 ACCEPTED SOLUTION

TrentO
Contributor II

One thing you have to remember when writing scripts for Jamf is that Jamf executes everything as root, not the currently logged in user. Because of this, your current script has two issues:

1) The check for the current user is going to always return root if run directly. Instead you can use this bash oneliner to get the currently logged on user.

CURRENT_USER=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print$3}')

Then you can check if the user is the admin user as normal.

if [[ "$CURRENT_USER" = "admin" ]]; then
  ...do something here
fi

2) Since the context for the script is root, when you attempt to open a UI element for the user, you will need to use sudo -u $CURRENT_USER to instead run your command to open then UI element in the user context. 

sudo -u "$CURRENT_USER" -- osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script "tail -f /var/log/jamf.log"'

All together this would be

#!/bin/bash

CURRENT_USER=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print$3}')
if [[ "$CURRENT_USER" = "admin" ]]; then
  sudo -u "$CURRENT_USER" -- osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script "tail -f /var/log/jamf.log"'
fi

 

View solution in original post

3 REPLIES 3

TrentO
Contributor II

One thing you have to remember when writing scripts for Jamf is that Jamf executes everything as root, not the currently logged in user. Because of this, your current script has two issues:

1) The check for the current user is going to always return root if run directly. Instead you can use this bash oneliner to get the currently logged on user.

CURRENT_USER=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print$3}')

Then you can check if the user is the admin user as normal.

if [[ "$CURRENT_USER" = "admin" ]]; then
  ...do something here
fi

2) Since the context for the script is root, when you attempt to open a UI element for the user, you will need to use sudo -u $CURRENT_USER to instead run your command to open then UI element in the user context. 

sudo -u "$CURRENT_USER" -- osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script "tail -f /var/log/jamf.log"'

All together this would be

#!/bin/bash

CURRENT_USER=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print$3}')
if [[ "$CURRENT_USER" = "admin" ]]; then
  sudo -u "$CURRENT_USER" -- osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script "tail -f /var/log/jamf.log"'
fi

 

Maclife
New Contributor III

cool thanks let me try that.

DBrowning
Valued Contributor II

try using 

osascript -e 'tell app "Terminal" to activate do script "tail -f /var/log/jamf.log"'