Uncommon Bash Script Question

Hello everyone. I am working on a bash script that has a uncommon requirement. This is how it needs to perform.

1.) Open a terminal from within any Linux desktop environment.
2.) Execute a bash script to install software. (sh install.sh)
3.) Logs user out of desktop environment then drops to the CLI.
4.) Bash script continues executing its commands automatically.
5.) Computer reboots once the bash script has finished.

Can anyone please help me by citing some example bash script commands which would accomplish this goal gracefully?

Thank you so much for your time and effort. Looking forward to hearing from you all soon. Take care and have a wonderful day.

1.) Open a terminal from within any Linux desktop environment.
2.) Execute a bash script to install software. (sh install.sh)

The syntax depends on which terminal you’re opening. For example this’d open xfce-terminal and run sh install.sh

xfce4-terminal -x sh install.sh

You can check the man page of any given terminal to get the syntax as I think all of them can execute scripts after launch one way or another (ex: man xfce4-terminal).

If it needs to work with any terminal you could check for existence of each one and use the appropriate command for the first one it finds.

3.) Logs user out of desktop environment then drops to the CLI.
4.) Bash script continues executing its commands automatically.
5.) Computer reboots once the bash script has finished.

If you logout the user it’ll kill the script because it’ll be a child process of the current session. They’ll also probably be using a desktop manager like lightdm meaning they won’t “logout” to a command line, they’ll end up in a GUI running as root.

So the solution is going to vary widely depending on what they’re running. The quickest hack to accomplishing this easily this would telling the user to logout and switch to a different TTY if they’re using a desktop manager, then run your script from there.

Knowing what your objective is may help here because this seems like a very convoluted way of doing something that almost certainly should be accomplished differently. Happy to help if I could get more information.

It is a project that I am not in liberty to discuss in greater detail at the moment for security reasons. However for the sake of troubleshooting I can be more generalized in my answers.

I won’t know what DE the user is using when the script is executed. So I would need to figure out a way to first automatically identify the desktop environment when the script starts running. Then it would have to choose the right command that corresponds to the right terminal application. Honestly it would be a bit of a pain in the ass, because of how many desktop environments there are out there. Plus not everyone keeps the default terminal application for their desktop environment.

Is there a way to stop the desktop environment and drop to the command line without logging the user out? Maybe restart the computer and have the bash script start right after grub2? Thus not killing the script? I am very open to suggestions.

It was also suggested elsewhere that I use a custom systemd service to accomplish this goal. However I have no idea what they are talking about and I am still waiting for their answer. Do you have any clue as to what they were suggesting?

The issue is that if the desktop environment is running when the script starts. The script will destroy the desktop environment before it is finished executing. Resulting in a borked system that is quite difficult to fix and successfully log back into for the average user.

While I would love to have confidence in the average user of my software to be able to drop into the command line from the desktop environment. However, I have no such confidence, because there will be many new users using the software that can barely use their operating system as it is.

When you “logout” from a desktop environment you’re really just ending the process. So if you start your DE from a TTY you’ll drop to command line still logged in but if you’re using a desktop manager it’s running the DE as a separate process so you’re never really “logged in” in the conventional TTY sense, the desktop process running under your user simply ends.

The user isn’t “logged in” in the conventional TTY sense so no, once the desktop session ends then all processes started by them (as their user, not root) during that session end.

systemd is used by almost all popular Linux desktop focused OS’s and they’re suggesting you create a root service that either listens for your script starting or is started by your script so it’d survive “logout” and continues operations from there as root or running commands as a particular user.

If you need a terminal interface you could switch TTY when the user reaches the desktop manager or possibly spawn a root terminal in the interface (not sure if that can be done).

If you didn’t know what systemd is this is something you probably don’t want to create yourself (or at least for anyone else) in consideration of the risks as it’s on the higher end of technical and will be very easy to make mistakes.

Ideally you’d want to find a different way unless this is for aesthetic purposes like for a movie.