How to run script (.sh) files in a new terminal after connecting to Ubuntu 16.04 server via ssh?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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:
command-line server bash scripts ssh
add a comment |Â
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:
command-line server bash scripts ssh
Use screen or similar or use &script.sh &
â Panther
May 26 at 12:45
add a comment |Â
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:
command-line server bash scripts ssh
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:
command-line server bash scripts ssh
asked May 26 at 12:40
![](https://lh6.googleusercontent.com/-1vMsOowL6yc/AAAAAAAAAAI/AAAAAAAAACE/SDlPfH9GPI8/photo.jpg?sz=32)
![](https://lh6.googleusercontent.com/-1vMsOowL6yc/AAAAAAAAAAI/AAAAAAAAACE/SDlPfH9GPI8/photo.jpg?sz=32)
Avery
132
132
Use screen or similar or use &script.sh &
â Panther
May 26 at 12:45
add a comment |Â
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
add a comment |Â
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 liketerminator
andgnome-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.
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
add a comment |Â
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.
add a comment |Â
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 launchedgnome-terminal
open, after the previous command is finish.
4. Demonstration:
add a comment |Â
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 liketerminator
andgnome-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.
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
add a comment |Â
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 liketerminator
andgnome-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.
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
add a comment |Â
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 liketerminator
andgnome-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.
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 liketerminator
andgnome-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.
edited May 27 at 9:31
answered May 26 at 13:40
![](https://i.stack.imgur.com/lcww5.png?s=32&g=1)
![](https://i.stack.imgur.com/lcww5.png?s=32&g=1)
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered May 27 at 9:10
![](https://i.stack.imgur.com/U1Jy6.jpg?s=32&g=1)
![](https://i.stack.imgur.com/U1Jy6.jpg?s=32&g=1)
Sergiy Kolodyazhnyy
64k9127274
64k9127274
add a comment |Â
add a comment |Â
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 launchedgnome-terminal
open, after the previous command is finish.
4. Demonstration:
add a comment |Â
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 launchedgnome-terminal
open, after the previous command is finish.
4. Demonstration:
add a comment |Â
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 launchedgnome-terminal
open, after the previous command is finish.
4. Demonstration:
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 launchedgnome-terminal
open, after the previous command is finish.
4. Demonstration:
edited May 27 at 12:10
answered May 27 at 11:24
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
pa4080
11.8k52255
11.8k52255
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Use screen or similar or use &
script.sh &
â Panther
May 26 at 12:45