I'm working on an embedded Linux system and looking for a way to set the time zone for all processes.

The question is: Is there any possibility to set the TZ environment variable at boot time (init scripts) so that all other init scripts have TZ set at start up?

The system is based on the BusyBox tools. I tried a script in /etc/profile.d/ folder.

export TZ="GMT-1"

But the init scripts do not have the variable set.

The only way I see is that all start scripts set this variable by themselves. Are there other solutions for this problem?

  • The system timezone is often set via a symbolic link from something like /usr/share/zoneinfo/Zone/SubZone to /etc/localtime - but I don't know enough about your config / setup to know if that's viable. – EightBitTony Mar 1 '16 at 12:35
  • 1
    @EightBitTony That works with Glibc, but not with some of the libc that are common on embedded systems. – Gilles 'SO- stop being evil' Mar 1 '16 at 22:40
  • How about /etc/TZ ? – EightBitTony Mar 1 '16 at 22:51
  • So actually, what we need to know is which libc @Klaus is using in their embedded system. It's really a question about libc, rather than busybox. – EightBitTony Mar 1 '16 at 22:52
  • @EightBitTony glibc version is 2.21 – Klaus Mar 2 '16 at 13:28

If your system uses BusyBox init, and it doesn't have /etc/inittab, then it runs /etc/init.d/rcS at boot time. If this is a shell script, just add the environment definitions you want there. If this isn't a shell script, you can change your build to rename /etc/init.d/rcS.bin, and create a shell script /etc/init.d/rcS that ends with exec /etc/init.d/rcS.bin. Of course, if you've changed the path /etc/init.d/rcS in the build configuration, adapt for that. Environment variables defined there will apply to all daemons, but not to shells started on consoles.

If you have /etc/inittab, check what it contains. The documentation is in the sample file. You can replace entries that run somecommand by /usr/bin/env TZ=GMT-1 somecommand .

That's the quick-and-dirty way, but in most cases you'd want to allow the end user to configure the timezone, so it should be stored in a separate file. In that case, go through a shell wrapper (or the rcS script) and use some code like

export TZ="$(cat /etc/TZ.txt)"

Note that depending on your libc, there may or may not be a better way of setting the timezone, e.g. writing the timezone rules in /etc/TZ for uClibc.

| improve this answer | |
  • With your answer we decide to do the following: The system has a export-tz script. This script we source in /etc/defaults/rcS. So all the init scripts have TZ. /etc/profile.d/ has a link to export-tz. So every user has TZ. For each /etc/inittab entry, the TZ needs, we source the export-tz script. – Klaus Mar 7 '16 at 13:29

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.