Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

how do i check from within a php script, if a user is logged in? My current way is, to get the output of

w | grep <username>

and check, if there is output. It works, if a user is logged in via gdm, kdm or ssh or login on a tty. But what it doesn't get is, if an other user opened a terminal and run su <username>, this shell process is not reported, how do i get this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Well, if userA uses su to become userB, then userB is still not logged in.

However, how about this:

ps -u <username>

Then you can check $? -eq 0 to see if any processes were found belonging to that user. However, if there are any long-running processes owned by that user, then it will still show them. If that's no good, you could check for their shell.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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