Back to Resources

Level Verified

Linux Monitor - File Contains Script

Created by

Level

Type

Script

Category

General

Platforms
WindowsApple iOSLinux

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.

Description

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.

Script

1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot 
4# guarantee it will work in all environments. Please test before deploying 
5# to your production environment. We welcome contributions to our community 
6# library
7
8# Level Library
9# level.io/library/script-linux-monitor-file-contains
10
11# Specify the file path to check
12file_path="{{FilePath}}"
13
14# Specify the string to search for in the file
15search_string="{{SearchString}}"
16# -----------------------------------------------------------------------------
17
18# Check if the file exists
19if [ -f "$file_path" ]; then
20  if grep -q "$search_string" "$file_path"; then
21    echo "SUCCESS: The string '$search_string' exists in the file."
22  else
23    echo "ERROR: The string '$search_string' does not exist in the file."
24  fi
25else
26  echo "ERROR: File not found: $file_path"
27fi

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

FAQ

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

Included with this Script:

Below is a list of what you can expect to find when importing this Script.

Script details:

The following data and settings will be imported with your script.

Script Name

Linux Monitor - File Contains

Description

This script checks if a specified file exists and verifies whether it contains a specific string, providing a success or error message to help monitor critical file contents on a Linux system.

Language

Bash

Timeout (In Seconds)

100

Run As

Local system

Import into Level

Related resources

Explore more automations, scripts, and policies to further enhance your IT operations.

View all resources