TL:DR: In which init level /etc/profile or /etc/environment files are read?

I want to create a service (/etc/init.d/myservice) starting a shell script located at /opt/myservice/myservice.sh. However this location (/opt/myservice/) is defined at installation time, so it's not a fixed location.

Let's say that I set an environment variable in /etc/profile or /etc/environment like MYSERVICE_PATH=/opt/myservice, set # Default-Start: 3 4 5 on /etc/init.d/myservice file.

Could I safely assume that $MYSERVICE_PATH will always be available at those init levels (3,4,5), so my init script could call sh $MYSERVICE_PATH/myservice.sh?

  • 1
    /etc/profile is only read upon login. What you can do is have your installer drop a configuration file in /etc/ (for example, /etc/myapplication.conf) that the startup script can read for installation location and other relevant information. – DopeGhoti Dec 7 '16 at 20:34
  • 1
    Why not simply create /etc/init.d/myservice on installation? – michas Dec 8 '16 at 6:30
  • Thank you for answering @DopeGhoti. I think your answer is the right answer. Could you provide an answer with some more technical details? This way I could accept yours as the correct answer and upvote it. – cezarlamann Dec 8 '16 at 16:20
  • Thank you for answering @michas. I'm trying to do this, but I'm not a "full fledged" shell script developer. :-) – cezarlamann Dec 8 '16 at 16:23
  • 1
    My pleasure. I've gone ahead and written up a longer version of my comment as an answer. – DopeGhoti Dec 8 '16 at 21:01

You may be looking in the wrong place to store application configuration information.

/etc/profile is used for the configuration of default settings for login shells (e. g. when SSHing in and invoking bash as the login shell). It purpose is not for the configuration of application installation locations and the like.

/etc/ as a whole, is, however, exactly that. What is probably the best way to accomplish what you are looking for is to define a configuration file which always lives at e. g. /etc/myservice.conf, which might look something like:

# Configuration file for the My Service daemon
# Default settings:
#   myservice_root - The directory in which the service is installed
#                    Default:  /var/run/myservice
#   myservice_port - The TCP port upon which the service listens for incoming connecitons
#                    Default:  55321

Your application, then, can always look at /etc/myservice.conf for its configuration, no matter what location on the filesystem it happens to be installed into, and other system administrators can instantly see what the file is for and what options are available to be reconfigured.

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.