Add x seconds to the current date in Linux

后端 未结 2 798
夕颜
夕颜 2021-02-02 12:16

I have two Linux (CentOS 6.0) machines over here and I need to add (or remove) 5 seconds to the current date time. In the end, both my machines would have a gap of 5 seconds (on

相关标签:
2条回答
  • 2021-02-02 12:41

    You can add 5 seconds to the current time in one command using date -s "5 seconds".

    0 讨论(0)
  • 2021-02-02 13:02

    Actually, Linux comes with a handy function to return a time plus a modifier.

    date --date='5 seconds'
    

    You can test it from the command line with a simple

    date && date --date='5 seconds'
    

    Using this you can just write a small batch file that sets a variable to the time you want and then runs the set command.

    EDIT: here's a bash script that will do it for you. It needs to be run as root

    #!/bin/bash
    
    NEWDATE=`date +%T --date '5 seconds'`;
    date +%T -s "$NEWDATE";
    
    0 讨论(0)
提交回复
热议问题