6

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Klaus
  • 63
  • 1
  • 3

1 Answers1

4

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 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 07 '16 at 13:29