This PowerShell script (monitor_script.ps1) monitors the main Multi-LLM Markdown Content Generator script (mcp_md_done.ps1) and automatically restarts it if it exits unexpectedly. It provides enhanced logging, configuration loading from .env, and basic error diagnostics to ensure the content generation process runs reliably.
- Automatic Restart: Monitors
mcp_md_done.ps1and restarts it upon exit. .envIntegration: Loads configuration (like API endpoints) from the.envfile used by the main script.- Endpoint Validation: Checks the format of
LMSTUDIO_ENDPOINTin.envand attempts to auto-correct if needed. - File Logging: Logs monitoring activities, script starts/exits, and errors to
monitor_log.txt. - Error Diagnostics: Checks the exit code of
mcp_md_done.ps1and reads the latestError_*.jsonlog file to provide basic diagnostic information (e.g., API endpoint errors). - Admin Privileges: Runs the main script with elevated privileges (
-Verb RunAs) if required by its operations. - Configurable Retries: Set maximum restart attempts and wait time between attempts.
- Windows Operating System
- PowerShell 7+ (
pwsh): Required to run both the monitor and the main script. - Administrator Privileges: The monitor script needs to be run as Administrator because it uses
-Verb RunAsto launch the main script. .envFile: A correctly configured.envfile in the same directory (seeREADME.mdfor the main script).mcp_md_done.ps1: The main content generation script must be present at the path specified withinmonitor_script.ps1.
- Ensure Requirements Met: Verify PowerShell 7+ is installed and you have the
mcp_md_done.ps1script and a configured.envfile. - Configure Script Path:
- Open
monitor_script.ps1in a text editor. - Crucially, update the
$scriptPathvariable to the correct full path of yourmcp_md_done.ps1file. - Optionally, update
$logFileif you want the log stored elsewhere.
$scriptPath = "D:\path\to\your\LMStudio-Markdown-Content-Generator\mcp_md_done.ps1" $logFile = "D:\path\to\your\LMStudio-Markdown-Content-Generator\monitor_log.txt"
- Open
- Set Execution Policy (if needed): If you haven't already, you might need to allow script execution. Open PowerShell as Administrator and run:
(Use
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-Scope Processfor temporary permission if preferred).
$scriptPath: (Required) Full path tomcp_md_done.ps1.$maxRetries: Maximum number of times the script will attempt to restartmcp_md_done.ps1(default: 100,000).$waitTime: Seconds to wait between restart attempts (default: 5).$logFile: (Required) Full path where the monitor log file (monitor_log.txt) will be created/appended.
- Open PowerShell 7 as Administrator.
- Navigate to the directory containing
monitor_script.ps1. - Execute the script:
.\monitor_script.ps1
- The script will start logging to the console and the specified
$logFile. It will then attempt to startmcp_md_done.ps1. - Leave the monitor script running. It will automatically restart
mcp_md_done.ps1if it closes, up to$maxRetriestimes.
- Press
Ctrl+Cin the PowerShell window wheremonitor_script.ps1is running.
- Initialization: Sets paths, retry limits, and wait times. Defines a logging function.
- Log Start: Writes a starting message to the log file and console.
- Load
.env: Checks for.env, loads environment variables, and validates/fixesLMSTUDIO_ENDPOINTif present. - Monitoring Loop:
- Starts
mcp_md_done.ps1usingStart-Process pwsh -Verb RunAs. - Waits for
mcp_md_done.ps1to exit usingWaitForExit(). - Logs the exit code.
- Error Check: If the exit code indicates an error (e.g., 1), it checks for the latest
Error_*.jsonfile created bymcp_md_done.ps1and logs its content, looking specifically for API endpoint issues. - Increments the retry counter.
- Waits
$waitTimeseconds. - Repeats until
$maxRetriesis reached orCtrl+Cis pressed.
- Starts
- Termination: Logs when max retries are reached or if an error occurs within the monitor script itself.
- Monitor Script Doesn't Start
mcp_md_done.ps1:- Permissions: Ensure you are running
monitor_script.ps1in PowerShell as Administrator. - Path Error: Double-check that
$scriptPathinsidemonitor_script.ps1points exactly to yourmcp_md_done.ps1file. - PowerShell 7 Not Found: Make sure
pwsh(PowerShell 7+) is correctly installed and in your system's PATH.
- Permissions: Ensure you are running
mcp_md_done.ps1Exits Immediately:- Check the
monitor_log.txtfile for the exit code and any error messages logged by the monitor. - Check for
Error_*.jsonfiles in the same directory for errors logged bymcp_md_done.ps1itself (e.g., invalid API keys, connection issues). - Verify the
.envfile configuration is correct for the selectedLLM_PROVIDER.
- Check the
- Endpoint Validation Warning/Error: If the monitor logs warnings about
LMSTUDIO_ENDPOINT, ensure it's correctly formatted in your.envfile (e.g.,http://localhost:1234/v1/chat/completions). - Log File Issues: Ensure the path specified in
$logFileis writable.
monitor_log.txt: Contains logs from themonitor_script.ps1itself (start/stop times, restarts, exit codes, basic error diagnostics).Error_*.json: Created bymcp_md_done.ps1when it encounters a critical error during file processing. Contains details about the specific error.Retry_*.log/Timeout_*.log: Created bymcp_md_done.ps1during API call retries or timeouts.- (Debug)
Search_*.txt,Content_*.txt,Summary_*.txt: Created bymcp_md_done.ps1ifDEBUG_SAVE_JSON_DUMPS=truein.env.
This project follows the MIT license, see the LICENSE file for details.
This README.md provides a comprehensive guide to understanding, installing, and using the monitoring script. You can customize the License and Contact sections with your specific information.