从后台运行命令在文件名上附加当前时间戳

Pra*_*kar 4 command-line bash timestamp

以下是我在 bash 脚本中使用的命令,它在后台运行:

sshpass -p prakash12 ssh -t -p $1 prakash@localhost './test_new_update_script.sh > /home/log/unit_update_output.log 2> /home/log/unit_update_error.log < /dev/null | echo $! > /home/log/unit_update_pids &'
Run Code Online (Sandbox Code Playgroud)

该命令在运行时创建了 3 个文件,即unit_update_output.logunit_update_error.logunit_update_pids. 并打印相关信息。在那个文件中。

current timestamp我想在每 3 个文件名的末尾附加。

例如: unit_update_output-2014-04-08T22-15-02.log “2014-04-08T22-15-02”这是当前时间戳。

上述命令在后台运行:

那么我该怎么做呢?

ter*_*don 5

该命令是否后台运行并不重要,您所需要做的就是将调用的输出添加date到您正在创建的文件名中。使用与您的问题完全相同的语法,您可以执行以下操作:

sshpass -p prakash12 ssh -t -p $1 prakash@localhost \
    './test_new_update_script.sh > /home/log/unit_update_output.$(date '+%F-%T').log \
     2> /home/log/unit_update_error.$(date '+%F-%T').log < /dev/null | 
     echo $! > /home/log/unit_update_pids$(date '+%F-%T') &'
Run Code Online (Sandbox Code Playgroud)

技巧是添加$(date '+%F-%T').,这将返回(例如):

$ date '+%F-%T'
2014-04-08-18:54:52
Run Code Online (Sandbox Code Playgroud)

因此,上面的命令将创建:

/home/log/unit_update_output.2014-04-08-18:57:02.log 
/home/log/unit_update_error.2014-04-08-18:57:02.log 
/home/log/unit_update_pids2014-04-08-18:57:02
Run Code Online (Sandbox Code Playgroud)

查看man date您可以拥有的不同格式。