"Must be connected to a terminal error" with screen -x command on a Linux Container - linux

I am using Linux containers with Proxmox 4.
When I am connected on an Ubuntu 16.04 container with:
pct enter <id>
I run a script with screen and try to watch the process running with:
screen -x <processname>
I got the error:
Must be connected to a terminal error
When I connect with:
ssh -t <user>#<container_ip>
It works, I can attach the display to the screen.
How can I use screen -x when I am connected with pct enter ?

I found a solution that works for same case(https://www.svennd.be/screen-in-lxc-attach)
Execute this
script /dev/null
Then you can run screen, et voila

This is happening mostly because of the reason that you must have
switched user
to the current terminal user using the command sudo su or user changing command.
You will need to own the shell on which you are working.
You can do so by running this:
script /dev/null.
Alternatively,
You can restart the terminal session with the user(which was used when creating screen) and not coming to current user using
switch user commands.
To know active screens :
screen -list

Alternatively, You can start the session within "hohup" and send to background

Related

Run a shell script in Terminal through a keyboard/desktop shortcut

I'm using Linux (Mint 20.3) to run a simple Minecraft server and I want to be able to start the server with a keyboard or desktop shortcut. I also want to be able to interact with the server in a terminal after it starts. I'm currently using the server software supplied by Mojang. I wrote a little program to get things started:
#!/bin/bash
cd /home/trevor/Minecraft_Server
LD_LIBRARY_PATH=. ./bedrock_server
exec $SHELL
I can get the server to run but I have no clue how to get it to open a terminal window so I can interact with the server. I'm relatively new to Linux so any input would be greatly appreciated.
you can use screen to detach and attach to run commands into the minecraft terminal.
to install screen:
apt-get install -y screen
To launch, update your script with something like:
screen -S mcs ./bedrock_server
to reattach, run the following in a terminal:
screen -r mcs
Use screen in your script to reattach to bedrock process.
Install screen:
apt-get install screen
Define your script as:
#!/bin/bash
export LD_LIBRARY_PATH=.
cd /home/trevor/Minecraft_Server
screen -d -m -S bedrock ./bedrock_server
After invoke your script, screen creates a socket that can be used to reattach to your script terminal. You can show screen sockets available with:
screen -ls
Parameter -S defined 'bedrock' as the socket name. So you can open another terminal as you like and reattach to the bedrock process with:
screen -r bedrock
If you detach the screen with CTRL+C the screen will be closed and so the minecraft bedrock server. To deattach without close the process you must use CTRL+A and CTRL+D.

Run Linux command in background and keep runing after closing SSH [duplicate]

This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 3 years ago.
I need to run a Perl script for several days processing something. On a linux Centos server, from the SSH terminal I run this command:
nohup perl script.cgi 2>&1 &
This runs the script in the background and writes the output to nohup.out.
The problem when I close the SSH terminal or even my internet connection disconnects the script terminates.
I need to keep this command running in the background on the server after I close
the SSH terminal.
You can use Terminal multiplexer tools like screen, byobu or tmux.
I personally use screen. so install it on remote server via sudo apt-get install screen.
ssh into server
open screen session by screen -S sessionname
Now run your command (background/foreground both works)
now detach to your session by command ctrl+a then press d.
Now shut your pc and enjoy
now come back ssh into server then use command screen -x sessionname to reconnect the detached session.
Hurray! script is still running.
you can either use screen or run the command using supervisor in linux systems.
you can install screen using sudo apt install screen
then use following command to run it.
screen -S test_command
nohup perl script.cgi 2>&1 &
Then press ctrl+a and ctrl+d to leave that session running for whatever amount of time required until your server reboots.
If you want to stop the command use screen -x test_command, then ctrl+c and use ctrl+a and ctrl+d to close screen or ctrl+a and ctrl+d to leave the screen session as it is.
The only way I was able to run the command and exit the shell and keep the command running is using the "at" tool to schedule the job like this using the full script path:
echo "perl /home/username/www/script.cgi" | at now + 1 minute

Using screen in startup script

I'm trying to write a startup script to launch rtorrent. I've planed on using screen in the script. I tried this command in the startup script :
su -l nico -c "screen -dmS rtd rtorrent"
But it seems like it doesn't work (i can't get the screen session back) :
screen -ls
No Sockets found in /var/run/screen/S-nico.
Do i miss something ?
The list of active screen sessions is by default maintained independently per user.
In your screen launch command, the session was started by user nico, so if you want to inquiry about it you should probably run su -l nico -c 'screen -ls' instead.

OpenSSH on Cygwin

I have a Linux box (Ubuntu Server 13.04) which needs to run a job on a Windows 7 box (with cygwin installed) under a specific user's account. I have set up a password-less login to access the Windows machine through openSSH.
The problem I face is the following: when I manually ssh into the Win7 machine and launch the job everything is fine. However, when I launch the job using ssh winuser#winmachine command, I end up connecting to the Windows machine under the privileged sshd user 'cyg_server':
$ whoami
linuxuser
$ ssh winuser#Win7
$ whoami
winuser
$ exit
$ ssh winuser#Win7 "whoami; exit"
cyg_server
>> This should be 'winuser' too.
Why could this be happening? I have tried running ssh-host-config again to no avail. I don't see what parameters might influence this in sshd_config either.
Any help is greatly appreciated!
I had similar issues when I was connecting to a Cygwin machine using SSH. I used to have no problems logging on until one day I noticed that my path wasn't set correctly. I spent ages recreating the configuration files with ssh-host-config only to find my answer in the man page for ssh:
If command is specified, it is executed on the remote host instead of
a login shell.
The problem was the alias I had used to connect to the machine had been changed to connect to a screen session automatically (screen -DR). That meant that if there wasn't already a screen session to attach to, screen was not being run as a child process of a user login shell and not inheriting any of the relevant user environment.
When you provide a command as an argument to ssh, the resulting command is run as a process started by cyg_server. Ensuring the SSH command is being run as part of a login shell should do what you want:
ssh winuser#Win7 "bash -l -c 'whoami; exit'"
Explanation (from the bash man page):
-c string If the -c option is present, then commands are read from string.
-l Make bash act as if it had been invoked as a login shell.

Using SSH to open application on desktop

So normally people ask how to forward x11 to the local machine, but rather I want to leave the application running on the remote box's desktop.
So let say I ssh from a windows machine (using putty) and run xclock & disown. If I then walked over to my desktop and look at the screen and see xclock running on the linux machine.
Any way to do that? Using Mint 13.
This works for me once I'm ssh'd in:
export DISPLAY=:0; nohup <command> &>/dev/null &
For example:
export DISPLAY=:0; nohup iceweasel &>/dev/null &
When you're ssh'd in normally, set the DISPLAY variable with export DISPLAY=0:0 (0:0 being the display of the target box), and then you can run your command as you normally would. If you want to be able to close the ssh session, prepend the command with nohup: nohup ./yourcommand > dev/null.
h4bo --> That worked. just had to make the script.
First part of learning ssh stuff. Now instead of leaving teamviewer (a remote desktop program) open all the time, I can launch when I need it and then use it.
i guess
nohup ssh -X <ip address> <application name>
I recently had to do some python tkinter development on a Raspberry Pi with Thonny, where I wanted the tkinter graphics to show on the Pi's X server. For this specific purpose, I created a /usr/bin/pythonx script which simply consisted of:
#!/bin/bash
DISPLAY=:0 python "$#"
This wrapping technique works when Thonny ssh's into the Pi where the user is also logged in at the GUI desktop, as the ssh user has access to the .Xauthority data needed to send X client requests (application) to the server (GUI).

Resources