# Finding no of active users in FreeBSD 5.2.1



## potterv (Nov 15, 2009)

Hello evryone,

I was wondering how can we find the no. of active users in FreeBSD 5.2.1 Is there a way to do that? How can we implement it in source code?


----------



## CodeBlock (Nov 16, 2009)

What do you mean number of active users? The number of people actually running 5.2? How would putting it in the source help that, if they refuse to upgrade to 7 what makes you think they would upgrade their kernel at all?

Or do you mean number of users on your system, in which case, `w` comes in handy.


----------



## SirDice (Nov 16, 2009)

Both w(1) and who(1) come to mind.


----------



## DutchDaemon (Nov 16, 2009)

[cmd=]last | awk '{print $1}' | grep -v -E '(^$|reboot|shutdown)' | sort -u | wc -l[/cmd]

for this month


----------



## potterv (Nov 16, 2009)

Active users mean the number of users who are currently running atleast 1 process on the system. Can you tellmehow to do it inside the code. Do we have to assign a variable or something?


----------



## DutchDaemon (Nov 16, 2009)

[cmd=]ps aux | awk '{print $1}' | sort -u | wc -l[/cmd]


----------



## SirDice (Nov 16, 2009)

potterv said:
			
		

> Active users mean the number of users who are currently running atleast 1 process on the system. Can you tellmehow to do it inside the code. Do we have to assign a variable or something?



What kind of code?

You can find the C source code for both commands here:
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/w/
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/who/


----------



## anemos (Nov 16, 2009)

potterv said:
			
		

> Can you tellmehow to do it inside the code.



I assume you mean C code, so, have a look at utmp(5).

As you may find out from the links provided by SirDice, w(1) and who(1), as well as users(1) use /var/run/utmp file to extract this info from the system.


----------



## potterv (Nov 18, 2009)

Hey Guys,

I was thinking ...Can we write a sysctl statement inside w.c file so the number of users become a global variable and can be accessed system-wide.

If so, what will be the correct way?

Help!!


----------



## SirDice (Nov 18, 2009)

potterv said:
			
		

> I was thinking ...Can we write a sysctl statement inside w.c file so the number of users become a global variable and can be accessed system-wide.


Simple question, why?


----------

