How can I set environment variables in my Linux service for Asterisk even though it doesn't have a real user?

26,575

Solution 1

Either set them in the startup script (/etc/init.d/yourdaemon), or put a line in that file that looks like:

. /etc/yourdaemon.env

and put the environment variables in that file, using the syntax export VAR=value. On Red Hat-like systems, I believe the correct place for such a file is /etc/sysconfig. Debian/Ubuntu seems to have /etc/default for this purpose.

Solution 2

If your distro of choice is now using systemd try systemctl edit --full asterisk.service and consider EnvironmentFile and Environment

These files normally live here: /etc/systemd/system/myservice.service e.g cron.service

Share:
26,575
domino
Author by

domino

Just another lone ranger #SOreadytohelp

Updated on July 19, 2020

Comments

  • domino
    domino almost 4 years

    I have created a linux service that runs as a deamon (and gets started from /etc/init.d/X). I need to set some environment variables that can be accessed by the application.

    Here's the scenario. The application is a bunch of Perl AGI scripts that depend on (and therefore need to run as) asterisk user but asterisk doesn't have a shell. Ideally I'd just set this in /home/asterisk/.bashrc but that doesn't exist for asterisk.

    How can I set environment variables for my app in the asterisk user's running environment so that my app can use them?

  • domino
    domino over 13 years
    Tried that approach. The problem am having with that is that '/etc/init.d/yourdaemon' runs as root and so they are not available to my script, which runs as asterisk.
  • domino
    domino over 13 years
    start-stop-daemon --start --chuid=$USER --exec $DAEMON. This executes the app as $USER
  • Fred Foo
    Fred Foo over 13 years
    I checked the source code for the Debian version of start-stop-daemon and there's only one place where it touches the environment, to reset HOME. Can you post the init.d script?
  • domino
    domino over 13 years
    It worked. I am the one that had not used export. Thanks. Just a small point for future users - if there's a file to be sourced when running /etc/init.d/myscript it's good practice to add it to /etc/default/myscript.
  • domino
    domino over 13 years
    One thing I still don't understand though - how are the variables being exported to my deamons environment even though the init script is run by root?
  • Fred Foo
    Fred Foo over 13 years
    Every process passes its environment to its child processes. The start-stop-daemon program gets its environment from the init.d script; then it switches users internally, running as user asterisk for a short while and preserving its environment; then it starts your daemon. You'd have to use the env command explicitly to wipe the environment clean.
  • AliBZ
    AliBZ over 10 years
    @domino Your comment worked for me, not the answer. I don't know why.
  • sequence
    sequence about 2 years
    Your answer helped me find this answer (unix.stackexchange.com/a/455283/248589), which solved my problem.