LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2009, 02:43 PM   #1
xaviraan
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
script : How many sessions are there ?


Hi everybody,

I need a script that shows the total number of sessions there are ...

This shows how many sessions are open...

Code:
 echo "Number of open sessions: "
  w | grep :0 | wc | awk '{print $1}
But how can I see how many sessions there are in total??
I hope someone can help me...

Grtz
 
Old 06-10-2009, 03:26 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi grtz,
welcome to LQ!

How do you define a "session", what discriminates an open session
from which other kind of session?

And I'm fairly certain that the snippet above doesn't provide
the information you believe it does. How does grepping idle
times for ":0" qualify for a open session?
Code:
w | grep :0
tink    pts/0    -                Fri09   17:03m  0.16s  0.16s bash


Cheers,
Tink
 
Old 06-10-2009, 03:27 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,838
Blog Entries: 14

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Based on that definition of "session":

Code:
 w |egrep -v "load average|USER" |wc -l
The -l argument of wc tells it to give you number of lines only.
 
Old 06-10-2009, 03:40 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
I'm not sure about how to answer your question, but I found a couple of problems with the command you posted.

In short, grepping for :0 is not an accurate way to list users. If a user is logged into a tty terminal then there won't be a :0 listed. Also, I just tried to run it, and I matched a line with an idle time of "2:07" as well. A better one would list all lines from w after the first two. Something like this:
Code:
w|sed -n '3,$p'|wc| awk '{print $1}'
I'll bet you also find a solution using just awk, too, but I don't have the energy to work it out myself right now. :P

Edit: beaten to the punch again.

Last edited by David the H.; 06-10-2009 at 03:43 PM.
 
Old 06-10-2009, 03:52 PM   #5
xaviraan
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks, jlightner! I think that's what I need

And thanks also for the upgrade of my code, David!!

grtz
 
Old 06-10-2009, 03:59 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by David the H. View Post
I'll bet you also find a solution using just awk, too, but I don't have the energy to work it out myself right now. :P
;}
Code:
w | awk 'END{print NR-2}'

Cheers,
Tink
 
Old 06-10-2009, 04:17 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Thanks Tinkster. I knew it had to be something easy like that. I was using the same "print NR-2" expression as yours, but couldn't figure out how to get it to output only the last record number. I didn't think to put it in the END statement. How simple.

Last edited by David the H.; 06-10-2009 at 04:19 PM.
 
Old 06-11-2009, 09:24 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,838
Blog Entries: 14

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Quote:
Originally Posted by xaviraan View Post
Thanks, jlightner! I think that's what I need

And thanks also for the upgrade of my code, David!!

grtz

No problem.

By the way - if you click the little thumbs up ICON on the posts David and I made we'll be "officially" thanked for the record.
 
Old 06-11-2009, 09:30 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Another option:
Code:
users | wc -w
 
  


Reply

Tags
sessions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to test for open FTP sessions from specific clients dz-015 Programming 7 04-09-2009 06:33 AM
LXer: Using Screen, Script, Mkfifo And Redirection To Watch Or Log User Sessions LXer Syndicated Linux News 0 07-11-2008 08:00 AM
Two X sessions? LinuxSeeker Linux - General 3 04-07-2004 03:39 PM
Sessions BajaNick Linux - General 0 07-30-2003 09:19 PM
Sessions in PHP4 glj Programming 6 10-23-2001 04:45 AM


All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration