Determine if user is logged in via CLI?

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
NotoriousPyro

Determine if user is logged in via CLI?

Post by NotoriousPyro »

Is there any way to determine if a user is logged in via CLI or on desktop?

I've got this script which mounts user directories on login, and do not want it to execute certain parts when the user is logged in via CLI:

Code: Select all

if [ "$USER" != "root" ]; then
  pulseaudio --start&
  g15aiostats&
  nvidia-settings -a [gpu:0]/GPUPowerMizerMode=1&
  # User profile setup
  # Variables
  NW_MNTD=/home/$USER/.network
  
  # Remove redundant stuff
  rmdir ~/Documents ~/Music ~/Pictures ~/Videos ~/Public
  
  # Change user folder permissions
  chmod 0700 /home/$USER
  
  # Create softlinks
  ln -sf $NW_MNTD/$USER/Documents	~/Documents
  ln -sf $NW_MNTD/$USER/Music		~/Music
  ln -sf $NW_MNTD/$USER/Pictures	~/Pictures
  ln -sf $NW_MNTD/$USER/Videos		~/Videos
  ln -sf $NW_MNTD/Media			~/Media
  if grep -qs $NW_MNTD/Admin /proc/mounts; then
    ln -sf $NW_MNTD/Admin		~/Admin
  fi
fi
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Talegolas

Re: Determine if user is logged in via CLI?

Post by Talegolas »

the command "who" tells you the name of the user and if they are connected to tty or pts.. would that work?
User avatar
bjornmu
Level 3
Level 3
Posts: 189
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: Determine if user is logged in via CLI?

Post by bjornmu »

I think this might work:

Code: Select all

if tty > /dev/null ; then
  echo CLI
else
  echo Not CLI
fi
Locked

Return to “Other topics”