1

I have a cron script under /etc/cron.d as so:

SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 0 * * * root /usr/local/sbin/app_logrotate >> /var/log/app-newday.log

This works but it executes always at 00:00 local time. The app in question uses UTC time ( I cannot change this). In my time zone this is a few hours behind resulting in the date tag on the daily logfile this application creates never being a new day.
I can't change the system localtime to UTC as other applications depend on localtime. I was wondering if it is possible to execute this cron only at 00:00 UTC while keeping my system localtime.
I have tried adding TZ=UTC to the cron script which did not work.
Does anyone know how this can be done?

2

2 Answers 2

1

In general the cron dameon will inherit the system's timezone. The only (easy) way I know is to set the TZ/CRON_TZ variable.

Did you restart the cron daemon after setting the TZ / CRON_TZ variable. This si required for cron to pick up the changes!

0

Some implementations recognize a CRON_TZ variable to specify the interpretation of the crontab numbers. man 5 crontab Centos 7 says:

The CRON_TZ variable specifies the time zone specific for the cron table. The user should enter a time according to the specified time zone into the table. The time used for writing into a log file is taken from the local time zone, where the daemon is running.

While default Debian/Ubuntu says:

The cron daemon runs with a defined timezone. It currently does not support per-user timezones. All the tasks: system's and user's will be run based on the configured timezone. Even if a user specifies the TZ environment variable in his crontab this will affect only the commands executed in the crontab, not the execution of the crontab tasks themselves.

So, if your system respects it:

CRON_TZ=UTC
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 0 * * * root /usr/local/sbin/app_logrotate >> /var/log/app-newday.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .