How can I set environment variables in my Linux service for Asterisk even though it doesn't have a real user?
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
Comments
-
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 over 13 yearsTried 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 over 13 yearsstart-stop-daemon --start --chuid=$USER --exec $DAEMON. This executes the app as $USER
-
Fred Foo over 13 yearsI checked the source code for the Debian version of
start-stop-daemon
and there's only one place where it touches the environment, to resetHOME
. Can you post theinit.d
script? -
domino over 13 yearsIt 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 over 13 yearsOne 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 over 13 yearsEvery process passes its environment to its child processes. The
start-stop-daemon
program gets its environment from theinit.d
script; then it switches users internally, running as userasterisk
for a short while and preserving its environment; then it starts your daemon. You'd have to use theenv
command explicitly to wipe the environment clean. -
AliBZ over 10 years@domino Your comment worked for me, not the answer. I don't know why.
-
sequence about 2 yearsYour answer helped me find this answer (unix.stackexchange.com/a/455283/248589), which solved my problem.