watch-spring/README.md

109 lines
3.9 KiB
Markdown
Raw Normal View History

2024-08-23 14:11:38 +02:00
# watch-spring
`watch-spring` is a Windows service written in Go that monitors a specified directory for changes and executes a script when changes are detected. This project is intended for automating tasks such as running backup jobs or deploying files when changes occur in a specified directory.
## Features
- **Directory Monitoring**: Monitors a specified directory for file changes (specifically write operations).
- **Script Execution**: Automatically executes a specified script when changes are detected in the monitored directory.
- **Windows Service**: Runs as a Windows service, allowing the process to run in the background, even when no user is logged in.
- **Logging**: Supports logging with different levels of verbosity.
## Installation
1. **Build the Windows Service Executable**:
- Ensure you have Go installed on your machine.
- Clone the repository and navigate to the project directory.
- Build the service executable:
```sh
go build -o watch-spring.exe ./cmd
```
2. **Create the Windows Service**:
- Use the built executable to create a Windows service using the `sc` command:
```sh
sc create "WatchSpringService" binPath= "C:\path\to\watch-spring.exe --dir C:\path\to\watch --script C:\path\to\script.bat --name BackupService"
```
## Usage
### Command-Line Flags
| Flag | Description | Example |
|------------|----------------------------------------------------------|--------------------------------------|
| `--dir` | Directory to monitor for changes. | `--dir "C:\backup\ovh5win2oneshot"` |
| `--script` | Script to execute when a change is detected. | `--script "C:\scripts\rclone-deploy.bat"` |
| `--name` | Name of the service (used for logging and identification). | `--name "BackupService"` |
| `--log-level` | Log verbosity level (0 = Panic, 1 = Fatal, 2 = Error, 3 = Warn, 4 = Info, 5 = Debug, 6 = Trace). | `--log-level 4` |
### Example
To create and run a Windows service that monitors the `C:\backup\ovh5win2oneshot` directory and executes the `rclone-deploy.bat` script when changes are detected, use the following command:
```sh
C:\path\to\watch-spring.exe --dir "C:\backup\ovh5win2oneshot" --script "C:\scripts\rclone-deploy.bat" --name "BackupService" --log-level 4
```
### Running the Service
After creating the service, it can be managed using standard Windows service commands:
- **Start the service**:
```sh
sc start WatchSpringService
```
- **Stop the service**:
```sh
sc stop WatchSpringService
```
- **Delete the service**:
```sh
sc delete WatchSpringService
```
## Logging
Logging is handled using [logrus](https://github.com/sirupsen/logrus), providing different levels of verbosity. The log level can be set using the `--log-level` flag. By default, the log level is set to `Info` (level 4).
## Development
### Prerequisites
- Go 1.16+ (for building the service)
### Building the Project
To build the project, run:
```sh
go build -o watch-spring.exe ./cmd
```
### Running the Service in Interactive Mode
While developing, you might want to run the service in interactive mode to see logs directly in the console. Simply omit the service installation steps and run the executable with the desired flags.
```sh
watch-spring.exe --dir "C:\path\to\monitor" --script "C:\path\to\script.bat" --name "TestService" --log-level 5
```
## Contributing
Contributions are welcome! If you have any ideas, suggestions, or bug reports, feel free to open an issue or submit a pull request.
## License
This project is licensed under the License - see the [LICENSE](LICENSE) file for details.
---
This README should give a comprehensive overview of your project, including installation, usage, and development instructions. If you need more specific details or additional sections, feel free to ask!