9

I run a job at specific date[Client time not local time] to send email to my clients. I use cpanel cron job i tried to use cron job with time zone

but it still sending in my local time zone

32 16 08 05 *  /usr/bin/curl CRON_TZ=Africa/Algiers https://www.example.com/SendCron.php?CronID=382 #CronID382END # z93v66 

what wrong i did using Cron_TZ ??

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
Salem Ahmed
  • 193
  • 1
  • 1
  • 4

1 Answers1

20

Your CRON_TZ= is in a wrong place in your crontab file, as an option attribute for /usr/bin/curl.

You should instead have it as an environment variable set before the cron entries:

CRON_TZ=Africa/Algiers 

32 16 08 05 *  /usr/bin/curl https://www.example.com/SendCron.php?CronID=382

If you need to have time zone set for a single cron entry on the same single line, it is not possible with the CRON_TZ environment variable, but you need to invoke external env (run a program in a modified environment) to change the TZ:

32 16 08 05 * /usr/bin/env TZ=Africa/Algiers /usr/bin/curl https://www.example.com/Send...

Notice that both changes the time zone for the command, not for cron daemon. The cron daemon just checks whether the fields matches current time or not, regardless of the CRON_TZ variable. The manual page for systemd-cron crontab explains this limitation:

The systemd-cron units 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.

If you need to run the schedules based on timezone, you must change the server timezone, e.g.:

ln -sf /usr/share/zoneinfo/Africa/Algiers /etc/localtime
Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129
  • but in cronjob doesnot have an area to add cron timezone .. is there any way to add it in cron entry? – Salem Ahmed May 08 '17 at 14:17
  • A `crontab` is a text file. Plain text files should have plenty of space and area. In this case, you can get more by adding a newline i.e. EOL sequence i.e. `\n` (ASCII `0x0a`). Or are you possibly using some kind of [off-topic web hosting control panel](https://meta.serverfault.com/questions/8094/where-can-i-ask-questions-about-web-hosting-control-panels?rq=1)? – Esa Jokinen May 08 '17 at 14:25
  • Yes i have cpanel there is cron job manager – Salem Ahmed May 08 '17 at 14:54
  • thanks again .... can i set cron_tz for each cron ?? – Salem Ahmed May 08 '17 at 15:14
  • 3
    @SalemAhmed you could run `32 16 08 05 * /usr/bin/env TZ=... yourcommand ...` to set it specific to a command. – thrig May 08 '17 at 18:45
  • it doesnot work probably – Salem Ahmed May 09 '17 at 03:50
  • @thrig + esa i have to add both and now it worked – Salem Ahmed May 09 '17 at 09:07
  • @esa-jokinen CRONT_TZ works for the schedule table! [Documentation](https://linux.die.net/man/5/crontab) The CRON_TZ specifies the time zone specific for the cron table. User type into the chosen table times in the time of the specified time zone. The time into log is taken from local time zone, where is the daemon running. – Maxim Mandrik Apr 29 '22 at 17:55