Back to Resources
Level
Script
General
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.
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.
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
Linux Monitor - File Contains
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.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.