Pendrive/SD card file copy

Hi All,

I would like my node-red flow to copy some files to a pendrive or an SD card as soon as I plug it into the raspberry pi. I understand that this is more of a linux question how to detect a new device and mount the pendrive, but has anyone come across this before?
Can you recommend a good solution for this?


Yes, as you suggest, do it in Linux. Use udev rule to trigger a simple script to copy the files. No need for Node-RED

I used this earlier, it worked fine, I got notifications for usb device connections/disconnections

1 Like

Just an example flow to make it easier

[{"id":"7bb2d60f.c90478","type":"inject","z":"ff90d3c.832f33","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"true","payloadType":"bool","x":150,"y":60,"wires":[["f0e57ab4.99c038"]]},{"id":"322becb9.ad3c24","type":"inject","z":"ff90d3c.832f33","name":"Stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":110,"wires":[["f0e57ab4.99c038"]]},{"id":"988b91a2.8c941","type":"debug","z":"ff90d3c.832f33","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":90,"wires":[]},{"id":"f0e57ab4.99c038","type":"function","z":"ff90d3c.832f33","name":"","func":"var usbDetect = global.get('usbDetect');\n\nif(msg.payload){\n    usbDetect.startMonitoring();\n    node.warn('started usb monitoring');\n    \n    usbDetect.on('add', function(device) {\n        device.state = \"connected\";\n        node.send(device);\n    });\n    \n    usbDetect.on('remove', function(device) {\n        device.state = \"removed\";\n        node.send(device);\n    });\n}\n\nif(!msg.payload){\n    usbDetect.stopMonitoring();\n    node.warn('stopped usb monitoring');\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":90,"wires":[["988b91a2.8c941"]]}]

Thanks a lot. Do you remember how you installed the package. I used

npm install usb-detection

on my .node-red folder, but it would not install. Stops during complication because libudev.h is missing.

Further down in the readme you will find the instruction below. You need those as well since npm will build the correct version for you as part of the install


You know what you need for you system, basically your appropriate analog of build-essential. Keep rocking!

To compile and install native addons from npm you may also need to install build tools (source) :

sudo apt-get install -y build-essential

Also install libudev:

sudo apt-get install libudev-dev

Oh sorry indeed. The component is now installed. I am still getting "TypeError: Cannot read property 'startMonitoring' of undefined" when clicking on start.
Do I need to add the usbdetect to settings.js as a global function as well?

Yes of course, I forgot to mention that


Thanks. I keep looking into this, because since I added the usb-detection to the settings.js my node-red keep restarting from time-to-time:

nodered.service: main process exited, code=killed, status=11/SEGV
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.

The code is working, as I can see some of the usb messages in the debug, but does make the node-red service to fall after about 10 seconds or so.

Well, in my setup it works without problems, no node-red restarts at all, it just runs
I have node-red v 1.1.2

Can you show the few lines around that in settings.js ? Chances are a missing or extra ,

functionGlobalContext: {
        // os:require('os'),
        // octalbonescript:require('octalbonescript'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
        // usbDetect:require('usb-detection')
        // moment:require('moment')

I commented the line now, but previously it was only the usbDetect line uncommented.
And it was working for a while as I could see the message in the debug.

deleted message

Run node-red-log in a terminal and leave it running while you make it crash. Likely there will be something meaningful there.
Show us the full text from node-red start to crash, use the </> button above the forum text window when pasting the text here.

Check my post above. These were the only 3 meaningful lines in the node-red-log. Previous to that it was just regular messages (like connected to MQTT and such) and after these 3 lines the normal start-up message sequence. Nothing else.

Show us the whole log from start to crash please.

Could it be this part that is causing your problems? Does your flow already start to write files to the usb device too quickly after it has been recognized? If so, disable that part and just check if the removal/insertion itself causes the crash

Find the entire log from the beginning attached. Node-Red has restarted 3 times in a span of just a few minutes. I tried it without having the pendrive inserted or not, but it does not make a difference. So far I only have the test flow running, I am not even accessing or mounting the pendrive. Just plug it in and that's it.
noderedlog.txt (26.0 KB)

There appear to be conflicting statements there. First you say that it doesn't make a difference whether the pendrive is inserted or not, then you say "plug it in and that's it".
Something is killing node-red but it isn't obvious what. Run sudo journalctl -f then if it is necessary to have the drive in then plug it in. See if there is anything interesting in the journal. If not certain then post that too, from node red start to crash. The node red start messages should be there but there may be some relevant system messages too.
I presume you haven't got a watchdog of some sort running that triggers a restart of node-red have you?

[Edit] Just noticed you are getting a SEGV error, which means a segmentation fault. Something is crashing horribly. Hopefully the journal will give some clues.

[More edits] This looks a bit concerning, though it may not be related it does suggest it may be a bit flaky Calling startMonitoring() and stopMonitoring() multiple times causes segmentation fault (Linux, macOS) · Issue #57 · MadLittleMods/node-usb-detection · GitHub

Doesn't Raspbian automatically mount a usb stick when you plug it in? You should be able see that by running
sudo journalctl -f
and plugging the stick in. I would it expect it to mount at something like /media/pi/. In which case perhaps you could use a watch node to see it being mounted instead of the usb detection node.

1 Like