Jamf exit code show error exit code 1

Abhinav_mohan
New Contributor II

Hi there, 
I am trying to use Swiftdialog prompts for installing application with deferrals

I have the following code which exits out with error code 1 when deployed by jamf , but the same code exit without any issues in vscode

So i basically use this part of the code to exit

log_message()
{
    echo "$(date): $@"
}
cleanup_and_exit()
{
    # If you have temp folders/files that you want to delete as this script exits, this is the place to add that
    log_message "${2}"
    exit "${1}"
}

Write_To_Config()
{
    log_message "Writing to Plist"
    $pBuddy -c "Set :CurrentDeferralTime $currentDeferralTime" "$deferralPlist"
    $pBuddy -c "Set :TotalDeferralTime $TotalDeferralTime" "$deferralPlist"
    # $pBuddy -c "Set :TotalDeferralTimeEpoch $TotalDeferralTimeEpoch" "$deferralPlist"
    $pBuddy -c "Set :CurrentDeferralTimeEpoch $deferralDateSeconds" "$deferralPlist"  
    $pBuddy -c "Set :AllowedDeferralTime $AllowedDeferralTime" "$deferralPlist"  
    $pBuddy -c "Add :HumanReadableDeferralDate string $deferralDateReadable" "$deferralPlist"  > /dev/null 2>&1
    cleanup_and_exit 1 "Set deferral, Exiting." 
}
#####################SCRIPT#########################################
verify_config_file
check_for_deadline
check_for_active_deferral
currentDeferralTime=$(prompt_with_deferral)
# if the user presses ok i am ready
if [[ -z "$currentDeferralTime" ]]; then
    install_application
fi
#if the user presses deferral without any option , set it to defer by 1 hour
if [[ "$currentDeferralTime" == 0 ]];then
    echo "Setting deferral value to 1 hour"
    currentDeferralTime=3600
fi
deferralDateSeconds=$((unixEpochTime + currentDeferralTime ))
deferralDateReadable=$(date -j -f %s $deferralDateSeconds)
if [[ "$($pBuddy -c "Print :TotalDeferralTime" "$deferralPlist")"  == 0 ]]; then
    TotalDeferralTime=$currentDeferralTime 
else
    TotalDeferralTime=$(( $( $pBuddy -c "Print :TotalDeferralTime" "$deferralPlist" ) + currentDeferralTime ))
fi
Write_To_Config


Screenshot 2024-10-24 at 12.58.57 PM.png

Screenshot 2024-10-24 at 12.59.31 PM.png

Screenshot 2024-10-24 at 12.59.38 PM.png

First screenshot is the output from VScode
Second and third are from Jamf

I use the exact same code in VScode and JAMF , the script does everything as intended. But i see the exit status as failed in jamf and with error code 1.
Can anyone advise why is this happening and i get failed exit status in Jamf even tho the script work as intended?

6 REPLIES 6

AJPinto
Honored Contributor III

Your script is missing the shebang. You need to have #!/bin/bash (or another interpreter like zsh or python3) as the first line of a script. 

 

Also try to run the script using terminal, save it somewhere and the command is sudo sh {path to file}.

Abhinav_mohan
New Contributor II

Oh sorry i do use shebang , i only copied portion of script which is relevant to exit , entire script is 250 lines long. 
I tried executing through terminal it works fine without any failed exit status.
even pushing through jamf , script works as expected but i just see failed status when i check the log, I am trying to understand why it show failed status and fix that.

AJPinto
Honored Contributor III

What is the shebang you are using? Jamf can be a bit temperamental with some interpreters, I usually just stick to bash as stuff that works locally with others like zsh sometimes has issues with Jamf.

Shyamsundar
Contributor

When executing a script in Visual Studio Code, it runs in the user’s space. Conversely, when executing a script via JAMF, it runs in the root space.

Please verify if any functions require execution in the user’s space. If so, use the appropriate method for execution.

 

https://scriptingosx.com/2020/08/running-a-command-as-another-user/

# convenience function to run a command as the current user
# usage:
#   runAsUser command arguments...
runAsUser() {  
  if [ "$currentUser" != "loginwindow" ]; then
    launchctl asuser "$uid" sudo -u "$currentUser" "$@"
  else
    echo "no user logged in"
    # uncomment the exit command
    # to make the function exit with an error when no user is logged in
    # exit 1
  fi
}

 

acodega
New Contributor

I don't believe it's related to user space, but that's a good post to bookmark anyway.

Your Write_To_Config function is simply causing the exit code 1 because of the line

cleanup_and_exit 1 "Set deferral, Exiting."

So you'll need to decide when do you need to exit with an error and when do you not, and adjust your use of cleanup_and_exit. 

howie_isaacks
Valued Contributor II

CodeRunner is a great app to use to test your scripts.