How to Use a Bash Script to Find Crash Files on Mac

System stability is critical for IT professionals, especially those managing multiple devices or providing Managed Service Provider (MSP) services. Ensuring that devices operate smoothly without unexpected crashes can prevent downtime, maintain productivity, and uphold user satisfaction.

This guide explores a simple yet powerful script designed to detect recent system crashes on macOS. By understanding and implementing this script, IT professionals can proactively manage and troubleshoot system issues effectively.

Background

System crashes can be indicative of underlying hardware or software problems. On macOS, crash reports are generated and stored under specific directories, helping diagnose issues that caused the crash. These reports come in various file formats like .ips, .panic, and .crash, each providing different levels of detail about the incidents.

For IT professionals and MSPs, having a tool that can automatically scan for these crash reports can save significant time and effort. This script offers a straightforward solution to identify any crash files created within the last 180 days, allowing for timely intervention and resolution.

The Script

#!/usr/bin/env bash

# Description: Exits with a 1 if any crash files were created in the last 180 days. Be it .ips, .panic, or .crash under /Library/Logs/DiagnosticReports.
#
# Release Notes: Initial Release
# By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
# Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
# Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
# Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
# Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
# Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
# Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).

if [[ "${testForAppCrashes}" == "true" ]]; then
    ipsFiles=($(find "/Library/Logs/DiagnosticReports" -type f -name "*.ips" -mtime -180))
    for item in "${ipsFiles[@]}"; do
        echo "[Warn] Found ${item} ips file!"
    done
fi

panicFiles=($(find "/Library/Logs/DiagnosticReports" -type f -name "*.panic" -not -name "*.contents.panic" -mtime -180))
crashFiles=($(find "/Library/Logs/DiagnosticReports" -type f -name "*.crash" -mtime -180))

for item in "${panicFiles[@]}"; do
    echo "[Error] Found ${item} panic file!"
done

for item in "${crashFiles[@]}"; do
    echo "[Error] Found ${item} crash file!"
done

if [ ${#ipsFiles[@]} -gt 0 ] || [ ${#panicFiles[@]} -gt 0 ] || [ ${#crashFiles[@]} -gt 0 ]; then
    exit 1
else
    echo "No crash files found."
    exit 0
fi

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Detailed Breakdown

The script is written in Bash and performs the following key functions:

  1. Environment Setup: The shebang line specifies the script’s interpreter, ensuring it runs with the appropriate environment settings.
  2. Description and Release Notes: Comments provide a brief overview of the script’s purpose and its version history.
  3. Conditional Check for Application Crashes: If the environment variable testForAppCrashes is set to true, the script searches for .ips files (indicating application crashes) created in the last 180 days and lists them.
  4. Search for Panic and Crash Files: These commands locate .panic and .crash files, excluding files named *.contents.panic, within the specified timeframe.
  5. Logging Detected Files: The script outputs warnings and errors for each detected file, providing immediate feedback to the user.
  6. Exit Status Based on Findings: Depending on whether any crash files were found, the script exits with status 1 (indicating issues) or 0 (indicating no issues).

Potential Use Cases

Case Study: Proactive IT Management

An IT professional managing a fleet of Mac computers in a corporate environment could use this script to regularly check for recent crashes. By scheduling this script to run daily via a cron job, they can ensure they are immediately notified of any new crash reports. This allows for prompt troubleshooting, minimizing downtime and maintaining system reliability.

Comparisons

Script vs. Manual Check

Manually checking for crash reports involves navigating to the appropriate directories and inspecting files, which can be time-consuming and prone to error. This script automates the process, ensuring consistency and accuracy.

Script vs. Commercial Tools

While there are commercial tools available that offer comprehensive system monitoring, this script provides a cost-effective, lightweight alternative for crash detection, particularly suitable for smaller IT environments or specific use cases where a full-fledged monitoring solution is not justified.

FAQs

Q: How do I run this script?

A: Save the script to a file, make it executable with chmod +x filename.sh, and run it using ./filename.sh.

Q: What if I want to check for crashes over a different time period?

A: Modify the -mtime -180 parameter to the desired number of days.

Q: Can this script be used on non-macOS systems?

A: No, this script is specifically designed for macOS.

Implications

Regularly running this script can help identify recurring crash patterns, allowing IT professionals to address potential issues before they escalate. Detecting crashes early can prevent larger system failures and maintain optimal performance and user satisfaction.

Recommendations

  • Automate Execution: Schedule the script to run at regular intervals using cron jobs.
  • Integrate with Monitoring Tools: Combine this script with other monitoring solutions for a comprehensive approach.
  • Review Logs Regularly: Regularly review the output logs to ensure all issues are promptly addressed.

Final Thoughts

Incorporating this script into your IT management toolkit can significantly enhance your ability to maintain system stability and preemptively address issues. Tools like NinjaOne can further streamline these processes, providing a unified platform for monitoring and managing multiple devices. By leveraging such tools, IT professionals can ensure they are equipped to handle any challenges that arise, maintaining smooth and reliable operations.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).