Getting Fixed with exit code 2 on a perfectly fine and tested fixlet

When I’m trying to execute a fixlet for Disk cleanup in Linux, I’m encountering a strange error. this fixlet has been tested on personal lab and was working absolutely fine with “fixed 0” status.

but when I’m Trying to run this in an environment, I’m getting “Fixed 2” and no output file is being created. only thing which I’m getting in the error folder is the bash script which we are creating on the target server. When I’m running this bash script manually on the target server, it is running perfectly fine and getting me the desired results.

I’m pasting the action script, it’s a bit long as there are 2 conditions in this. both the conditions are giving me this issue. Any help would be appreciated!

action parameter query "filesystem" with description "Please enter the name of filesystem (eg: /var)" with default value ""
action parameter query "ErrorFolder" with description "Please enter the name of folder and path" with default value ""

// Enter your action script here



if { not exists folder (parameter "ErrorFolder")}


folder create {parameter "ErrorFolder"}


endif


if {parameter "filesystem" of action = "/var"}


delete __createfile

createfile until EOF

#!/bin/bash


# Threshold for filesystem utilization (in percentage)

THRESHOLD=2


filesystem={parameter "filesystem" of action}






if [ "$filesystem" == "/var" ]; then

    echo "You have selected $filesystem." > {parameter "ErrorFolder"}/eventmanager.txt


    # Step 1: Check filesystem utilization for /var

    echo "Checking filesystem utilization for $filesystem..." >> {parameter "ErrorFolder"}/eventmanager.txt

    utilization=$(df -h $filesystem | awk 'NR==2 {{print $5}' | sed 's/%//')


    if [ $? -ne 0 ]; then

        echo "Error: Failed to check filesystem utilization." >> {parameter "ErrorFolder"}/eventmanager.txt

		cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    fi


    echo "Current utilization of $filesystem is $utilization%." >> {parameter "ErrorFolder"}/eventmanager.txt


    # Proceed only if utilization is above the threshold

    if [ $utilization -lt $THRESHOLD ]; then

        echo "Filesystem utilization is below the threshold ($THRESHOLD%). No action needed." >> {parameter "ErrorFolder"}/eventmanager.txt

		cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/output.txt"

        exit 0

    else

        echo "Filesystem utilization exceeds $THRESHOLD%. Proceeding with the cleanup steps..." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 2: Navigate to /var directory

    echo "Navigating to $filesystem directory..." >> {parameter "ErrorFolder"}/eventmanager.txt

    cd $filesystem || {{ echo "Error: Failed to change directory to $filesystem."; }

    cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"



    # Step 3: Check for high disk usage in specific directories at any depth

    echo "Checking for large files within the $filesystem directory at any depth..." >> {parameter "ErrorFolder"}/eventmanager.txt


    # List of file patterns to check

    file_patterns=(

        "qualys-cloud-agent.log.*"

        "log.*.txt"

        "primary_db.sqlite"

        "messages-202*"

    )


    largest_file=""

    largest_size=0


    # Step 4: Find the largest file among the selected patterns

    for pattern in "${{file_patterns[@]}"; do

        # Use find to search for the files at any depth

        found_files=$(find "$filesystem" -type f -name "$pattern")

        for file in $found_files; do

            size=$(du -sb "$file" | cut -f1)

            if [ "$size" -gt "$largest_size" ]; then

                largest_size=$size

                largest_file=$file

            fi

        done

    done


    if [ -z "$largest_file" ]; then

        echo "Error: No large files found." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100 

    else

        echo "The largest file found is: $largest_file (size: $largest_size bytes)" >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 5: Dynamically check which filesystem has sufficient space for backup

    echo "Checking for available space on all filesystems for backup..." >> {parameter "ErrorFolder"}/eventmanager.txt


    backup_destination=""

    while read -r line; do

        mount_point=$(echo "$line" | awk '{{print $6}')

        free_space=$(echo "$line" | awk '{{print $4}' | sed 's/G//')


        # Check if free space is sufficient

        if [ "$(echo "$free_space >= ($largest_size / 1024 / 1024 / 1024)" | bc)" -eq 1 ]; then

            backup_destination=$mount_point

            break

        fi

    done < <(df -h | grep '^/dev')


    if [ -z "$backup_destination" ]; then

        echo "Error: No filesystem has enough free space for the backup." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    else

        echo "Backup will be taken on filesystem mounted at $backup_destination." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 6: Take a backup of the selected file

    backup_file="$backup_destination/$(basename "$largest_file")_$(date +%Y%m%d)"

    echo "Backing up $largest_file to $backup_file..."

    cp "$largest_file" "$backup_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to backup $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    else

        echo "Backup successful at $backup_file." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 7: Truncate the original file

    echo "Truncating the file $largest_file (size: $largest_size bytes)..." >> {parameter "ErrorFolder"}/eventmanager.txt

> "$largest_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to truncate $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    else

        echo "File truncated successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 8: Gzip the truncated file

    echo "Compressing the file $largest_file..." >> {parameter "ErrorFolder"}/eventmanager.txt

    gzip "$largest_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to compress $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt 

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100 

    else

        echo "File compressed successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 9: Move the compressed file to the old directory

    old_dir="$filesystem/old"

    if [ ! -d "$old_dir" ]; then

        echo "Creating old directory at $old_dir..." >> {parameter "ErrorFolder"}/eventmanager.txt

        mkdir -p "$old_dir"

    fi


    compressed_file="${{largest_file}.gz"

    echo "Moving compressed file to $old_dir..." >> {parameter "ErrorFolder"}/eventmanager.txt

    mv "$compressed_file" "$old_dir"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to move $compressed_file to $old_dir." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    else

        echo "File moved successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 10: Check filesystem utilization again after cleanup

    echo "Checking filesystem utilization for $filesystem again..." >> {parameter "ErrorFolder"}/eventmanager.txt

    utilization=$(df -h "$filesystem" | awk 'NR==2 {{print $5}' | sed 's/%//')

    if [ $? -ne 0 ]; then

        echo "Error: Failed to check filesystem utilization." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

        exit 100

    fi


    echo "Current utilization of $filesystem after cleanup is $utilization%." >> {parameter "ErrorFolder"}/eventmanager.txt

    # Output messages based on the final utilization

    if [ $utilization -lt $THRESHOLD ]; then

        echo "Filesystem utilization is now below the threshold ($THRESHOLD%). Cleanup was successful." >> {parameter "ErrorFolder"}/eventmanager.txt

    else

        echo "Filesystem utilization is still above the threshold ($THRESHOLD%). Further action may be required." >> {parameter "ErrorFolder"}/eventmanager.txt
	  cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"
	  exit 100

    fi


else

    echo "Error: Unsupported filesystem. This script is only for /var." >> {parameter "ErrorFolder"}/eventmanager.txt

    cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/error.txt"

    exit 100

fi


cat "{parameter "ErrorFolder"}/eventmanager.txt" > "{parameter "ErrorFolder"}/output.txt"


EOF

delete "/{parameter "ErrorFolder"}/memory.sh"

move __createfile "/{parameter "ErrorFolder"}/memory.sh"

run chmod 775 "/{parameter "ErrorFolder"}/memory.sh"

wait /bin/sh /{parameter "ErrorFolder"}/memory.sh




// Case 2: Handling the /var/log filesystem

else if {parameter "filesystem" of action = "/var/log"}


delete__createfile

createfile until EOF

#!/bin/bash


# Threshold for filesystem utilization (in percentage)

THRESHOLD=2


filesystem={parameter "filesystem"}


# Check if the input is /var/log

if [ "$filesystem" == "/var/log" ]; then

    echo "You have selected $filesystem." > {parameter "ErrorFolder"}/eventmanager.txt


    # Step 1: Check filesystem utilization for /var/log

    echo "Checking filesystem utilization for $filesystem..." >> {parameter "ErrorFolder"}/eventmanager.txt

    utilization=$(df -h $filesystem | awk 'NR==2 {{print $5}' | sed 's/%//')


    if [ $? -ne 0 ]; then

        echo "Error: Failed to check filesystem utilization." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt   

        exit 100

    fi


    echo "Current utilization of $filesystem is $utilization%." >> {parameter "ErrorFolder"}/eventmanager.txt


    # Proceed only if utilization is above the threshold

    if [ $utilization -lt $THRESHOLD ]; then

        echo "Filesystem utilization is below the threshold ($THRESHOLD%). No action needed." >> {parameter "ErrorFolder"}/eventmanager.txt

        exit 0

    else

        echo "Filesystem utilization exceeds $THRESHOLD%. Proceeding with the cleanup steps..." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 2: Navigate to /var/log directory

    echo "Navigating to $filesystem directory..." >> {parameter "ErrorFolder"}/eventmanager.txt

    cd $filesystem || {{ echo "Error: Failed to change directory to $filesystem."; }


    # Step 3: Check for specified files

    echo "Checking for specified files within the $filesystem directory..." >> {parameter "ErrorFolder"}/eventmanager.txt


    # List of file patterns to check

    file_patterns=(

        "qualys-cloud-agent.log.*"

        "messages-202*"

    )


    largest_file=""

    largest_size=0


    # Step 4: Find the largest file among the selected patterns

    for pattern in "${{file_patterns[@]}"; do

        # Use find to search for the files at any depth

        found_files=$(find $filesystem -type f -name "$pattern")

        for file in $found_files; do

            size=$(du -sb "$file" | cut -f1)

            if [ "$size" -gt "$largest_size" ]; then

                largest_size=$size

                largest_file=$file

            fi

        done

    done


    if [ -z "$largest_file" ]; then

        echo "Error: No large files found."

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else
	echo "The largest file found is: $largest_file (size: $largest_size bytes)" >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 5: Dynamically check which filesystem has sufficient space for backup

    echo "Checking for available space on all filesystems for backup..." >> {parameter "ErrorFolder"}/eventmanager.txt


    backup_destination=""

    while read -r line; do

        mount_point=$(echo $line | awk '{{print $6}')

        free_space=$(echo $line | awk '{{print $4}' | sed 's/G//')


        # Check if free space is sufficient

        if [ "$(echo "$free_space >= ($largest_size / 1024 / 1024 / 1024)" | bc)" -eq 1 ]; then

            backup_destination=$mount_point

            break

        fi

    done < <(df -h | grep '^/dev')


    if [ -z "$backup_destination" ]; then

        echo "Error: No filesystem has enough free space for the backup." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else

        echo "Backup will be taken on filesystem mounted at $backup_destination." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 6: Take a backup of the selected file

    backup_file="$backup_destination/$(basename $largest_file)_$(date +%Y%m%d)" # Removed .bak extension

    echo "Backing up $largest_file to $backup_file..." >> {parameter "ErrorFolder"}/eventmanager.txt

    cp "$largest_file" "$backup_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to backup $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else

        echo "Backup successful at $backup_file." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 7: Truncate the original file

    echo "Truncating the file $largest_file (size: $largest_size bytes)..." >> {parameter "ErrorFolder"}/eventmanager.txt

> "$largest_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to truncate $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else

        echo "File truncated successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 8: Gzip the truncated file

    echo "Compressing the file $largest_file..." >> {parameter "ErrorFolder"}/eventmanager.txt

    gzip "$largest_file"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to compress $largest_file." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else

        echo "File compressed successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 9: Move the compressed file to the old directory

    old_dir="$filesystem/old"

    if [ ! -d "$old_dir" ]; then

        echo "Creating old directory at $old_dir..." >> {parameter "ErrorFolder"}/eventmanager.txt

        mkdir -p "$old_dir"

    fi


    compressed_file="${{largest_file}.gz"

    echo "Moving compressed file to $old_dir..." >> {parameter "ErrorFolder"}/eventmanager.txt

    mv "$compressed_file" "$old_dir"

    if [ $? -ne 0 ]; then

        echo "Error: Failed to move $compressed_file to $old_dir." >> {parameter "ErrorFolder"}/eventmanager.txt

  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

        exit 100

    else

        echo "File moved successfully." >> {parameter "ErrorFolder"}/eventmanager.txt

    fi


    # Step 10: Collecting artifacts

    echo "Collecting artifacts..." >> {parameter "ErrorFolder"}/eventmanager.txt

    df -h "$filesystem"

    date


    # Step 11: Check disk utilization again after cleanup

    echo "Checking disk utilization again after cleanup..." >> {parameter "ErrorFolder"}/eventmanager.txt

    new_utilization=$(df -h $filesystem | awk 'NR==2 {{print $5}' | sed 's/%//')

    if [ "$new_utilization" -lt "$THRESHOLD" ]; then

        echo "After cleanup, filesystem utilization is below the threshold ($THRESHOLD%)." >> {parameter "ErrorFolder"}/eventmanager.txt

    else

        echo "After cleanup, filesystem utilization is still above the threshold ($THRESHOLD%). Current utilization: $new_utilization%." >> {parameter "ErrorFolder"}/eventmanager.txt
	  cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt
	  exit 100

    fi


    echo "Process completed for $filesystem." >> {parameter "ErrorFolder"}/eventmanager.txt


else

    echo "Error: Unsupported filesystem. This script is only for /var/log." >> {parameter "ErrorFolder"}/eventmanager.txt

    cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/error.txt

    exit 100

fi


cat "{parameter "ErrorFolder"}/eventmanager.txt" > {parameter "ErrorFolder"}/output.txt


EOF

delete "/{parameter "ErrorFolder"}/memory2.sh"

move __createfile "/{parameter "ErrorFolder"}/memory2.sh"

run chmod 775 "/{parameter "ErrorFolder"}/memory2.sh"

wait /bin/sh /{parameter "ErrorFolder"}/memory2.sh



endif

I think you’d need to start by troubleshooting this the same way I would - by simplifying. Reduce the Bash script to just one or two lines, remove all the logic in it and just create an output file. See whether that runs as expected. What we try to do there is determine whether BigFix is failing to run the bash script at all, or whether the script is not operating as you expect.

Once you verify the bash script is launching, you can start adding pieces back to it until you find which part breaks.

Got it!!

Apparently there was some issue with the wait command and it was not executing the bash script in this particular environment.

i replaced it with:

wait /bin/bash -c ". /{parameter "ErrorFolder"}/memory.sh"

It is working now.

1 Like

Glad you got it sorted out!

I’m finding more distributions are changing their default system shells away from ‘bash’ to instead use lighter shells like ‘dash’. The default user shells are often configured to /bin/bash directly to give the convenience of bash, but often /bin/sh is linked to a lighter shell instead of the link to /bin/bash to which we may be accustomed.