A simple program to update the system time of a Linux device using NTP during the boot process.
Compile the C program using GCC:
gcc -o update_time update_time.c
-
Compile the program using make:
makeOr manually:
gcc -o update_time update_time.c -
Install the binary:
sudo make installOr manually:
sudo cp update_time /usr/local/bin/ -
Edit the root crontab to run at boot with a delay:
sudo crontab -e -
Add the following line (adjust sleep time as needed, e.g., 30 seconds to ensure internet connection):
@reboot sleep 30 && /usr/local/bin/update_time
The program will automatically run at boot with a delay (30 seconds by default) to ensure the internet connection is established. It fetches the current time from an NTP server (pool.ntp.org) and sets the system time.
To check if the cronjob is set:
sudo crontab -l
To test manually:
/usr/local/bin/update_time
- GCC for compilation
- Make for building
- libcurl for HTTP requests (install with
sudo apt install libcurl4-openssl-devon Debian/Ubuntu) - Root privileges for setting system time and installing the binary
- Internet connection
make: Compile the programmake install: Install the binary to /usr/local/bin/make clean: Remove the compiled binary