Need to use "source" command in node red (function) (want to update the environment variavbles)

582 views
Skip to first unread message

TitusRathinaraj Stalin

unread,
Sep 7, 2016, 11:35:10 AM9/7/16
to Node-RED
Dear All,

I want to use "source"command in node red's function node (exec), but when I use "sudo source /etc/bash.bashrc" or "source /etc/bash.bashrc"
Also I tried to locate the "source" command in Linux, but it seems bash builtin command, not able to find.
How we can execute/update the environment variables (/etc/bash.bashrc) ?

I have the variable in "/etc/bash.bashrc" (say "PORT=1234") and I updated the port variable in "/etc/bash.bashrc" file using "storage" node, so I need to reload the "PORT" variable inside the node-red

PS:
Without restarting the node-red or entering manual commands.

Thanks for your help.

Regards,
Titus S.

Julian Knight

unread,
Sep 7, 2016, 12:39:21 PM9/7/16
to Node-RED
BASH's rc file is only used for interactive sessions I think and so is otherwise ignored. I think you need to put things into the profile file if you want them to run in non-interactive sessions.

Dave C-J

unread,
Sep 7, 2016, 12:45:33 PM9/7/16
to node...@googlegroups.com
Node-RED reads the settings file at run time so will only set settings.uiPort then... so you could potentially set that directly (if you write a custom node) - but, then it only sets the http listen port when RED.start is called (at this time using the settings.uiPort value)... which would normally be done after a stop() command ;-)

so doing it without restarting is currently not possible

Running commands as sudo is also not possible within the exec node as that is an "escalation of privileges" and is specifically blocked.

TitusRathinaraj Stalin

unread,
Sep 9, 2016, 9:19:52 AM9/9/16
to Node-RED
Thanks Julian.
I tried to put this into "/etc/profile" and same behavior, this too needs to be reloaded the environment using "source" or "exec" commands in terminal before running the "red.js" script.
Please let me know if any idea.
I declared the following piece of lines in "/etc/profile"

TITUS=4532
export TITUS

When I open the new terminal1, variable got shown as 4532, if I changed the value to 4562 in other terminal2 then termina1 doesn't have updated value (4562) and we need to run "source /etc/profile" to get the updated value.

Thanks for your reply.

TitusRathinaraj Stalin

unread,
Sep 9, 2016, 9:23:52 AM9/9/16
to Node-RED
Thanks Dave.
Now I'm okay for restarting the node-red but don't want to enter "source /etc/bash.bashrc" or "source /etc/profile" commands manually.
Can we put this command in "red.js" script ?

I tried but not able to get the updated value (seem not executed)

exec('source /etc/bash.bashrc ', {silent:true}).output;

Can you please provide any insights to reload or run the source command in red.js script or any help ?

Thanks for your support.

Regards,
Titus S.

TitusRathinaraj Stalin

unread,
Sep 9, 2016, 9:39:40 AM9/9/16
to Node-RED
I tried this and got the "command not found" error.

var args = exec('source /etc/profile', {silent:true}).output;
console.log(args)


/bin/sh: 1: source: not found

I also tried below method and still has the old value.

var args = exec('echo $TITUS ', {silent:true}).output;
console.log(args)
var args = exec('#!/bin/bash source /etc/profile', {silent:true}).output;
console.log(args)
var args = exec('echo $TITUS ', {silent:true}).output;
console.log(args)



OUTPUT:

root@raspberrypi:/home/pi/workdir/rPi/node-red# node red.js
1234


1234



Welcome to Node-RED
===================

9 Sep 13:39:01 - [info] Node-RED version: v0.14.6-git
9 Sep 13:39:01 - [info] Node.js  version: v4.5.0

sebasti...@gmail.com

unread,
Sep 9, 2016, 10:50:17 AM9/9/16
to Node-RED
Hi Titus,

try ". /etc/profile" instead of "source /etc/profile".

You can check with ". /etc/profile && set" which will print all variables

If this does not work (at least at should give you not the "source not found" any more..
try to move your relevant parts from /etc/profile into a script like "/etc/profile.d/mynoderedexecinit.sh"

Checked that here on a raspbery and it worked.



 

TitusRathinaraj Stalin

unread,
Sep 9, 2016, 2:05:42 PM9/9/16
to Node-RED
Thanks Sebastien.

I tried to put those stuff into "etc/profile.d/script.sh" but still its not updated.
Able to see the updated value when I open new terminal.

I'm doing this in node-red.

Regards,
Titus S.

Julian Knight

unread,
Sep 9, 2016, 3:03:59 PM9/9/16
to Node-RED
I think we may need to step back a bit and look at WHY you are doing this.

Why do you actually want to source the profile? Just to set an OS environment variable that you can then reference in Node-RED?

Assuming that you have put this into /etc/profile.d, you need to have logged out and back in again before the script will have run. Once you've done that, the variable should already have been set before you run Node-RED and should therefore be available. The only other thing that would prevent this is if you are running Node-RED from a startup script (systemd for example) and it is running before the environment is ready. If that is the case, I recommend an alternative.

The alternative I would recommend is putting the port into a file in a fixed format, the same format as the script would be fine. Then make sure the file is readable by the user running Node-RED. Finally, simply read the file and parse the data in Node-RED itself. This will be a lot more robust
Reply all
Reply to author
Forward
0 new messages