I have a Raspberry Pi running well in headless mode. I am comfortable running things from the command line, and so have it set to boot to CLI. I use PuTTY from Windows 10 to run any commands etc.
Occasionally, however, I would like to do something from the Desktop GUI (using the mouse for a lot of drag & drop kind of tasks), and I have VNC to operate this. My problem is that I don't know how to turn off the Desktop GUI again once I'm done. Currently I have two methods available to me - one that works (but isn't ideal) is I can get on the Pi itself and run
startx
This starts the GUI, then I can connect using VNC and do whatever I need to do, and have the option from the GUI to "Exit to command line", which closes the Desktop and puts the Pi back into CLI only.
However I have to get on the Pi itself to do this, I can't start that remotely (since PuTTY isn't graphical).
So from PuTTY I have been starting the desktop service
sudo systemctl start lightdm
Which allows me to then connect to the Desktop using VNC as desired, but I no longer have the option "Exit to command line" from the Shutdown menu
I can open the PuTTY terminal and stop the service, but this does not put the Pi back into CLI - if I get on the Pi directly I'm met with a blank screen and blinking cursor and am unable to do anything from there (although a PuTTY terminal still works).
How can I remotely start the Desktop GUI for use with VNC, and be able to close that GUI again once I'm done?
Trying to do this with both RPi 4B and 3B+, so looking for answers that could work in both, but mostly need it for the 4.