Skip to main content
Question

Jamf exit code show error exit code 1


Forum|alt.badge.img+4

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

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?

AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • October 24, 2024

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}.


Forum|alt.badge.img+4

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
Forum|alt.badge.img+26
  • Legendary Contributor
  • October 24, 2024
Abhinav_mohan wrote:

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.


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
Forum|alt.badge.img+13

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
Forum|alt.badge.img+15
  • Valued Contributor
  • October 24, 2024

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
Forum|alt.badge.img+23

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings