How to run script (.sh) files in a new terminal after connecting to Ubuntu 16.04 server via ssh?

Assuming Ubuntu to Ubuntu

  • If you have installed the relevant software in the server, you can log in remotely with ssh -X and then run graphical application programs like terminator and gnome-terminal. See this link,

    What is the simplest way to have remote GUI access to Ubuntu 16.04 “server” from Ubuntu 16.04 “desktop”?

  • You can also simply start other terminal windows locally, and in those windows login remotely with ssh and that way run several text mode application programs in the server (each one in its own terminal window).

If you use Windows 10 desktop and want to connect to your ubuntu server

A simple solution is to install and use Putty in Windows according to the following link,

www.putty.org/

PuTTY is an SSH and telnet client, developed originally by Simon Tatham for the Windows platform. PuTTY is open source software that is available with source code and is developed and supported by a group of volunteers.

You can download PuTTY here.

You can start one or more Putty windows and run different tasks via ssh.


As described in a comment by @SergiyKolodyazhnyy, you can use xrdp to open remote desktop session from Windows to Ubuntu.

As described in a comment by @PerlDuck, you can install and use an X server in Windows to be able to run graphical application programs via ssh, but according to your original question, it might be overkill.


Here's the thing: terminator and gnome-terminal are GUI apps. If your script doesn't require GUI and is a simple shell script, then you could run it in your ssh session just fine without needing a terminal emulator. Of course, your script needs to live on the filesystem where you're trying to run the script.

If for some reason you absolutely need terminator or gnome-terminal, you could always make use of xrdp to start remote desktop session. That's of course if the Ubuntu system you're trying to access has X server at all; server computers for instance often don't have any GUI because it's a security risk.


It is possible to run an application that requires GUI from the SSH session into the Desktop session if you really need it. I'm using the following approach to launch VMWare virtual machines when I need them but I'm not on the front of the computer.

I would emphasise you've mentioned that you are connecting to Ubuntu Server, that doesn't have Desktop environment installed by default. And in this case it is worth to use tmux or screen, or push the script into the background, or use a second SSH session. If a Desktop environment is installed into the server the following steps could be applied.

The following scripts works with Lightdm and Unity which are default for Ubuntu 16.04.


1. First requirement is that your user must be logged-in in Desktop session. That I'm using to achieve this is the following script (source and explanations):

#!/bin/bash

# NAME: lightdm-auto-login

main() {
    # If the file '/etc/lightdm/lightdm.conf' exists create a backup copy
    [[ -f /etc/lightdm/lightdm.conf ]] && mv /etc/lightdm/lightdm.conf{,.bak}

    # Create autologin configuration for the current $USER = $1
    echo -e "[Seat:*]\nautologin-user=$1" > /etc/lightdm/lightdm.conf

    # Restart 'lightdm' while autologin option is enabled
    systemctl restart lightdm.service

    # Wait for a moment to complete the login process and remove the conf file
    sleep 30 && rm /etc/lightdm/lightdm.conf

    # Restore the backup if exists
    [[ -f /etc/lightdm/lightdm.conf.bak ]] && mv /etc/lightdm/lightdm.conf{.bak,}
}

# Execute the 'main()' function with root privileges in the background 'sudo -b'
# Pass the curent $USER as arg (https://unix.stackexchange.com/a/269080/201297)
sudo -b bash -c "$(declare -f main); main $USER"
  • The script should be executed as regular user (that belongs to the sudoers group).

  • I would prefer to place the script in /usr/local/bin to be accessible as shell command system wide. Don't forgot to make it executable.


2. Second, few environment variables (as $DISPLAY, etc.) must be exported from the Desktop session into the SSH session. The following script will do that and also will launch the commands that are passed as positional parameters (source and explanations):

#!/bin/bash -e

# NAME: gui-launcher

# Check whether the user is logged-in
while [ -z "$(pgrep gnome-session -n -U $UID)" ]; do sleep 3; done

# Export the current desktop session environment variables
export $(xargs -0 -a "/proc/$(pgrep gnome-session -n -U $UID)/environ")

# Execute the input command
nohup "$@" >/dev/null 2>&1 &

exit 0
  • The script will work until the user is logged-in, including a locked screen.

  • I would prefer to place the script in /usr/local/bin to be accessible as shell command system wide. Don't forgot to make it executable.


3. Usage:

  • Establish SSH session;
  • Execute lightdm-auto-login;
  • Execute gui-launcher <commands or script>, for example:

    gui-launcher gnome-terminal -x bash -c "<my command or script>; exec bash"
    

    Note the last sub command exec bash will keep the launched gnome-terminal open, after the previous command is finish.


4. Demonstration:

enter image description here


5. References and more examples:

  • https://github.com/pa4080/cron-gui-launcher