How to Tell If Running in a Linux Console Versus an Ssh Session

How to tell if running in a linux console versus an ssh session?

Checking the return value of ttyname(3) against your stdin should give you the name of the terminal which is feeding the input of your process.

It will be /dev/console if the program is being run on the console (and doesn't have it's input redirected). You can also check stdout to see if it is connected to /dev/console - see which fits your usage scenario better.

Linux Terminal: how to capture or watch other terminal session

If the other person is using the Linux console, you can use conspy.

Bash script - determine if script launched in terminal console or gui

The answer by @LeonidMew is incomplete and somewhat incorrect.

You should not detect GUI by presence of STDIN (that's what [ -t 0 ] test does). There are cases when none of STDIN and GUI are available, e.g. when you run the script over ssh session in non-interactive mode. This happens often for CI deploys.

Correct answer heavily depends on your task, in general there are 4 distinct environments:

  1. App is run non-interactively, e.g. from ssh command, spawned as child process without STDIO attached, etc. GUI is missing, STDIN is missing.
  2. App is run interactively from X-session with .desktop file or alike. GUI is present, STDIN is missing.
  3. App is run interactively from linux terminal (ssh, bare text console, hosting recovery console, etc.). GUI is missing, STDIN is present. App can interact with user in text mode via STDIN.
  4. App is run interactively from GUI terminal app, like xterm. GUI is present, STDIN is present.

There are 2 basic tests that can help to identify the environment:

  1. GUI test - whether app can interact with user using graphical windows: test for $DISPLAY env variable.
  2. STDIN test - whether app can interact with user using text console: test for file descriptor 0 (aka STDIN) with if [ -t 0 ]; ...

Combining these two test will give you the environment:

test 1 false + test 2 false: case 1 -- no user interaction available
test 1 true + test 2 false: case 2 -- interact via XWindows
test 1 false + test 2 true: case 3 -- interact via STDIN/console
test 1 true + test 2 true: case 4 -- XWindows or STDIN/console, whichever is preferred

How to check if ssh-agent is already running in bash?

No, really, how to check if ssh-agent is already running in bash?

Answers so far don't appear to answer the original question...

Here's what works for me:

if ps -p $SSH_AGENT_PID > /dev/null
then
echo "ssh-agent is already running"
# Do something knowing the pid exists, i.e. the process with $PID is running
else
eval `ssh-agent -s`
fi

This was taken from here

How to run an X program from outside the X session (e.g. from the console or SSH)

The short answer is that you have to set the DISPLAY environment variable, and then the app will run.

The long answer is that we've got Xauth, and unless you're running as the same user on the same machine that's probably not going to work unless you export the Xauth credential from the account running the X server to the account running the X client. ssh -X handles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAY on the X server account and feeding that data into xauth merge - on the client account. (Warning: the data is binary.)

On modern Linux systems, there is one X session at :0 and the X11 authority data file is always $HOME/.Xauthority so you can most often set two environment variables, for example, in Bash:

export XAUTHORITY=/home/$your_username/.Xauthority
export DISPLAY=':0'


Related Topics



Leave a reply



Submit