Linux modify the system time of a single application

Introduction

The following is a simple example of libfaketime.
It is often necessary to test the modification time in the work. If the environment does not allow the time to be adjusted, it is necessary to find a way to adjust the time of a single process.

Compile and install

    git clone https://github.com/wolfcw/libfaketime.git
    
    cd libfaketime  && make install 

python test script

test.py

import os
import time
import datetime
def set_time():
    while True:
        print(datetime.datetime.now())
        time.sleep(2)


set_time()

shell set environment variables

#!/usr/bin/env bash

export LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 
#export FAKETIME="2020-12-24 20:30:00"  #该时间会一直保持不变
export FAKETIME="@2020-12-24 20:30:00"  #时间会从这里往后递增
python ./test.py

The result is as follows

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325188942&siteId=291194637