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?


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'

So, the command above will create:


See man date for the different formats you can have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.