1

I'm working on an embedded device that runs Yocto Poky as distro.

The final product will have several applications that are started by SysVinit. There is only one root user.

I have some scripts, both in /etc/init.d and /etc/profile.d that add some environment variables needed by our applications. The problem I'm facing is that, if I run the app "by hand" (so I have an interactive shell) they run nicely, since the scripts in /etc/profile.d/ were sourced, but when ran by SysVinit, they cannot find the environment populated (obviously). We are using sh as shell.

I read about different files like .bashrc, .profile and some variables like ENV and BASH_ENV. I tried to define my variables there, but without luck. A small workaround I found is launching my main application in a login shell (from SysVinit):

2:12345:once:/bin/sh -lc /usr/bin/my_app

So my questions are:

  1. How can I set the environment during boot (aka without logging in) or let SysVinit do it in a nice way?
  2. The exports performed in /etc/init.d scripts do not propagate to my environment at all; that's because those scripts are run in a subshell, and not sourced in the current one (that will required login two) I guess, so how can I export variables from there?
  3. Is my workaround a valid solution?
1
  1. How can I set the environment during boot (aka without logging in) or let SysVinit doing it, in a nice way?

During the boot environment is clean (isn't initialised jet). You need put env variables via source or . commands for sh (if your service is a script) or initialise it in the program (i.e. via config file).

Small example of service script header:

#!/bin/bash

[ -f /etc/profile.d/needed.sh ] && . /etc/profile.d/needed.sh

. . .
  1. The exports performed in /etc/init.d scripts do not propagate to my environment at all, that's because those script are run in a subshell, and not sourced in the current one (that will required login two) I guess, so how can I export variables from there?

source your env variables for subshells or call commands with specified env variable.

Small example of service script header:

#!/bin/bash

. . .

$(BASH_ENV=/etc/profile.d/needed.sh command)
$(ENV_VAR1=value1 command)

. . .
  1. Is my workaround a valid solution?

If it's work - it's dirty way for the service. See 1. and 2.

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.