The server provides the command, bin/stop-server, to stop the server. You can run it manually from the command line or within a script.
If the server has been configured to use a large amount of memory, then it might take several seconds for the operating system to fully release the memory and make it available again. If you try to start the server too quickly after shutting it down, then the server might fail because the system does not yet have enough free memory. On UNIX systems, run the vmstat command and watch the values in the "free" column increase until all memory held by the server is released back to the system.
You can also set a configuration option that specifies the maximum shutdown time a process might take.