'RELIABLE way of starting and stoping xampp (Mysql, Apache, Tomcat) from a batch file without crashing mysql databases

THE ENVIRONMENT:
I am using Xampp (Mysql, Apache, Tomcat) for writing Java applications in Windows 11.

THE PROBLEM:
Everytime i forget to close xampp, and restart computer, Mysql crashes. To solve this I am using the script below:

# Based on this answer: https://stackoverflow.com/a/61859561/1956278
$loc = "D:\xampp\mysql"
$dataOld = "./data_old_$(get-date -f yyyy-MM-dd-hh-mm-ss)"
$dataOld_ibdata1 = $( $dataOld + "/ibdata1" )
$backup = "./backup"
$target = "./data"
$target_ibdata1 = $( $target + "/ibdata1" )
$targetTest = "./data/test"

Write-Output $( "Will go to dir " + $loc )
pause
Set-Location -Path $loc

Write-Output $( "Will rename data to " + $dataOld )
pause
Rename-Item -Path $target -NewName $dataOld

Write-Output $( "Will copy data from " + $backup + " to " +  $target )
pause
Copy-Item -Path $backup -Destination $target -Recurse

Write-Output $( "Will delete directory " + $targetTest )
pause
Remove-Item $targetTest -Recurse

Write-Output $( "Will restore databases except mysql,performance_schema,phpmyadmin from " + $dataOld + " to " + $target )
pause
$dbPaths = Get-ChildItem -Path $dataOld -Exclude ('mysql', 'performance_schema', 'phpmyadmin') -Recurse -Directory
Copy-Item -Path $dbPaths.FullName -Destination $target -Recurse

Write-Output $( "Will restore ibdata1 from " + $dataOld_ibdata1 + " to " + $target_ibdata1 )
pause
Copy-Item -Path $dataOld_ibdata1 -Destination $target_ibdata1 

Write-Output $( "Previous data is located at " + $dataOld )
Write-Output $( "goto http://localhost/phpmyadmin and press log-out once!" )
pause

THE NEED:
I wish not to use xampp user interface but bat files to take control of starting and stoping procedures.

HOW FAR I COME SO FAR:
By using answer "https://stackoverflow.com/questions/26941864/how-to-find-out-if-tomcat-has-stopped-running-with-batch" I tried to combine bat files.
And I wrote, start and stop procedure for xampp.
Start procedure works; however on stop procedure, even if the pid file of tomcat does not exists, I can see that tomcat is running from the xampp interface.

XAMPP START CODE:
I used "start" in the command to see the output in the command prompt.
"C:\me\codes\GWT\bat\xampp_start.bat"

@echo off
cls

Rem BASE XAMMP DIR
set xampp=d:\xampp\
echo xampp dir %xampp%

Rem MYSQL.EXE
set mysql=%xampp%mysql\bin\mysql.exe
echo mysql file %mysql%

Rem CHECK IF MYSQL IS ACTIVE ALREADY
echo \q | %mysql% -u root 2>nul
if "%ERRORLEVEL%" == "0" (
    echo mysql already running
    GOTO :mysql_already_running
) else (
    echo need to start mysql
)

Rem START MYSQL
@echo mysql_start...
start /b "MYSQL" %xampp%mysql_start.bat

Rem WAIT UNTIL MYSQL STARTED
:check_mysql_running
@echo wait5...
TIMEOUT 5

echo \q | %mysql% -u root 2>nul
if "%ERRORLEVEL%" == "0" (
    echo CONNECTION SUCCESSFUL
) else (
  echo CONNECTION FAILED
  GOTO :check_mysql_running
)

Rem BY NOW MYSQL SHOULD BE STARTED
:mysql_already_running

@echo apache_start...
start /b "APACHE" %xampp%apache_start.bat

@echo catalina_start...
start /b "TOMCAT" %xampp%catalina_start.bat

XAMPP STOP CODE:
I used "call" in the command to run sequentially.
"C:\me\codes\GWT\bat\xampp_stop.bat"

@echo off
cls

Rem BASE XAMMP DIR
set xampp=d:\xampp\
echo xampp dir %xampp%

Rem MYSQL.EXE
set mysql=%xampp%mysql\bin\mysql.exe
echo mysql file %mysql%

Rem PID FILE
set pid=%xampp%tomcat\logs\catalina.pid
echo pid file %pid%

Rem TASKLIST FILE
set tasklist=%xampp%tomcat\logs\tasklist.pid
echo tasklist file %tasklist%

Rem FETCH PID of TOMCAT
echo fetching pid id...
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in (%pid%) do (
    set /a count+=1
    set var[!count!]=%%x
)
echo pid content %var[1]%
IF [!var[1]!] == [] GOTO :tomcat_stoped

Rem CHECK TOMCAT RUNNING
:check_tomcat_running
@echo catalina_stop...
call %xampp%catalina_stop.bat

@echo wait5...
TIMEOUT 5

echo fetching tasklist...
tasklist /fi "pid eq %var[1]%" > %tasklist%
set count=0
for /f "tokens=*" %%x in (%tasklist%) do (
    set /a count+=1
    set var[!count!]=%%x
)
echo tasklist content 1 %var[1]%
echo tasklist content 2 %var[2]%
echo tasklist content 3 %var[3]%
echo tasklist content 4 %var[4]%
echo tasklist content 5 %var[5]%

echo isEmpty var[3]
IF [!var[3]!] == [] GOTO :tomcat_stoped
GOTO :check_tomcat_running

Rem TOMCAT ALREADY STOPPED
:tomcat_stoped
@echo catalina_stop has stopped

Rem STOP APACHE
call %xampp%apache_stop.bat
@echo apache_stop has stopped

