Added readme details
This commit is contained in:
11
Program.cs
11
Program.cs
@@ -3,14 +3,17 @@ using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Diagnostics;
|
||||
|
||||
// Asking the user if they want to open new processes in a separate window
|
||||
Console.WriteLine("Do you want to start processes in a new window? (yes/no)");
|
||||
string userInput = Console.ReadLine().ToLower();
|
||||
bool openInNewWindow = userInput == "yes";
|
||||
|
||||
double checkOnce = 0;
|
||||
|
||||
// The main loop that continuously checks and manages processes
|
||||
while (true)
|
||||
{
|
||||
// Define the path to the JSON file containing process information
|
||||
string jsonFilePath = "processlist.json"; // Relative path to the JSON file
|
||||
string json = File.ReadAllText(jsonFilePath);
|
||||
var jObject = JObject.Parse(json);
|
||||
@@ -18,27 +21,32 @@ while (true)
|
||||
|
||||
foreach (var processInfo in processInfos.Where(x => x.Enable))
|
||||
{
|
||||
// Check if it's the first run and if the process has a specific interval set
|
||||
if (checkOnce == 0 && processInfo.Interval.HasValue)
|
||||
{
|
||||
// Schedule repeated execution
|
||||
ScheduleRepeatedExecution(processInfo);
|
||||
}
|
||||
// If no specific time is set, ensure the process is running continuously
|
||||
else if (string.IsNullOrEmpty(processInfo.Time))
|
||||
{
|
||||
// Run continuously
|
||||
EnsureProcessRunning(processInfo, processInfo.Count);
|
||||
}
|
||||
// If it's the first run and a specific time is set, schedule the process
|
||||
else if (checkOnce == 0)
|
||||
{
|
||||
// Schedule for a specific time
|
||||
ScheduleProcessStart(processInfo);
|
||||
}
|
||||
}
|
||||
// After the first iteration, set checkOnce to 1 to avoid rescheduling
|
||||
if (checkOnce == 0)
|
||||
checkOnce = 1;
|
||||
Thread.Sleep(10000); // Wait for 10 seconds before checking again
|
||||
}
|
||||
|
||||
// Ensures the desired number of instances of a process are running
|
||||
void EnsureProcessRunning(ProcessInfo processInfo, int desiredCount)
|
||||
{
|
||||
var runningProcesses = Process.GetProcessesByName(processInfo.Name);
|
||||
@@ -59,6 +67,7 @@ void EnsureProcessRunning(ProcessInfo processInfo, int desiredCount)
|
||||
}
|
||||
}
|
||||
|
||||
// Schedules a process to start at a specific time
|
||||
void ScheduleProcessStart(ProcessInfo processInfo)
|
||||
{
|
||||
DateTime scheduledTime = DateTime.Today.Add(TimeSpan.Parse(processInfo.Time));
|
||||
@@ -85,6 +94,7 @@ void ScheduleProcessStart(ProcessInfo processInfo)
|
||||
Console.WriteLine($"Scheduled {processInfo.Name} to start at {scheduledTime}");
|
||||
}
|
||||
|
||||
// Schedules a process for repeated execution
|
||||
void ScheduleRepeatedExecution(ProcessInfo processInfo)
|
||||
{
|
||||
var timer = new System.Threading.Timer(_ =>
|
||||
@@ -111,6 +121,7 @@ void ScheduleRepeatedExecution(ProcessInfo processInfo)
|
||||
Console.WriteLine($"Scheduled {processInfo.Name} to run every {processInfo.Interval.Value} minutes");
|
||||
}
|
||||
|
||||
// Appends a log entry for a process action
|
||||
void AppendToLogFile(ProcessInfo processInfo, string state)
|
||||
{
|
||||
string logFilePath = Path.Combine("Logs", GetLogFileName()); // Store logs in a "Logs" subfolder
|
||||
|
||||
68
README.md
68
README.md
@@ -1 +1,67 @@
|
||||
# marketally.processmonitor
|
||||
# Process Monitor Utility
|
||||
|
||||
## Overview
|
||||
|
||||
This utility is a process monitoring and management tool written in C#. It allows users to automate the starting of processes based on a predefined schedule and conditions. The program reads from a JSON file to determine which processes to manage and how to manage them.
|
||||
|
||||
## Features
|
||||
|
||||
- **Flexible Process Management**: Enables starting processes at specific times, continuously running them, or scheduling them at regular intervals.
|
||||
- **User Input for Window Management**: Users can choose whether processes should start in a new window.
|
||||
- **Logging**: Automatically logs process start and management actions in a weekly log file.
|
||||
|
||||
## Requirements
|
||||
|
||||
- .NET Framework or .NET Core
|
||||
- Newtonsoft.Json package for JSON processing
|
||||
- Marketally.ProcessMonitor library (if applicable)
|
||||
|
||||
## Installation
|
||||
|
||||
1. Ensure that .NET Framework or .NET Core is installed on your system.
|
||||
2. Include the `Newtonsoft.Json` package in your project.
|
||||
3. Add `marketally.processmonitor` library, if it's a separate dependency.
|
||||
|
||||
## Usage
|
||||
|
||||
1. **Configure Process List**: Edit the `processlist.json` file to include the list of processes you want to manage. The JSON structure should be as follows:
|
||||
|
||||
```json
|
||||
{
|
||||
"processes": [
|
||||
{
|
||||
"Name": "ProcessName",
|
||||
"Path": "ExecutablePath",
|
||||
"Enable": true/false,
|
||||
"Interval": Minutes (optional),
|
||||
"Time": "HH:mm" (optional),
|
||||
"Count": NumberOfInstances (optional)
|
||||
},
|
||||
...
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
2. **Run the Utility**: Execute the program. It will ask if processes should be started in a new window. Respond with `yes` or `no`.
|
||||
3. **Monitor Logs**: Check the `Logs` folder for weekly log files detailing the process management actions.
|
||||
|
||||
## Functions Description
|
||||
|
||||
- `EnsureProcessRunning`: Ensures the specified number of process instances are running.
|
||||
- `ScheduleProcessStart`: Schedules a process to start at a specific time.
|
||||
- `ScheduleRepeatedExecution`: Schedules a process to start at regular intervals.
|
||||
- `AppendToLogFile`: Logs actions to a weekly log file.
|
||||
- `GetLogFileName`: Generates a filename for the log based on the current week of the year.
|
||||
|
||||
## Notes
|
||||
|
||||
- The program uses a 10-second loop to continuously check and manage processes.
|
||||
- It is advisable to have error handling for reading the JSON file and managing processes.
|
||||
|
||||
## Contribution
|
||||
|
||||
Contributions to the project are welcome. Please ensure you follow the coding standards for new features.
|
||||
|
||||
---
|
||||
|
||||
This README provides a basic overview. Please read through the source code comments for more detailed information.
|
||||
Reference in New Issue
Block a user