Hey there, it’s Mohamed KADI, and today I want to share with you a challenge I faced while managing my WordPress site and how I resolved it using a simple yet effective solution.
The Problem: Auto-Generated Files with Page URL Names
As a web developer, maintaining a WordPress site comes with its set of challenges. Recently, I encountered a significant problem – auto-generated files were affecting the accessibility of my original URLs. When these files were created, attempting to access example.com/page-name/ resulted in a “Not Found” message, impacting the overall quality of my website and causing me to lose valuable visitors.
The Solution: A Bash Script for Silent Cleanup
To address this issue, I crafted a bash script that silently checks and deletes files matching specific names. The script is designed to run in the background, continuously checking for these auto-generated files and cleaning them up without bothering me with notifications.
Here’s a glimpse of the script:
#!/bin/bash # List of files to delete filesToDelete=( "www.example.com" "services" "activities" "contact-us" "apartments" "gallery" "f-a-qs" "team" ) # Get the current directory directory="$(dirname "$0")" while true; do for file in "${filesToDelete[@]}"; do filePath="$directory/$file" # Check if the file exists before attempting to delete if [ -e "$filePath" ]; then # Attempt to delete the file rm "$filePath" > /dev/null 2>&1 fi done # Sleep for 5 seconds before checking again sleep 5 done
This script operates in the background, silently removing files with names corresponding to the URLs of specified pages.
How to Use the Script:
- Adjust the Configuration: Modify the
filesToDelete
array to include the names of files you want to delete. - Make it Executable: Ensure the script has executable permissions:
-
chmod +x yourscript.sh
-
- Run in Background: Execute the script and run it in the background:
nohup bash yourscript.sh > /dev/null 2>&1 &
Checking Background Processes:
Wondering how to keep tabs on the background processes? You can use the
ps
command:ps aux | grep yourscript.sh
This will display information about the running process, including the process ID (PID).
Terminating a Background Process:
To stop the script, find its PID using the
ps
command, and then terminate it:kill -9 PID
This ensures a clean termination, allowing the script to perform necessary cleanup operations.