Why forever can't see environment variable? - node.js

I had a problem for accessing environment variable using forever.
Thanks to this post, I have a workaround making env vrariable accessible but I'm still wondering why I have to add them all the time :
MY_VARIABLE=SOMETHING forever start my_script.js
because I've previously done an :
export MY_VARIABLE=SOMETHING
Then opened a new terminal but still no luck ...
I would like to set my variable once and not have to write it down each time I'm managing my script ...
Thanks for people who will take time to make me less an idiot and satisfy my curiosity ...

export setting tightly on the current bash or session effectively.It exists in memory.

Related

Passing and updating an environment variable constantly

I need a way to constantly update a environment variable from a script. I need to use that environment variable in another program almost real time.
What I have is this code:
# !bin/bash
while :
do
AMA="$(cut -c 1-13 text.txt)"
source directory/this_script
done
I am getting the right information from my file when running cut this way. I just need to get this variable permanently updating if possible. Also a way to even get it to the environment.
Slackware Linux

Run init.d script conditionally based on hostname

What would be the best way to conditionally run an init.d script on linux based on hostname? I'm working with New Relic and some of the servers simply don't need it installed, but they're all otherwise basic copies of one another. This is Ubuntu.
I've tried (and failed) to put in a host conditional but for the life of me I can't get it working. Threw exits in the top of the file as well as in the start function, but it seems to fire up every time. Without knowing completely how those scripts are fired I'm a little confused on how to alter it to not fire if it server name isn't something like production, etc.
Any guidance would be super helpful.
Put this at the top of the script you would like to disable:
if [ $(hostname) != "goodhost" ]
then
exit
fi
replacing "goodhost" with the actual name of the host where the script is supposed to run.
Does that solve the problem?

How to deal with DISPLAY=:0.0

My X program sometimes must be started from console, but by default on Ubuntu the DISPLAY env var is not set. Is it correct to include DISPLAY=:0.0 startmyapp in the startup script, or how to deal with it otherwise? Who and when and how is responsible for setting this display variable properly? Why the environment is not out of the box ready for this once you install X or the desktop that includes X?
The DISPLAY variable controls the X session that the application communicates with. There can be many (or none) of these available at any given moment. You can't just statically set this value and have it necessarily be correct.
It is set correctly inside an X session (through the environment) and you can set it manually outside of that to use that same session (but you have to know which session you want to use for that).
If you know that will always be your session and you know that will always be the session you want to use then yes, you can just set it in the services startup script. Otherwise you need to come up with some other way to figure out which DISPLAY value you need.

placing a value in a variable and making it stay

I have been trying to write a script where instead of using names I have done the obvious and saved time by assigning user ids and placed them in a variable. var1=usid
echo $usid shows that its done correct but I save, exit and when I reopen in the morning, the variable no longer exists. Is there a way to make it permanent?
Why don't you use Unix export?
VAR=value
export VAR
The export command will mark each VAR for automatic export to the environment of subsequently executed commands i.e. make the local shell variable VAR global.

What is the XDG_SESSION_COOKIE environment variable for?

I've been fighting with crontab recently because in Intrepid the gconftool uses a dbus backend, and that means that when used from crontab it doesn't work.
To make it work I have had to export the relevant environment variables when I log in so that it finds the dbus session address when the cron comes to run.
Out of curiosity I wondered what environment the cron could see and it turns out all I have is HOME, LOGNAME, PATH, SHELL, CWD and this new one on me, XDG_SESSION_COOKIE. This looks curious and several googlings have thrown up a number of bugs or other feature requests involving it but nothing that tells me what it does.
My instinct is that this variable can be used to find all the stuff that I've had to export to the file that I source before the cron job runs.
My questions, therefore, are a) can I? b) if so, how? and c) what (else) does it do?
Thanks all
This is very interesting. I found out it is the display manager setting a cookie. That one can be used to register processes to belong to a "session" which are managed by a daemon called ConsoleKit. That is to support fast user switching. My KDE4.2.1 system apparently supports it too.
Read this fedora wiki entry.
So this environment variable is like DBUS_SESSION_BUS_ADDRESS to give access to some entity (in the case of XDG_SESSION_COOKIE a login-session managed by ConsoleKit). For example having that environment variable in place, you can ask the manager for your current session:
$ dbus-send --print-reply --system --type=method_call \
--dest=org.freedesktop.ConsoleKit \
/org/freedesktop/ConsoleKit/Manager \
org.freedesktop.ConsoleKit.Manager.GetCurrentSession
method return sender=:1.1 -> dest=:1.34 reply_serial=2
object path "/org/freedesktop/ConsoleKit/Session1"
$
The Manager also supports querying for the session some process belongs to
$ [...].Manager.GetSessionForUnixProcess uint32:4494
method return sender=:1.1 -> dest=:1.42 reply_serial=2
object path "/org/freedesktop/ConsoleKit/Session1"
However, it does not list or somehow contain variables that is related to some cron job. However, documentation of dbus-launch says that libdbus will automatically find the right DBUS bus address. For example, files are stored in /home/js/.dbus/session-bus that contain the correct current dbus session addresses.

Resources