Rem STOP MYSQL
call %xampp%mysql_stop.bat     
@echo mysql_stop has stopped

Rem CHECK IF MYSQL CLOSED
:check_mysql_closed
echo \q | %mysql% -u root 2>nul
if "%ERRORLEVEL%" == "0" (
    echo mysql still running...
    @echo wait5...
    TIMEOUT 5
    GOTO :check_mysql_closed
) else (
  echo mysql stopped.
)

OUTPUT OF START:
Everything looks fine...

xampp dir d:\xampp\
mysql file d:\xampp\mysql\bin\mysql.exe
need to start mysql
mysql_start...
wait5...
Diese Eingabeforderung nicht waehrend des Running beenden
Please dont close Window while MySQL is running
MySQL is trying to start
Please wait  ...
MySQL is starting with mysql\bin\my.ini (console)

Waiting for 0 seconds, press a key to continue ...2022-03-09  8:14:36 0 [Note] Using unique option prefix 'key_buffer' is error-prone
CONNECTION SUCCESSFUL future. Please use the full name 'key_buffer_size' instead.
apache_start...4:36 0 [Note] mysql\bin\mysqld (mysqld 10.4.22-MariaDB) starting as process 11472 ...
catalina_start...

C:\me\codes\GWT\bat>Diese Eingabeforderung nicht waehrend des Running beenden
Bitte erst bei einem gewollten Shutdown schliessen
Please close this command only for Shutdown
Apache 2 is starting ...

[XAMPP]: Searching for JDK or JRE HOME with reg query ...
ERROR: The system was unable to find the specified registry key or value.
. [XAMPP]: Could not find 32 bit or 64 bit JDK
. [XAMPP]: Looking for 32 bit JDK on 64 bit machine

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK
    CurrentVersion    REG_SZ    15

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK\15
    JavaHome    REG_SZ    C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK\15.0.2.7
    JavaHome    REG_SZ    C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\


[XAMPP]: Using JDK

[XAMPP]: Seems fine
[XAMPP]: Set JAVA_HOME : C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\
[XAMPP]: Set CATALINA_HOME : d:\xampp\tomcat

Using CATALINA_BASE:   "d:\xampp\tomcat"
Using CATALINA_HOME:   "d:\xampp\tomcat"
Using CATALINA_TMPDIR: "d:\xampp\tomcat\temp"
Using JRE_HOME:        "C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\"
Using CLASSPATH:       "d:\xampp\tomcat\bin\bootstrap.jar;d:\xampp\tomcat\bin\tomcat-juli.jar"
Using CATALINA_OPTS:   "-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
09-Mar-2022 08:14:46.750 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.73
09-Mar-2022 08:14:46.751 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Nov 11 2021 13:14:36 UTC
09-Mar-2022 08:14:46.751 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.73.0
09-Mar-2022 08:14:46.751 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           15.0.2+7
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            AdoptOpenJDK
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         D:\xampp\tomcat
09-Mar-2022 08:14:46.752 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\xampp\tomcat
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
09-Mar-2022 08:14:46.753 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=d:\xampp\tomcat\conf\logging.properties
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=8008
09-Mar-2022 08:14:46.754 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=d:\xampp\tomcat
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=d:\xampp\tomcat
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=d:\xampp\tomcat\temp
09-Mar-2022 08:14:46.755 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\nodejs\;C:\Program Files (x86)\Certbot\bin;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Users\tugal\AppData\Local\Microsoft\WindowsApps;C:\Users\tugal\AppData\Roaming\npm;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.2\bin;;.]
09-Mar-2022 08:14:46.783 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["https-jsse-nio-8443"]
09-Mar-2022 08:14:46.968 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
09-Mar-2022 08:14:46.976 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 402 ms
09-Mar-2022 08:14:47.000 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
09-Mar-2022 08:14:47.000 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.73]
09-Mar-2022 08:14:47.010 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [D:\xampp\tomcat\webapps\app-backup.war]
...deploy...

OUTPUT OF THE STOP:
Everything looks fine...

xampp dir d:\xampp\
mysql file d:\xampp\mysql\bin\mysql.exe
pid file d:\xampp\tomcat\logs\catalina.pid
tasklist file d:\xampp\tomcat\logs\tasklist.pid
fetching pid id...
The system cannot find the file d:\xampp\tomcat\logs\catalina.pid.
pid content
catalina_stop has stopped
apache_stop has stopped
Mysql shutdowm ...
mysql_stop has stopped
mysql still running...
wait5...

Waiting for 0 seconds, press a key to continue ...
mysql stopped.
C:\me\codes\GWT\bat>

OUTPUT AFTER STOP COMMAND:
PROBLEM: Error messages in german error says, apache, mysql is not started where i wanted them to close.

Apache konnte nicht gestartet werden
Apache could not be started
Press any key to continue . . .
MySQL konnte nicht gestartet werden
MySQL could not be started
Press any key to continue . . .
C:\me\codes\GWT\bat>

And it OUTPUT OF THE START COMMAND stucks recursively, no matter how many times i click enter:

C:\me\codes\GWT\bat>
d:\xampp>More?
d:\xampp>
C:\me\codes\GWT\bat>
d:\xampp>
C:\me\codes\GWT\bat>
d:\xampp>
...

QUESTION:
Do you know any RELIABLE way of starting and stoping xampp from a batch file without crashing mysql databases? Meaning in sequential order



Solution 1:[1]

I am fed up with batch file hell.\

The most reliable way in windows to use mysql is using services.
Hence, the windows can start/stop mysql properly when computer closes.\

I am just clicking service button on XAMPP app to make apache and mysql service.
And I am starting/stoping tomcat from console with a batch files for development purposes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tugalsan Karabacak