How to solve crontab time zone mismatch with system time zone
Posted onIntutorialViews: Word count in article: 1kReading time ≈1 mins.
I have a program which need to run everyday at certain time. It has some package which cannot install in alpine Linux, which means I cannot deploy this in qinglong panel. So I decided to use crontab. However I found it did not run at the certain time I hoped. At first, I think it must because the system time zone is wrong and indeed I had not modify the system time zone.
How to modify system time zone on Linux (Ubuntu in my environment)
1 2 3 4 5
date# this shows your current time zone sudo apt install tzdata sudo dpkg-reconfigure tzdata # then choose the time zone you want ls -lh /etc/localtime date# check whether it is effective
However, the next day, the program did not wrong properly as well. Then I did some search and found the answer. The crontab has its own time zone environment which means we need to clarify it ourself.
How to set time zone for crontab
1
sudo vim /etc/crontab
1 2 3
# add the following lines into your crontab file, you can add it after SHELL=/bin/sh, replace the exact time zone you need, do not just copy and paste my time zone simply. CRON_TZ=Asia/Shanghai TZ=Asia/Shanghai
1
service cron restart
And you will find your task will run at the time you set.