Laharl
Laharl

Reputation: 129

Linux: Is there a way to timestamp all output bring printed to the monitor?

Is there a way to add a timestamp to everything that gets printed to the monitor for Linux? I'm running CentOS 6.5 and 7 for reference. For example, here is a sample output:

SELinux:  Initializing.
Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
Mount-cache hash table entries: 256
Initializing cgroup subsys ns

I'd like it to show up as:

1494019853 SELinux:  Initializing.
1494019853 Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
1494019854 Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
1494019854 Mount-cache hash table entries: 256
1494019857 Initializing cgroup subsys ns

Upvotes: 1

Views: 584

Answers (1)

Jamil Said
Jamil Said

Reputation: 2093

Here's a way to prepend a timestamp to the results of a command/script, using ts from the moreutils package:

echo 'Hello world!' | ts "%d/%m/%Y_%H:%M:%S"

Output:

05/05/2017_14:55:33 Hello world!

You can install the package moreutils (if available from your repository) with:

apt-get-update && apt-get install moreutils

Edit: If you want to print a timestamp on ALL system messages appearing on the terminal, I wouldn't know how to do this. However, you could still be able to check the timestamp for many of those messages on system logs, which store messages with a timestamp. Here are some of those logs (note that the path may be different on your system):

1) /var/log/messages, /var/log/syslog: those logs have human readable timestamps.

2) dmesg: to examine the dmesg log with readable timestamps, you could use the following command:

dmesg -T

Sample output:

[Mon May  8 11:53:59 2017] Initializing cgroup subsys cpuset
[Mon May  8 11:53:59 2017] Initializing cgroup subsys cpu
...

Upvotes: 2

Related Questions