I am looking to implement this feature where an LED connected to Pi GPIO should turn on whenever the Pi is successfully connected to a PC by SSH and turn off otherwise.

Appreciate any help in implementing this


2 Answers 2


Add this to your ~/.profile file, where <pin> refers to the pin number where the LED is connected:

if [[ -n $SSH_TTY ]] ; then
    gpio mode <pin> out
    trap 'gpio write <pin> 0' EXIT
    gpio write <pin> 1

The $SSH_TTY is set only in SSH sessions, so the whole block will be executed when you login via SSH, but not locally. trap executes a command when the session ends. Assuming that setting the GPIO pin to 1 turns the LED on and 0 turns it off, it should remain lit while the SSH session is active.

Note that this will not work with several simultaneous sessions. If you need that, you could write a script which lights the LED up repeatedly in a loop so that it would remain lit if one of multiple session ends.

  • Thanks for ur response. I added the above lines to /home/pi/.profile as follows: if [[ -n $SSH_TTY ]] ; then gpio mode 16 out trap 'gpio write 16 0' EXIT gpio write 16 1 fi LED is connected to Pi Pin no. 16 (physical pin). Sending 1 to the pin turns ON the LED. My Pi is connected to Putty through SSH. However, the LED didn't get turned ON. Pl. let me know if any further settings are to be made to implement the above feature. Thank u.
    – Raghavan K
    Commented Mar 4, 2022 at 7:49
  • @RaghavanK I would start by keeping a single line, gpio write 16 1, and progressively adding code to see what is not working. If even this line doesn't work, it would mean you either didn't test it on the command line (it could be that you tested it with sudo which you forgot to add to the code), or your system is set up to use a different startup file, not /home/pi/.profile. Commented Mar 4, 2022 at 10:40
  • I'm curious: The OP seems to be asking for a solution that will illuminate an LED when any user makes an SSH connection to the Pi. If your code is in ~/.profile, doesn't that limit it to working only for that one user?
    – Seamus
    Commented Mar 5, 2022 at 22:53

Here's how to do it w/ multiple SSH sessions:

Your question is a bit vague in some respects, but it seems to me that you'll want to know if anyone has an open SSH connection - not necessarily one specific user. The solution proposed here allows you to filter for certain users if that's what you want - or if any user has an ssh connection.

We'll develop a script to get the data we need, and turn the LED ON or OFF depending upon that data. The data on SSH connections will be provided by the command w. The LED will be controlled using the gpio-led overlay. Finally, this script will need to be a daemon of sorts - i.e. it will run in the background, even if no one is logged on. Otherwise, the LED would always be ON!

The script:

#!/usr/bin/env bash
# Consult 'w' for active SSH sessions - indicated by use of a pseudo-terminal/pts
# turn LED ON or OFF depending on number of pts

# put w in infinite loop w REFCYCLE sleep interval

   PTS_CT=$(w | awk '/pi/ && /pts/ {count++} END{print count}')

#  until PTS_CT has a value assigned, set it to 0; it's a cron thing
   if ! [[ $PTS_CT =~ ^[0-9]+$ ]]


   if [ $PTS_CT -gt 0 ]
      echo 1 > /sys/class/leds/mysshled/brightness
      echo 0 > /sys/class/leds/mysshled/brightness


   sleep $REFCYCLE


The crontab entry:

You can run this script from the root crontab (privileges required for writing to sysfs) as follows:

$ sudo crontab -e
# edit the crontab; add the following line:

@reboot /home/pi/ssh-stats-w.sh >> /home/pi/ssh-stats-w.log 2>&1

# save file & close editor to install new crontab

If we add a line to /boot/config.txt, we'll have a label assigned to the GPIO we want to use (makes our script easier):

$ sudo nano /boot/config.txt
# edit the file; add one line:


# save the file & exit the editor

And that's it - assuming you have an LED wired in properly.

If not, here's how to wire the LED:

Connect your LED & a resistor in the range of 100 - 220 ohms between (physical pin numbers) 20 and 22 your RPi - see pinout reference for further details if needed.

Ground at Pin 20 --> 100𝝮 R --> Cathode|LED|Anode --> GPIO25 at pin 22

anode and cathode

The Moment of Truth

With all of the pieces above in place, simply reboot to run the script from cron.

Shortly after logging in (via SSH of course) to your freshly-booted RPi, the LED should illuminate. With ssh-stats-w.sh running in the background & independent of your shell, you can now logout to extinguish the LED (assuming it's your only login). Before issuing the logout command, run w from the command prompt to verify you're the only user connected via SSH. If you're alone, a logout will extinguish the LED until you log in again.

Alternatively, start the script from the CLI as follows:

$ sudo su
# nohup ./ssh-stats-w.sh &
[1] 1406

Explanation of command line sequence:

The script must be run as the root user as it needs permissions to write to sysfs, but running nohup from sudo is a bit tetchy. Therefore we use sudo su to become root & run nohup from a root shell. The output 1406 is the PID number assigned to ssh-stats-w.sh. Also note that the process is run in the background & so that our terminal is freed for other activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.