I am curious as to what is the simplest way of sending an executed command (in this case, a bash script), that had been executed in a terminal emulator into a tty.
Context: I am sick and tired of the terribly uncomfortable and user-unfriendly way of using the NVIDIA GPU on an Optimus laptop. Ubuntu and its derivatives have a nice way of switching cards that involves only a relogging (eg. restarting the X session), however I don't like Ubuntu much, and all other distros I know of don't have such an utility, or it is highly imperfect (Solus configures PRIME properly upon installation of the driver, but switching GPUs is impossible without actually uninstalling the driver).
I also know about Bumblebee, but it is a bandaid fix held together by duct tape, so even if it works, I have a certain distrust and revulsion towards it (don't get me wrong, I think the project is brilliant, but I just prefer a less duct-tape-y way...).
There is a script called nvidia-xrun (https://github.com/Witko/nvidia-xrun) that almost does what I am looking for, but it is still somewhat inconvenient.
Inspired by nvidia-xrun, I wish to make a primitive shell script that implements PRIME functionality relatively well.
The simplest setup I could think of would schematically work like this:
The script stops the display manager (sudo systemctl stop gdm - for gdm and systemd).
The script renames a previously placed xorg.conf.nvidia into xorg.conf (which contains the proper xorg entry for the nvidia driver).
(I manually place a .xinitrc into ~/ which contains my DE and the two xrandr lines needed to offload the dGPU's output to the Intel card.)
The script at this point kills X, so I drop back into a logged-in tty.
The script wakes up the NVIDIA GPU via bbswitch, loads the kernel modules, and then initiates the proper X session via startx.
Upon logging out of the session, the previous startx command is finished, so the script continues - it will unload the kernel mods, shuts down the NVIDIA GPU, renames xorg.conf into xorg.conf.nvidia and does sudo systemctl start gdm, so I'm back the where I begun.
I know how to execute each and every step in this scheme, but if this script is executed from a terminal emulator, it will stop the moment I kill X, since its terminal is gone, so I would like the initial script to be executed from an arbitrary terminal emulator, but then sent to "the tty I am logged into", so that when X terminates, the script keeps running.
It is also important that the script doesn't just run in the background, but that I can interact with it - since unloading the modules, and turning on/off the GPU requires sudo, so I will need to interact with a shell to put in my password.
This is the problem I'd like to solve, but if somebody has any idea how to implement what I want differently (that hopefully goes around this issue), I am more than happy to listen.