I have a server that I use infrequently, so I'd like to cron
a job to shutdown daily if no users are logged in via SSH and SAMBA shares.
How can I determine how many active SSH connections there are, and how many active SAMBA connections there are?
If both of these values are zero, the cron script will shutdown the server.
Active ssh connection:
Active Samba connection:
or use lsof:
You can check any active connections to any service by using the following command
netstat -an | grep ESTABLISHED | grep ":[portname] " | wc -l
By portname you need to select the port of the service you're looking into, for SSH that'll be port 22 (so it should be
grep ":22 "
) (the space behind just makes sure that it's not another port.If you want to select more than one port (as it would be in samba) change grep for
egrep ":(port|port|port) "