Linux shell script to log and timestamp terminal input and display that log    - Page 1
Forum section: electronics / general

author: ornea
author_rank: Unknown
timestamp: February 02, 2023, 11:20:28 pm
content: My shell (redhat bash) scripting powers are very weak.I have searched for ways to create simple script (lets call it ow.sh) that when run will tail the contents of a file (lets call it ow.txt) and append any STDIN to that file with a timestamp.The idea is many users can ssh sally@server /path/path/ow.shThe terminal will then display the last ~10 entries in the ow.txt and wait for input and will update if there are any changes made to the file like tail -f does.Any input that is followed by <Enter> will be appended to the file with a time stampAll I have thus far is a script that appends a timestamp to a file (borrowed from elsewhere)Code:f=ow.txttmp=$(mktemp)(tail "$f";date;) > "$tmp" && mv "$tmp" "$f"I think 'tee' along with some >1 & <2 type hieroglyphics will be useful here but not sure howExample contents of ow.txtCode:3 Feb 2023 7:05am  Server Peanuts has been bounced as requested and is back up3 Feb 2023 7:15am  Yep, we can see that, all good now, thanks for your help3 Feb 2023 7:25am  Thanks Charles. You guys seen watchmeforever by any chance. Its ... differentIt appears to be a 'trival' task with all the funky io redirect features that linux has, but if it proves otherwise I can always create a python script as a fall back.Would welcome any tricks, hints or complete solutionsCheers Ornea

author: brucehoult
author_rank: Super Contributor
timestamp: February 02, 2023, 11:46:16 pm
content: Here's a one-liner. Adjust date format to suit, and pipe it as you wish.Code:perl -pe '$d = localtime(); $_ = "$d: $_"'For those unfamiliar, the "-p" flag to Perl puts a loop around your code that reads stdin line by line into $_, and then prints $_ after your code. So I simply modify what is in $_ (the current line).

author: ledtester
author_rank: Super Contributor
timestamp: February 02, 2023, 11:54:16 pm
content: Related Stackexchange questions:https://superuser.com/questions/298347/appending-timestamp-to-tail-f-resultshttps://stackoverflow.com/questions/43149846/tail-f-log-file-timestamp

author: magic
author_rank: Super Contributor
timestamp: February 03, 2023, 06:56:30 am
content: Quote from: ornea on February 02, 2023, 11:20:28 pmMultiple concurrent users?What happens if I'm halfway through writing a line and somebody else also adds a line?Obviously, we wouldn't want to mix these lines in the file, and this can be assured by buffering a full line before writing anything.But what is supposed to happen to the text display on my screen when this occurs, and how?This doesn't look trivial, it seems you want to implement some sort of chat?

author: Nominal Animal
author_rank: Super Contributor
timestamp: February 03, 2023, 11:34:55 am
content: Make sure you havebashandflock(from util-linux package) installed –– they are in all Linux distros by default ––, create/var/log/eventlog.userwith appropriate rw access to the group of users that are allowed to use this, and save the following as a script those users can execute:Code:#!/bin/bash# EVENTFILE: path to the event fileexport EVENTFILE=/var/log/eventlog.user# EVENTS: number of events to show; default 20, overridableEVENTS=$(($EVENTS-0))[[ $EVENTS -le 0 ]] && EVENTS=20while [[ 1 ]]; doclearprintf 'Events up to %s:\n' "$(date '+%Y-%m-%d %H:%M:%S %z')" >&2flock -s "$EVENTFILE" tail -n $EVENTS "$EVENTFILE"printf 'Enter to refresh, CTRL+D to exit, or text for new entry:\n' >&2printf '> ' >&2read Line || break# Remove leading and trailing whitespaceLine="${Line//[[:space:]]/ }"Line="${Line# }"Line="${Line% }"if [[ -n "$Line" ]]; thenexport MSG="$(date '+%Y-%m-%d %H:%M:%S %z') $(id -un): ${Line}"flock -e "$EVENTFILE" -c 'echo "$MSG" >> "$EVENTFILE"'fidoneprintf '[Ctrl-D]\n' >&2It works absolutely fine even if multiple users use it at the same time, because it usesflockto advisory-log the event log file during reading and appending. The downside is that new entries are not automatically shown, the user must press Enter to see any new messages.If you were to put the display of the log and appending to the log into separate shell scripts (/usr/share/eventlog/listand/usr/share/eventlog/addfor example, controlled by environment variablesEVENTFILEandEVENTS), it would be very easy to have them color-code the output, too, if so desired (and if the connection is to a tty:if $(stty &>/dev/null); thenis-a-tty; elsenot-a-tty; fi).Personally, however, I'd write a small C program for this, using ncursesw, so that it'd work like a real-time chat, except using the file for the chat record.Lemme know if that would be useful; other members here could perhaps suggest useful notation (like auto-highlighting patterns like @).

author: shapirus
author_rank: Super Contributor
timestamp: February 06, 2023, 11:45:21 pm
content: Quote from: ornea on February 02, 2023, 11:20:28 pmCode:$ sudo apt install moreutils$ man tsp.s. sorry I missed that you use redhat. You'll obviously need to use yum (or whatever the today's redhat package management tool of the day is) instead of apt, and the package name may be different. It may well be already installed too.

author: ornea
author_rank: Unknown
timestamp: February 07, 2023, 07:38:44 am
content: I will attempt some of the suggestions. Thankyou all for taking the time.I should have mentioned that the OS is locked down so I cant add anything except what I enter via a keyboard.I was having some success creating a script that launches two xterm's. One for input and one for output side by side. Just need to iron out the bugs when ssh-ing.Interesting that tail -f does not work in this setup over nfs so solved with tail -n executed regularly.Definitely not as trivial as I first thought

author: ornea
author_rank: Unknown
timestamp: February 22, 2023, 12:45:29 am
content: Solution I used if useful to others.Given the system restraints simple solution was to create a launcher bash script that opened two separate gnome terminal sessions that ssh'es to a server and launces that two scripts i.e. ssh user@server owInput.sh . One for input, and one for output. The launcher also pkill'ed any existing sessions if running.Input simply waited for user input and appended it to a file with date stamp and the user and then calls it self.Output tail'ed the file in a while loop (as tail -f did not work)Thankyou all for your input