# Run task before NR startup and when you click on the deploy button

**URL:** <https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613>\
**Category:** General\
**Created:** [28 September 2021 10:12 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613 "2021-09-28T10:12:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 10:12 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/1 "2021-09-28T10:12:45Z")

</div>

Hello, Do you think it is possible to:

1. Run a private task before the nodered service is started ?
2. Launch a script when using the Deploy button ?

For the 1st case, we can obviously create a script that does the private task and then launches node-red-start. But maybe there is already something integrated in NR ?

For the 2nd case, I don't see how to do it, any ideas?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 September 2021 10:56 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/2 "2021-09-28T10:56:30Z")

</div>

> [@Jean-Luc](#):
>
> Run a private task before the nodered service is started

Is that a long running service that you want to start before the node-red service starts? If so then you can build that requirement into the systemd startup script (assuming this is on a system that uses systemd, such as Raspbian or Ubuntu). Make a systemd script to start the service and then require that in the node red systemd script.

Another possibility is to use an exec node to run the script and trigger that from an Inject node set to fire on startup, but that may be too late for your requirement.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 11:08 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/3 "2021-09-28T11:08:09Z")

</div>

Thank you Colin for your thoughts.  
The idea behind this is the following.  
I want to "somewhat" protect my flows from being stolen.

I imagine encrypting my flows with OpenSSL into a script and decrypting them before NR reads them, once NR starts, launch an exec node that re-encrypts those same flows.  
Also for the Deploy, I would like to have an automatic encryption once the flows are written and saved in the .json. This point is the least important.

The mechanics are not yet finalized, but it's the idea that if the map is stolen, it's not so easy to read the json file with a simple editor.  
You will also have to find the decryption key. Not everything is accessible at once.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 September 2021 11:15 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/4 "2021-09-28T11:15:21Z")

</div>

Couldn't someone who has access to the editor just export the entire flow?

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 11:18 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/5 "2021-09-28T11:18:45Z")

</div>

hello @zenofmud  
The editor is password protected. It's should be enough. The people just use de /ui

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 September 2021 11:21 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/6 "2021-09-28T11:21:01Z")

</div>

I seem to remember reading that the filing system in node-red is pluggable, so one could replace that with your encrypt/decrypt code, but perhaps my memory is just of an intention to make it pluggable. Someone more knowledgeable would have to comment on that.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 September 2021 12:06 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/7 "2021-09-28T12:06:28Z")

</div>

