Apr 15, 2025

Windows 10/11 : Batch File to Start and Check Windows Services

Create a batch file to check the status of a Windows Service(s) and start it if its not started. The batch file can also be run inside "Task Scheduler" at an interval basis.


1. First, you need to find out the Service Name that you want to monitor, in this example let's say the service you need to monitor is an Exchange Server IMAP4 service.


2. Open --> Services and browse through the list until you found the IMAP4 service.


3. Right-click the Service Name --> Properties.


4. Take note of the "Service name:", copy the name to Notepad. As per below screenshot, the service name is "MSExchangeImap4".



5. Create a batch file as below :-


@ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A RETRY_IMAP=0
SET /A MAX_RETRIES=3
SET EXCH_IMAP= MSExchangeImap4

REM To check current status of the service.
:check_imap
SC QUERY %EXCH_IMAP% | FIND "RUNNING" >NUL
IF %ERRORLEVEL%==0 (
    ECHO Exchange IMAP4 Service is Running.
        GOTO end
)

REM To start the service if the status is stopped.
ECHO Exchange IMAP4 Service is Stopped.
SC START %EXCH_IMAP%
TIMEOUT /T 10 >NUL

REM To re-check the service status again for confirmation.
SC QUERY %EXCH_IMAP% | FIND "RUNNING" >NUL
IF %ERRORLEVEL%==0 (
    ECHO Exchange IMAP4 Service is Running.
        GOTO end
)

REM Retrying to start the service with a maximum of 3 retries only.
SET /A RETRY_IMAP+=1
IF !RETRY_IMAP! LSS %MAX_RETRIES% (
    ECHO Retrying Start %EXCH_IMAP% (Attemp: !RETRY_IMAP!).
    GOTO check_imap
) ELSE (
    ECHO Max Retries Reached for %EXCH_IMAP%, will stop retrying.
        GOTO end
)

:end
EXIT



6. You can now test run the batch file and create a "Task Scheduler" to run the check at an interval basis (eg. every 2 hours or at Startup of the server).



!!! HAPPY COMPUTING !!!

Ubuntu : Nginx Proxy Manager (NPM)

Nginx Proxy Manager (NPM) is a user friendly, open source tool that simplifies the management of Nginx Reverse Proxy configurations. It have a Web UI for creating and managing Proxy Hosts, SSL Certificates and User Management and Permissions.

NPM makes managing services exposed to the Internet easily and securely. For easy installation and consistent environment, NPM runs on Docker.

Let's Get Started !

1. Install Docker's GPG Key.

sudo apt install ca-certificates curl gnupg -y


2. Modify the permission for "keyrings".

sudo install -m 0755 -d /etc/apt/keyrings


3. Download Docker's GPG Key.


sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc



4. Grant required permissions.

sudo chmod a+r /etc/apt/keyrings/docker.asc


5. Add Docker to the Repository.

echo \


"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \



$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \


sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


6. Update the Repository.

sudo apt update


7. Install Docker.


sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y



8. Test Docker installation.

sudo docker run hello-world


9. Enable and Start Docker.

sudo systemctl enable docker

sudo systemctl start docker


10. Add "docker" to User group.

sudo usermod -aG docker $USER


11. Install Docker Compose.


sudo curl -SL https://github.com/docker/compose/releases/download/v2.34.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose



12. Modify Docker Compose Permission.

sudo chmod +x /usr/local/bin/docker-compose


13. Test the Docker Compose.

sudo docker-compose


14. Create a installation folder for Nginx Proxy Manager.

sudo mkdir ~/nginx-proxy


15. Change directory into the newly created folder.

cd ~/nginx-proxy


16. Create sub-folders inside "nginx-proxy" folder.

sudo mkdir {data,letsencrypt}


17. Create Docker Compose file inside "nginx-proxy" folder.

sudo nano docker-compose.yml


18. Copy and Paste the following codes into the file. You might want to check for the latest configuration at (https://nginxproxymanager.com/setup/#using-mysql-mariadb-database)


services:
    app:
        image: 'jc21/nginx-proxy-manager:latest'
        restart: unless-stopped
        ports:
            # These ports are in format <host-port>:<container-port>
            - '80:80' # Public HTTP Port
            - '443:443' # Public HTTPS Port
            - '81:81' # Admin Web Port
            # Add any other Stream port you want to expose
            # - '21:21' # FTP
        environment:
            # Mysql/Maria connection parameters:
            DB_MYSQL_HOST: "db"
            DB_MYSQL_PORT: 3306
            DB_MYSQL_USER: "npm"
            DB_MYSQL_PASSWORD: "npm"
            DB_MYSQL_NAME: "npm"
            # Uncomment this if IPv6 is not enabled on your host
            # DISABLE_IPV6: 'true'
        volumes:
            - ./data:/data
            - ./letsencrypt:/etc/letsencrypt
        depends_on:
            - db

    db:
        image: 'jc21/mariadb-aria:latest'
        restart: unless-stopped
        environment:
            MYSQL_ROOT_PASSWORD: 'npm'
            MYSQL_DATABASE: 'npm'
            MYSQL_USER: 'npm'
            MYSQL_PASSWORD: 'npm'
            MARIADB_AUTO_UPGRADE: '1'
        volumes:
            - ./mysql:/var/lib/mysql



19. Create Docker Network for Nginx Proxy Manager.

sudo docker network create npm-nw


20. Start Nginx Proxy Manager.

sudo docker-compose up -d


21. Check the Docker Status.

sudo docker ps


22. Open favorite browser (eg. Google Chrome) and browse the following address. You should be able to see a Nginx Proxy Manager Welcome Page.

http://[IP Address]


23. Now login to Nginx Proxy Manager Admin Web UI, please note the port number is 81.

http://[IP Address]:81


24. Login with the default Username and Password as below.


Default Username    : admin@example.com

Default Password    : changeme



25. After successfully login, you will be prompted to change the Username and Password, please proceed to change it accordingly and remember the new Username and Password.



!!! HAPPY COMPUTING !!!