
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
bgbash is a partial drop-in replacement for require('child_process').exec specifically made for long-running applications.
npm i bgbash --save
Starting a child process using spawn or exec will create a new instance of ChildProcess, open several streams and create a new system background process. This is very expensive if you do it often. bgbash starts a single background process on the the first request, every future request will use that process's stdin to execute commands. Reducing the startup and feedback time for subsequent calls.
| "echo hi" - 2000 runs on node-v10.12.0(darwin) | node.js | bgback | notes |
|---|---|---|---|
| startup | 10.98ms | 16.05ms | 46% slower - The startup is naturally slower as it does a little more. |
| repeat response | 4.91ms | 1.96ms | 251% faster - But repeat calls are significantly faster, |
| repeat user | 1.31ms | 0.95ms | 137% faster - with part of it coming from the reduced user execution time ... |
| repeat system | 0.64ms | 0.04ms | 1457% faster - ... and a significantly reduced system execution time. |
| rss | 37.9Mb (avg. 19.8Mb) | 9.9Mb (avg. 6.6Mb) | With a significantly lower rss memory allocation (which is stable even with more calls) |
| heap total | 37.5Mb (avg. 21.4Mb) | 6Mb (avg. 3.2Mb) | The node.js version also fills up the heap a lot quicker to a avg. 32Mb use at 10000 execs while the bgback version needs around 20000 to get there. |
| heap used | 19.5Mb (avg. 6.3Mb) | 4.1Mb (avg. 1.4Mb) | The difference in size can be attributed to the additional code loaded. This will slightly grow with number of calls (~20kb per 5000 calls). Reason is unclear but consistent for both the node.js and bgback version. |
| c-memory inc. | 8.6Kb (avg. -201b) | 170b (avg. -7.8Kb) | The C++ memory can be negative as some initial c memory is cleared. |
Note: This data is compiled using the ./perf.js script.
The API of exec is implemented to be a reasonable (but not feature complete) drop-in replacement for node's native exec.
const { exec } = require('bgbash')
const cmd = 'echo hi'
const opts = { // (optional)
cwd: '.', // (optional) Path in which the code is to be executed, defaults to `process.cwd()`.
env: { KEY: 'value' } // (optional) Environment variables to be used for the execution.
timeout: 100, // (optional) Time in milliseconds until a timeout appear, defaults to `0` = no timeout.
encoding: 'utf8', // (optional) Encoding to be used, an unknown or `null` encoding returns a Buffer.
}
exec(cmd, opts, (error, stdout, stderr) => {
error // Error that might occur, i.e. TIMEOUT or of the process died.
stdout // Output of the execution
stderr // Error of the execution
})
A notable incompatibility is that stderr will be empty, even though output might exist, if no error occurs.
This is done for performance reasons.
The background process will continue to run forever. If you want the process to close, you have to run close().
const { exec, close } = require('bgbash')
exec('echo hi')
close( // Will be executed after the previous command completed!
() => {
// All closed!
}
)
Much like node js, bgbash also comes with a Promise API that is available using either require('bgbash').promises
or require('bgbash/promises').
const { exec } = require('bgbash').promises
const opts = { /* Same options as above. */ }
const { stdout, stderr } = await exec('echo hi', opts)
try {
await exec('exit 1', opts)
} catch (error) {
// Error that happened.
}
FAQs
Running a background bash to reduce the amount of child processes.
The npm package bgbash receives a total of 0 weekly downloads. As such, bgbash popularity was classified as not popular.
We found that bgbash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.