4

Following is command that I have used in bash script and it is run on background:

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 &'

This command created 3 files at run time i.e. unit_update_output.log, unit_update_error.log and unit_update_pids. And print the related info. in that files.

I want to append the current timestamp at the end of each 3 files name.

Like: unit_update_output-2014-04-08T22-15-02.log "2014-04-08T22-15-02" this is current timestamp.

The above command is running on background:

So how can I do this?

terdon
  • 104,119
Prakash H
  • 2,631

1 Answers1

5

Whether the command is backgrounded or not is irrelevant, all you need to do is add the output of a date call to the file names you are creating. Using the exact same syntax as in your question, you could do:

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') &'

The trick is adding $(date '+%F-%T')., this will return (for example):

$ date '+%F-%T'
2014-04-08-18:54:52

So, the command above will create:

/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

See man date for the different formats you can have.

terdon
  • 104,119