简体   繁体   中英

how to add timestamp to the result of bash command for logging that can to file and screen

this link can not solve my questtion perfectly How to add timestamp to STDERR redirection

First question: i don't want a output of command, which add timestamp with everyline, i just want to add timestamp to first line, as the picture

Second question: atfer i execute the script , i must presss a enter key to end the process , how to solve enter image description here

  #!/bin/bash exec > >(xargs -L1 -I{} bash -c "echo \\$(date +'%x %T') '{}'" | tee error.log) 2>&1 errorcommand pwd ifconfig ls 
 output:
    root@xintian-desktop:~# bash exec.sh
    2017年03月19日 10:31:51 exec.sh: line 3: asdsad: command not found
    root@xintian-desktop:~# 2017年03月19日 10:31:51 /root
    2017年03月19日 10:31:51 enxb827eb65188e Link encap:Ethernet  HWaddr b8:27:eb:65:18:8e
    2017年03月19日 10:31:51 UP BROADCAST MULTICAST  MTU:1500  Metric:1
    2017年03月19日 10:31:51 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    2017年03月19日 10:31:51 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    2017年03月19日 10:31:51 collisions:0 txqueuelen:1000
    2017年03月19日 10:31:51 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    2017年03月19日 10:31:51 lo        Link encap:Local Loopback
    2017年03月19日 10:31:51 inet addr:127.0.0.1  Mask:255.0.0.0
    2017年03月19日 10:31:51 inet6 addr: ::1/128 Scope:Host
    2017年03月19日 10:31:51 UP LOOPBACK RUNNING  MTU:65536  Metric:1
    2017年03月19日 10:31:51 RX packets:425 errors:0 dropped:0 overruns:0 frame:0
    2017年03月19日 10:31:51 TX packets:425 errors:0 dropped:0 overruns:0 carrier:0
    2017年03月19日 10:31:51 collisions:0 txqueuelen:0
    2017年03月19日 10:31:51 RX bytes:34225 (34.2 KB)  TX bytes:34225 (34.2 KB)
    2017年03月19日 10:31:51 wlan0     Link encap:Ethernet  HWaddr b8:27:eb:30:4d:db
    2017年03月19日 10:31:51 inet addr:192.168.88.26  Bcast:192.168.88.255  Mask:255.255.255.0
    2017年03月19日 10:31:51 inet6 addr: fe80::11f7:7058:ec37:3064/64 Scope:Link
    2017年03月19日 10:31:51 UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    2017年03月19日 10:31:51 RX packets:342872 errors:0 dropped:203 overruns:0 frame:0
    2017年03月19日 10:31:51 TX packets:17631 errors:0 dropped:0 overruns:0 carrier:0
    2017年03月19日 10:31:51 collisions:0 txqueuelen:1000
    2017年03月19日 10:31:51 RX bytes:69917459 (69.9 MB)  TX bytes:2141207 (2.1 MB)
    2017年03月19日 10:31:51 20150305
    2017年03月19日 10:31:51 20150405
    2017年03月19日 10:31:51 20150505
    2017年03月19日 10:31:51 aaa
    2017年03月19日 10:31:51 aaa.bak
    2017年03月19日 10:31:51 asd
    2017年03月19日 10:31:51 error.log
    2017年03月19日 10:31:51 exec.sh
    2017年03月19日 10:31:51 nohup.out
    2017年03月19日 10:31:51 python_act
    2017年03月19日 10:31:51 sec
    2017年03月19日 10:31:51 stderr.log
    2017年03月19日 10:31:51 trap.sh

To answer your first question, I routinely add a timestamp line to the output of my scripts, especially those that run periodically. There are lots of ways to do it. Here's what I typically do:

   DATE=`date`
   echo "------------------- $DATE -------------------"

The key is executing the date command and printing the output in any format you want.

The simplest is to simply execute date by itself, without storing the output in a variable and then adding extra characters (in the snipped above I add extra "-" characters to make the timestamp line stand out in a log). Eg,

    date

You could also store the output in a variable using the more modern form:

    DATE=$(date)

You can of course format the timestamp line as you wish, eg,

    echo "== $0 running $DATE =="

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM