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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite












I tried many ways such as



terminator -x abc.sh
The above command gives me this error:



You need to run terminator in an X environment. Make sure $DISPLAY is properly set


I also tried



gnome-terminal -x ./abc.sh


The above command gives me the error below



Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Failed to parse arguments: Cannot open display:






share|improve this question




















  • Use screen or similar or use & script.sh &
    – Panther
    May 26 at 12:45














up vote
2
down vote

favorite












I tried many ways such as



terminator -x abc.sh
The above command gives me this error:



You need to run terminator in an X environment. Make sure $DISPLAY is properly set


I also tried



gnome-terminal -x ./abc.sh


The above command gives me the error below



Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Failed to parse arguments: Cannot open display:






share|improve this question




















  • Use screen or similar or use & script.sh &
    – Panther
    May 26 at 12:45












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I tried many ways such as



terminator -x abc.sh
The above command gives me this error:



You need to run terminator in an X environment. Make sure $DISPLAY is properly set


I also tried



gnome-terminal -x ./abc.sh


The above command gives me the error below



Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Failed to parse arguments: Cannot open display:






share|improve this question












I tried many ways such as



terminator -x abc.sh
The above command gives me this error:



You need to run terminator in an X environment. Make sure $DISPLAY is properly set


I also tried



gnome-terminal -x ./abc.sh


The above command gives me the error below



Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Failed to parse arguments: Cannot open display:








share|improve this question











share|improve this question




share|improve this question










asked May 26 at 12:40









Avery

132




132











  • Use screen or similar or use & script.sh &
    – Panther
    May 26 at 12:45
















  • Use screen or similar or use & script.sh &
    – Panther
    May 26 at 12:45















Use screen or similar or use & script.sh &
– Panther
May 26 at 12:45




Use screen or similar or use & script.sh &
– Panther
May 26 at 12:45










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










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.






share|improve this answer






















  • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
    – Avery
    May 26 at 13:44






  • 1




    @Avery Then you need to install an X server on Windows, e.g. xming.
    – PerlDuck
    May 26 at 14:21







  • 1




    That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
    – Sergiy Kolodyazhnyy
    May 27 at 9:02










  • It should work with Putty from one or more command line window(s) in Windows.
    – sudodus
    May 27 at 9:22

















up vote
2
down vote













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.






share|improve this answer



























    up vote
    1
    down vote













    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






    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1040578%2fhow-to-run-script-sh-files-in-a-new-terminal-after-connecting-to-ubuntu-16-04%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      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.






      share|improve this answer






















      • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
        – Avery
        May 26 at 13:44






      • 1




        @Avery Then you need to install an X server on Windows, e.g. xming.
        – PerlDuck
        May 26 at 14:21







      • 1




        That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
        – Sergiy Kolodyazhnyy
        May 27 at 9:02










      • It should work with Putty from one or more command line window(s) in Windows.
        – sudodus
        May 27 at 9:22














      up vote
      3
      down vote



      accepted










      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.






      share|improve this answer






















      • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
        – Avery
        May 26 at 13:44






      • 1




        @Avery Then you need to install an X server on Windows, e.g. xming.
        – PerlDuck
        May 26 at 14:21







      • 1




        That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
        – Sergiy Kolodyazhnyy
        May 27 at 9:02










      • It should work with Putty from one or more command line window(s) in Windows.
        – sudodus
        May 27 at 9:22












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      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.






      share|improve this answer














      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 27 at 9:31

























      answered May 26 at 13:40









      sudodus

      19.8k32666




      19.8k32666











      • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
        – Avery
        May 26 at 13:44






      • 1




        @Avery Then you need to install an X server on Windows, e.g. xming.
        – PerlDuck
        May 26 at 14:21







      • 1




        That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
        – Sergiy Kolodyazhnyy
        May 27 at 9:02










      • It should work with Putty from one or more command line window(s) in Windows.
        – sudodus
        May 27 at 9:22
















      • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
        – Avery
        May 26 at 13:44






      • 1




        @Avery Then you need to install an X server on Windows, e.g. xming.
        – PerlDuck
        May 26 at 14:21







      • 1




        That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
        – Sergiy Kolodyazhnyy
        May 27 at 9:02










      • It should work with Putty from one or more command line window(s) in Windows.
        – sudodus
        May 27 at 9:22















      what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
      – Avery
      May 26 at 13:44




      what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
      – Avery
      May 26 at 13:44




      1




      1




      @Avery Then you need to install an X server on Windows, e.g. xming.
      – PerlDuck
      May 26 at 14:21





      @Avery Then you need to install an X server on Windows, e.g. xming.
      – PerlDuck
      May 26 at 14:21





      1




      1




      That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
      – Sergiy Kolodyazhnyy
      May 27 at 9:02




      That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically.
      – Sergiy Kolodyazhnyy
      May 27 at 9:02












      It should work with Putty from one or more command line window(s) in Windows.
      – sudodus
      May 27 at 9:22




      It should work with Putty from one or more command line window(s) in Windows.
      – sudodus
      May 27 at 9:22












      up vote
      2
      down vote













      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.






      share|improve this answer
























        up vote
        2
        down vote













        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.






        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 27 at 9:10









          Sergiy Kolodyazhnyy

          64k9127274




          64k9127274




















              up vote
              1
              down vote













              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






              share|improve this answer


























                up vote
                1
                down vote













                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






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  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






                  share|improve this answer














                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 27 at 12:10

























                  answered May 27 at 11:24









                  pa4080

                  11.8k52255




                  11.8k52255






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1040578%2fhow-to-run-script-sh-files-in-a-new-terminal-after-connecting-to-ubuntu-16-04%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491