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:
- App is run non-interactively, e.g. from ssh command, spawned as child process without STDIO attached, etc.
GUI is missing, STDIN is missing
. - App is run interactively from X-session with .desktop file or alike.
GUI is present, STDIN is missing
. - 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. - 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:
- GUI test - whether app can interact with user using graphical windows: test for
$DISPLAY env variable
. - 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
Makefile for Linux Kernel Module
Split Delimited File into Smaller Files by Column
Installing Octave Package in Ubuntu
How to Run Command During Docker Build Which Requires a Tty
How to Uniquely Identify an Usb-Device
How to Catch Stdout Stream in Ffmpeg Then Pipe It to V4L2Loopback
How to Install Visual Studio 2015 in Ubuntu
Xargs Sh -C Skipping the First Argument
Best Text Search Engine for Integrating with Custom Web App
Initiating Dynamic Variables (Variable Variables) in Bash Shell Script
Arm Assembly "Retne" Instruction
Random Number Between Range in Shell
How to Set a Dynamic Variable in Haproxy
How to Run a Linux Command That Still Runs After I Close My Putty Ssh Session