I recently saw this blog post regarding performance when the TZ environment variable is not set: https://blog.packagecloud.io/eng/2017/02/21/set-environment-variable-save-thousands-of-system-calls/

I have noticed this problem when stracing running daemons on my systems and would like to fix it. I reviewed the official Ubuntu documentation for where to define environment variables: https://help.ubuntu.com/community/EnvironmentVariables

I tried defining TZ in /etc/environment and in /etc/profile.d/test.sh, but I cannot get this environment variable to be available in all cases (e.g. if I just execute bash without --login or if I run the sample c program provided in the above article). How can I make the TZ environment variable defined completely system-wide?


Edit: It was suggested that my question is a duplicate of this other post, but the methods described in that post do not solve this problem. I tried both of these: sudo dpkg-reconfigure tzdata and sudo timedatectl set-timezone America/New_York. After, I confirmed that /etc/localtime is a valid symlink as expected. However, after a reboot I still see 10 stat() calls to /etc/localtime as documented in the original article I posted above. The behavior I am looking for (as documented in the original article) is making it so /etc/localtime is not stat()-ed repeatedly, which is very inefficient

  • 1
    Did you log out (of the desktop session) and back in after modifying /etc/environment or /etc/profile.d/xxx? AFAIK those changes should propagate down from the shell that's started by the display manager – steeldriver Mar 28 '17 at 16:22
  • Yes, I rebooted. Note that I want this variable to be available from services started via upstart or systemd, which could execute with /bin/sh or another shell (not bash) and thus may not read in /etc/profile.d/* – Andrew Martin Mar 28 '17 at 17:03
  • I'm only scratching the surface with systemd, but as far as I can make out your options are to specify EnvironmentFile=-/etc/environment in your unit files, or set a DefaultEnvironment="TZ=whatever" as specified in the systemd-system.conf manpage (I don't think there is currently a DefaultEnvironmentFile) – steeldriver Mar 28 '17 at 17:58
  • That is not how you set your TZ, see askubuntu.com/questions/323131/setting-timezone-from-terminal – Panther Mar 28 '17 at 18:07
  • 3
    Possible duplicate of setting timezone from terminal – Panther Mar 28 '17 at 18:07

I assume you did something like


This will not however propagate to subshells - and you normal bash shell is a subshell of the wider environment. So you need to do:

export TZ=:/etc/localtime
  • Correct. Neither of these worked, even after a reboot. I need a solution that will work for upstart jobs and systemd units as well as commands invoked via other shells (e.g. sh, dash, ash, etc). – Andrew Martin Mar 28 '17 at 17:03
  • @AndrewMartin most of those start with clean environments, and usually don't have any common file for setting an environment. – muru Apr 3 '17 at 5:21
  • Thanks, it sounds like I will need a multi-faceted solution then. – Andrew Martin Apr 5 '17 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.