Skip to content

One command to turn any script into a Windows Service. Leverages NSSM for production-ready deployment: automatic restart, real-time logging with rotation, and graceful error handling—all with zero configuration. Stop manual setup. Just run the script for a reliable Windows Service.

Notifications You must be signed in to change notification settings

thenik/service-windows-create

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Windows Service Creator - Automatic Python Service Installation Tool

Automatically create and manage Windows services for Python scripts with zero configuration required.

Python Version License Platform

Overview

Windows Service Creator is a powerful Python automation tool that simplifies the process of creating Windows services from Python scripts. With a single command, transform your main.py script into a fully-functional Windows service with automatic restart, logging, and service management capabilities.

Key Features

Automatic Service Configuration

  • Zero-configuration setup - automatically detects script location, working directory, and Python interpreter
  • Intelligent naming - uses current folder name as service identifier
  • Smart defaults - production-ready configuration out of the box

Robust Service Management

  • Auto-restart on failure - ensures your service stays running 24/7
  • Configurable restart delays - prevents rapid restart loops
  • Automatic startup - service starts automatically when Windows boots
  • Graceful error handling - comprehensive error management and reporting

Professional Logging

  • Automatic log file creation - separate stdout and stderr logs
  • Log rotation support - prevents disk space issues with 10MB rotation
  • Real-time monitoring - easy access to service output and errors
  • Journal-style logging - similar to Linux systemd journal

Enterprise-Ready Features

  • NSSM integration - uses Non-Sucking Service Manager for reliability
  • Auto-download capability - fetches NSSM automatically if not installed
  • Administrator privilege detection - prevents permission-related errors
  • Service conflict detection - handles existing service scenarios gracefully

Installation

Prerequisites

  • Windows 7/8/10/11 or Windows Server 2012+
  • Python 3.6 or higher installed and added to PATH
  • Administrator privileges required for service installation

Quick Install

# Clone the repository
git clone https://github.com/thenik/service-windows-create.git
cd service-windows-create

# Install Python dependencies
pip install pywin32

# Run the service creator (as Administrator)
python service_windows_create.py

Manual NSSM Installation (Optional)

The script automatically downloads NSSM, but you can install it manually:

# Download from https://nssm.cc/download
# Extract and add to PATH or place in script directory

Usage Guide

Basic Usage

  1. Navigate to your Python project directory containing main.py:

    cd C:\path\to\your\project
  2. Run the service creator as Administrator:

    python service_windows_create.py
  3. Service is automatically created, configured, and started

Command Reference

After installation, manage your service with these Windows commands:

# Check service status
sc query your_service_name

# Start the service
net start your_service_name

# Stop the service
net stop your_service_name

# Restart the service
net stop your_service_name && net start your_service_name

# Remove the service
nssm remove your_service_name confirm

Advanced Configuration

Edit service settings using NSSM GUI:

nssm edit your_service_name

Or access Windows Services Manager:

services.msc

Configuration Options

The service is automatically configured with these settings:

Setting Default Value Description
Script Path ./main.py Python script to run as service
Working Directory Current folder Service execution directory
Python Interpreter sys.executable Active Python interpreter path
Service Name Folder name Windows service identifier
Startup Type Automatic Service starts with Windows
Restart Delay 5 seconds Time between restart attempts
Log Location ./logs/ Service output log directory
Log Rotation 10 MB Maximum log file size before rotation

Log Management

Viewing Logs

Access service logs in the automatically created logs directory:

# View standard output
type logs\your_service_name_stdout.log

# View error output
type logs\your_service_name_stderr.log

# Monitor logs in real-time (PowerShell)
Get-Content logs\your_service_name_stdout.log -Wait -Tail 50

Log Rotation

Logs automatically rotate when they reach 10MB, preventing disk space issues. Older logs are archived with timestamps.

Troubleshooting

Common Issues and Solutions

"Not running as Administrator" Error

Solution: Right-click Command Prompt or PowerShell and select "Run as administrator"

"Service already exists" Error

Solution: The script will prompt to remove and recreate the service, or manually remove it:

nssm remove your_service_name confirm

Service won't start

Solution: Check error logs in logs\your_service_name_stderr.log and verify:

  • Python script has no syntax errors
  • All dependencies are installed
  • File paths are correct

NSSM download fails

Solution: Manually download NSSM from nssm.cc and place in script directory

Comparison with Linux systemd

This tool provides Windows equivalents for systemd features:

systemd Feature Windows Equivalent
systemctl enable Automatic startup configuration
systemctl start Service start via NSSM
Restart=on-failure Auto-restart on exit codes
RestartSec=5 5-second restart delay
journalctl File-based logging with rotation
/etc/systemd/system/ Windows Service Manager

Best Practices

Production Deployment

  1. Test your Python script thoroughly before creating the service
  2. Use virtual environments - point to the venv Python executable
  3. Configure proper logging in your application
  4. Set appropriate restart policies based on your use case
  5. Monitor service health regularly using Windows Event Viewer

Security Considerations

  • Run services with least privilege accounts when possible
  • Store sensitive credentials in Windows Credential Manager
  • Enable Windows Firewall rules if service uses network ports
  • Regularly update dependencies for security patches

Performance Optimization

  • Minimize startup time for faster service recovery
  • Implement graceful shutdown handlers in your script
  • Use connection pooling for database services
  • Monitor resource usage via Task Manager or Performance Monitor

Project Structure

your-project/
├── main.py                          # Your Python application
├── service-windows-create.py                # Service creator script
├── logs/                            # Auto-generated log directory
│   ├── your_service_name_stdout.log # Standard output
│   └── your_service_name_stderr.log # Error output
└── nssm/                            # Auto-downloaded NSSM (if needed)
    └── nssm.exe

Requirements

  • Python: 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12
  • Operating System: Windows 7/8/10/11, Windows Server 2012+
  • Dependencies: pywin32
  • Privileges: Administrator access required
  • Architecture: x86 (32-bit) and x64 (64-bit) supported

Contributing

Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest new features.

Development Setup

# Fork and clone the repository
git clone https://github.com/thenik/service-windows-create.git

# Create a feature branch
git checkout -b feature/your-feature-name

# Make changes and test thoroughly

# Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Related Projects

Support

Keywords

windows service, python service, windows automation, systemd windows, service manager, python windows service, automatic service creation, nssm python, windows service installer, python daemon windows, background service windows, python script service, windows service wrapper, auto restart service, python service manager


Made with ❤️ for Python developers who need reliable Windows services

About

One command to turn any script into a Windows Service. Leverages NSSM for production-ready deployment: automatic restart, real-time logging with rotation, and graceful error handling—all with zero configuration. Stop manual setup. Just run the script for a reliable Windows Service.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages