Creating an "odometer" for time spent on the server

I want to build an odometer to track how long I have been on the server since I last reset the counter.

I recently spent quite a bit of time working on one of my school unix servers, and began to wonder how much time I had in the last couple of days. I started trying to think about how I could write either a Bash script program or C program to run when my .bash_profile was uploaded (i.e. when I ssh to the server), the background itself and save the file time when I closed the session.

I know how to get the program to start at login (via .bash_profile) and how to use the C program (by forking?), But I'm not sure how to determine if the ssh session was completed (maybe by looking at the sshd process?) I hope this is the correct stack exchange to ask how you are going for something like this and appreciate any input.

+4
source share
2 answers

Depending on your shell, you can simply start the process in the background when you log in, and then process the kill signal when the parent process (shell) exits. It will not consume resources, you do not need root privileges, and it should give a fairly accurate account of your logged time.

You may need to use POSIX semaphores to log multiple instances at the same time.

+1
source

Do you think that you are writing a script that can be run by cron every minute, starting “who”, looking at its output for lines with your uid in them and throwing a counter if it finds any? (Use "crontab -e" to edit your crontab.)

Even a line in crontab:

* * * * * (date; who | grep $LOGNAME)>>$HOME/.whodata 

... will create a journal that you could process later in your free time.

+1
source

Source: https://habr.com/ru/post/1379821/


All Articles