Skip to main content
Solved

Command to check if the user is logged in?


chrisdaggett
Forum|alt.badge.img+7

I am making a simple bash script. I want to use an IF statement that checks to make sure the user is logged in before it runs (else Exit).

Anyone know what command line to use to make sure the computer is not at the login window, or that a user is logged in?

Thanks in advance.

Best answer by maxbehr

Agree, I'm sure there is probably a more elegant way but I just use

who | grep "console"

if that returns anything then you know a user is logged in.

View original
Did this topic help you find an answer to your question?

4 replies

Forum|alt.badge.img+16
  • Valued Contributor
  • 277 replies
  • November 2, 2016

You can gather that information from who. There might be a better way to do it, though.


Forum|alt.badge.img+5
  • New Contributor
  • 78 replies
  • Answer
  • November 2, 2016

Agree, I'm sure there is probably a more elegant way but I just use

who | grep "console"

if that returns anything then you know a user is logged in.


iJake
Forum|alt.badge.img+21
  • Contributor
  • 279 replies
  • November 2, 2016
currentUser=$(stat -f %Su "/dev/console")

if [[ "$currentUser" == "root" ]]; then
exit
fi

chrisdaggett
Forum|alt.badge.img+7
  • Author
  • Valued Contributor
  • 94 replies
  • November 2, 2016

Thanks all :).


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings