簡體   English   中英

如何在bash命令的結果中添加時間戳以記錄可以記錄到文件和屏幕的內容

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

此鏈接無法完美解決我的問題如何為STDERR重定向添加時間戳

第一個問題:我不想要命令的輸出,該命令在每行中添加時間戳,我只想在第一行中添加時間戳,如圖所示

第二個問題:我執行腳本后,必須按Enter鍵才能結束該過程,如何解決此處輸入圖像的描述

  #!/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

為了回答您的第一個問題,我通常在腳本的輸出中添加一條時間戳記行,尤其是那些定期運行的腳本。 有很多方法可以做到這一點。 這是我通常做的事情:

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

關鍵是執行date命令並以所需的任何格式打印輸出。

最簡單的方法是簡單地自己執行date ,而不將輸出存儲在變量中,然后添加額外的字符(在上面的代碼片段中,我添加了額外的“-”字符以使時間戳記行在日志中突出顯示)。 例如,

    date

您還可以使用更現代的形式將輸出存儲在變量中:

    DATE=$(date)

當然,您可以根據需要設置時間戳記行的格式,例如,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM