Add timestamp to cat output from shell script
You need to redirect awk's output to the file, not cat's. The way you have it, awk gets nothing. Actually, you may not need cat at all:
awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 &
Bash: Append command output and timestamp to file
Given your comments, it seems that you want multiple processes to be writing to the file concurrently, and have a timestamp on each individual line. Something like this might suffice:
some_cmd | perl -ne '$|=1; print localtime . ": [some_cmd] $_"' >> logfile
If you want to massage the format of the date, use POSIX::strftime
some_cmd | perl -MPOSIX -ne 'BEGIN{ $|=1 }
print strftime( "%Y-%m-%d %H:%M:%S", localtime ) . " [some_cmd] $_"' >> logfile
Adding timestamp to a filename with mv in BASH
The few lines you posted from your script look okay to me. It's probably something a bit deeper.
You need to find which line is giving you this error. Add set -xv
to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.
BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash
on top, but your bash interpreter is located in /usr/bin/bash
, you'll see this error.
EDIT
New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?
Two ways:
- Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
- To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.
How to add a timestamp to bash script log for every command and exclude set, echo, log?
I found a working solution:
timestamp_xtrace() {
while IFS= read -r line; do
if [[ "$line" != "+ set"* ]] && [[ "$line" != "+ echo"* ]] && [[ "$line" != "+ log"* ]]; then
echo "$(date) $line";
fi
done
}
i changed the regex (=~ "^ "+ echo"
) to (!= "+ echo"*
) so the test is matches the right lines :
# cat << EOF | timestamp_xtrace
+ somecmd
+ echo "hello world!"
hello world!
+ svnadmin -args
EOF
Output:
Wed Sep 30 09:12:43 CEST 2015 + somecmd
Wed Sep 30 09:12:43 CEST 2015 hello world!
Wed Sep 30 09:12:43 CEST 2015 + svnadmin -args
Prepend timestamp to each line received from stdin
Your issue is because sed "s/^/$(date -u) /"
captures the date string at the time it is launched and places it at the start of each line. The date string is not updated for each incoming new line from the curl
's response.
It is safer to use awk
rather than sed
, to prepend the date to each line, as it will capture a new date for each line.
Your original date format is locale formatted, local time. It has been kept here as-is, if it is your preference; but usually logs timestamps are better printed with the iso.org: ISO 8601 format, or an UTC relative Unix timestamp.
curl https://example.com/process |
awk '{ printf("%s %s\n", strftime("%c", systime()), $0) }' >>/path/to/logs.log &
With an ISO-8601 timestamp:
curl https://example.com/process |
awk '{ printf("%s %s\n", strftime("%Y-%m-%dT%H:%M:%S%z", systime()), $0) }' \
>>/path/to/logs.log &
See man strftime
for time formatting.
Adding timestamp to output with AWK
You can add the timestamp within awk using the strftime function (GNU awk) without the need to use date outside:
timeout 2s ping -qc1 $output 2>&1 | awk -F/ '/^rtt/ { printf strftime("%d-%m-%Y_%H-%M-%S")" OK %.2f ms\n", $5; ok = 1 } END { if (!ok) print "FAIL" }' >> $output.txt
Without GNU awk:
timeout 2s ping -qc1 $output 2>&1 | awk -v timestamp="$(date +%d-%m-%Y_%H-%M-%S)" -F/ '/^rtt/ { printf timestamp" OK %.2f ms\n", $5; ok = 1 } END { if (!ok) print "FAIL" }' >> $output.txt
Pass the result of the date command into awk with -v as a variable timestamp.
Related Topics
Hbase Does Not Run After ./Start-Hbase.Sh - Permission Denied
How to Add More Algorithm in Cryptoapi in Linux
Enabling The Vt-X Inside a Virtual Machine
Can Inotify Tell Me Where a Monitored File Is Moved
How to Store Data Permanently in /Tmp Directory in Linux
Reset Bash History Search Position
How to Flush Stdout of a Running Process
App Engine Ignores Symlinks to Directories
Transliteration Script for Linux Shell
Qt - How to Detect Whether The Application Is Running on Gnome or Kde
Combine Two Audio Files with a Command Line Tool
How to Delete 5 Lines Before and 6 Lines After Pattern Match Using Sed
Getting "Permission Denied" on Dirname and Basename
Linux Kernel Code in Memory Check with Sha256 Sum
Will Java Compiled in Windows Work in Linux