I have been trying to change the system time on an offline Raspberry Pi 4 manually from a Python script that receives the time from a TCP client in the form of a string. As required by my project, the time will be need to be adjusted regularly from an input string. Below is the function I have tried using:
import subprocess
def timechange(time):
cmd = ['exec sudo date +%T -s ' + str(time)]
proc = subprocess.Popen(cmd, shell=True)
proc.kill()
newtime = "13:00:00"
timechange(newtime)
However, I can't seem to consistently get this to change the time successfully using the above function. I have tried using os.open/subprocess.call instead but from other question responses, it seems that using os is not recommended.
All this function needs to do is to change the system time. Does the subprocess automatically exit and close the shell after the command finishes?
Being a beginner, I am not sure if it has to do with the termination of the shell/cmd or if it spawns too many shells. Any help will be much appreciated!