> [@Jean-Luc](#):
>
> Run a private task before the nodered service is started ?

Yes, absolutely. Colin has given one suggestion but another would be to put the task into Node-RED's `settings.js` file _above_ the exports line. I do this regularly to capture some information that I want to be able to use later. That information can be fed into Node-RED flows via the globals property in the settings.

> [@Jean-Luc](#):
>
> Launch a script when using the Deploy button

Yes, this is possible too. But in this case, you need a custom node.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 September 2021 12:07 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/8 "2021-09-28T12:07:34Z")

</div>

> [@Colin](#):
>
> I seem to remember reading that the filing system in node-red is pluggable, so one could replace that with your encrypt/decrypt code, but perhaps my memory is just of an intention to make it pluggable. Someone more knowledgeable would have to comment on that.

~~I don't know that the pluggable flows are ready yet? The events are in place though.~~

[Storage API : Node-RED (nodered.org)](https://nodered.org/docs/api/storage/)

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 16:44 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/9 "2021-09-28T16:44:54Z")

</div>

> [@TotallyInformation](#):
>
> `settings.js` file _above_ the exports line

Very interesting to be able to link everything from NR and not outside.  
When you say above the part \*module.exports = \* is it the place where for example I can define a variable? _process.env.test = "blabla";_  
What would be the format to launch a shell named for example decrypt.sh?  
On the command line it is: _sh decrypt.sh_  
Will NR launch the script synchronously to make sure that it can read the json files

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 September 2021 16:46 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/10 "2021-09-28T16:46:29Z")

</div>

The plugable storage linked to by @TotallyInformation must surely be the way to go. Then you can encrypt it there and don't need to worry about anything else.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 16:50 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/11 "2021-09-28T16:50:35Z")

</div>

Here I must say that I did not understand, it is completely beyond my knowledge.  
Are they talking about storage 🤔  
it must have another meaning in English.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 September 2021 16:53 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/12 "2021-09-28T16:53:08Z")

</div>

You may need to read up a little on node.js apps and how they work. The settings.js file is actually a node.js module that is loaded by Node-RED as it starts up.

Anything above (technically anything outside) the extports object is run before the export and any stored values will not be available to Node-RED unless you shove them into the exports section. That's what the globals property is there for, it exposes whatever you put in it as a global variable in Node-RED.

In your case, you might be able to decrypt the file there but you would have to try it as I don't know exactly where Node-RED reads the json file.

As Colin says, you will almost certainly be better off working with the [Custom Storage Module](https://nodered.org/docs/api/storage/) approach.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 September 2021 19:52 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/13 "2021-09-28T19:52:52Z")

</div>

> [@Jean-Luc](#):
>
> Are they talking about storage

Yes, disc (or whatever) storage for flows etc. It is possible to replace the code that node-red uses to write to disc storage, with your own code. If you do that then you can decrypt the flow file before passing it to node-red and you can encrypt when node-red gives it to you to save.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [28 September 2021 19:57 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/14 "2021-09-28T19:57:41Z")

</div>

there it is clearer. I'm going to dig in this direction

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [30 September 2021 15:27 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/15 "2021-09-30T15:27:28Z")

</div>

> [@TotallyInformation](#):
>
> but another would be to put the task into Node-RED's `settings.js` file _above_ the exports line. I do this regularly to capture some information that I want to be able to use later

I understand the idea of putting code there, I've read a lot of articles but for me it's like looking for something I don't know in the fog. Not easy 🤔  
I didn't know Node.js before I discovered NR.  
What I'm thinking of doing is just a script at startup that decrypts the files and then runs NR.  
Thanks for the advice though.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [8 October 2021 21:07 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/16 "2021-10-08T21:07:05Z")

</div>

> [@TotallyInformation](#):
>
> I do this regularly to capture some information that I want to be able to use later

Would it be possible to show me one or two examples of actions taken before the launch of NR.  
And the associated code used in the settings.js file.  
This will help me to get on the right track.  
Thanks for your help  
/JL

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 October 2021 00:30 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/17 "2021-10-09T00:30:23Z")

</div>

Here is the start of the settings.js for my dev instance:

```auto
const path = require('path') // Node core library. path library for cross-platform file system specs
const fs = require('fs') // The `https` setting requires the `fs` module.
//const _ = require('lodash'); // lodash (improved underscore) for JS utility fns

/** Save a PID file so that Node-RED can be easily restarted even when run manually */
const pid = process.pid
console.info('PID: ', pid)
process.env.node_red_pid = pid+''
fs.readdir('.', (err, files)=>{
    if (err) throw err

    for (var i = 0, len = files.length; i < len; i++) {
        var match = files[i].match(/.*\.pid/)
        if(match !== null) fs.unlink(match[0], (err) => {
            if (err) throw err
        })
    }

    fs.writeFile(`${pid}.pid`, `${pid}`, (err) => {
        if (err) throw err
    })
})

// const Gun = require('gun')
// const thoughts = Gun().get('thoughts')
// thoughts.map().on(function(thought, id){
// console.log('A THOUGHT OCCURED: ', thought)
// })

/** Optionally display the Node.js/v8 engine heap size on startup */
const v8 = require('v8')
console.info(`V8 Total Heap Size: ${(v8.getHeapStatistics().total_available_size / 1024 / 1024).toFixed(2)} MB`)
let mem = process.memoryUsage()
const formatMem = (m) => ( m/1048576 ).toFixed(2)
console.info(`Initial Memory Use (MB): RSS=${formatMem(mem.rss)}. Heap: Used=${formatMem(mem.heapUsed)}, Tot=${formatMem(mem.heapTotal)}. Ext C++=${formatMem(mem.external)}`)
 
const nrsettings = {
    // ,,, the settings ...
} // ---- End of exports ---- //

module.exports = nrsettings

```

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [9 October 2021 18:40 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/18 "2021-10-09T18:40:37Z")

</div>

With an example, it becomes clearer. I appreciate your help.  
I will see how to launch a shell script from javascript commands.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c8879cfc4781b48db02a36567fa87f09b28680b.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 October 2021 20:53 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/19 "2021-10-09T20:53:39Z")

</div>

Certainly worth getting to know JavaScript and Node.js a bit more.

The structure of modules and how they work is especially useful to understand.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 October 2021 20:54 UTC](https://discourse.nodered.org/t/run-task-before-nr-startup-and-when-you-click-on-the-deploy-button/51613/20 "2021-10-23T20:54:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
