Level Verified

Linux Monitor - File Contains Script

Quickly verify if a specific string is present in a Linux file. Perfect for script-based monitoring and auto-remediation.

Import into Level

Problem overview

Ensuring critical log messages or specific strings are present in key Linux files can be tedious without an automated check. This script helps IT Professionals and MSPs proactively detect missing entries that might indicate configuration errors, security issues, or incomplete processes, reducing manual oversight and speeding up resolution times.

Bash 100s timeout Runs as Local system Linux
#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Level Library
# level.io/library/script-linux-monitor-file-contains

# Specify the file path to check
file_path="{{FilePath}}"

# Specify the string to search for in the file
search_string="{{SearchString}}"
# -----------------------------------------------------------------------------

# Check if the file exists
if [ -f "$file_path" ]; then
  if grep -q "$search_string" "$file_path"; then
    echo "SUCCESS: The string '$search_string' exists in the file."
  else
    echo "ERROR: The string '$search_string' does not exist in the file."
  fi
else
  echo "ERROR: File not found: $file_path"
fi

This script checks whether a designated string exists within a specified file. It uses Level Script Variables for both the file path and search term, providing flexibility in various environments. If the file is found and the string exists, it returns a success message; otherwise, it reports an error.

You can easily integrate this script into a script-based monitor in Level to receive alerts whenever the string is missing. By pairing it with an automated remediation workflow, you can immediately address issues detected in your environment.

Use cases

  • Monitoring log files for critical error patterns
  • Verifying configuration files for essential directives
  • Ensuring required security settings are present in system files
  • Automating checks for newly added or changed file contents

Recommendations

  • Test the script in a non-production environment before widespread deployment
  • Use a script-based monitor in Level to trigger this script and alert if the string is missing
  • Consider building an automation in Level to run this script on a schedule if continuous checks are needed
  • Make sure the file path and string variables are accurate and up to date
  • Keep an eye on script output for “ERROR” vs “SUCCESS” messages to pinpoint issues quickly

Frequently asked questions.

How do I run this script?

Simply configure it in Level and assign the correct file path and search string as Script Variables.

What permissions are required?

This script runs with System or Root level permissions, ensuring it can read files that might otherwise be restricted.

What if the file doesn’t exist?

The script will return an “ERROR: File not found” message. Double-check the file path for typos or changes.

Can I automate remediation if the string is missing?

Yes, pair this script with an automation in Level to perform corrective actions whenever the script reports an error.

Does this script work with any file type?

As long as the file is text-based and grep can search it, this script will work as intended. Test in your environment to confirm.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.