
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Like spawn('bash', [ '-c', cmd ])
, but with better error handling.
npm i -S bash-exec
Why you possibly don't want to use this library!
Every bash command is being ran in separate process, which continues to work even when your app finished. For example, if you ran bashExec('cat /dev/urandom > /tmp/urandom-mirror')
in your app and kill it (the app), you will see soon that your disk space ran out.
To kill all the generated bash processes before exit use command bashExec.killChilds([ SIGNAL = 'SIGINT' ])
. It would be nice to define a listener of system signals that would call the function before the app finishing.
Btw, you can call the function more that once but if you do this you probably doing something wrong.
You can find example of forgotten backgroung process in the file ex5-without-killChild.es6
, and example of correct finishing background processes before exit in examples/ex6-with-killChild
.
Возможно вам не стоит пользоваться этой библиотекой!
Каждая запущенная вами bash команда запускается в отдельном процессе, который продолжить работать после завершения работы вашего приложения. Напремер: если вы запустите в своем приложении bashExec('cat /dev/urandom > /tmp/urandom-mirror')
и остановите свой приложении, то очень скоро у вас закончится место на диске.
Для того чтобы, перед выходом из приложения, завершить все запущенные bash команды воспользуйтесь функцией bashExec.killChilds([ SIGNAL = 'SIGINT' ])
. Хорошей мыслью будет: определить слушателя системных сигналов, который вызовет функцию bashExec.killChilds
перед завершением приложения. Заметка, эту функцию можно вызвать более одного раза, но мне кажется, что если вам захочется это сделать, то вы пользуетесь библиотекой неправильно.
Пример образования "забытого" в фоне процесса вы можете найти в файле ex5-without-killChild.es6
. А пример корректного завершения фоновых процессов перед выходом в файле examples/ex6-with-killChild
.
bashExec(cmd[, options ]) -> Promise
:
cmd
- a string containing the command(s)options
- spawn options, detached
option will be ignored. Default: { detached: true }
.bashExec.killChilds([ signal ]) -> undefined
:
signal
- a string containing the signal name. Default: SIGINT
import bashExec from 'bash-exec'
import { homedir } from 'os'
let cmd = ` \
set -o pipefail \
&& cd "${homedir()}/Pictures" \
&& find . -name "*.png" \
| (wc -l || echo 0)`
bashExec(cmd).then(console.info)
/*
{ cmd: 'set -o pipefail && cd "/home/nskazki/Pictures" && find . -name "*.png" | (wc -l || echo 0)',
code: 0,
signal: null,
stdout: '109',
stderr: '',
cmdId: 1 }
*/
import bashExec from 'bash-exec'
bashExec(`exit 1`).catch(console.error)
/*
{ [Error: bashExec problem - onExit!
cmd: exit 1
code: 1
signal: null
stdout:
stderr:
cmdId: 1]
cmd: 'exit 1',
code: 1,
signal: null,
stdout: '',
stderr: '',
cmdId: 1 }
*/
import { delay } from 'bluebird'
import uuid from 'node-uuid'
import bashExec from 'bash-exec'
let marker = `im-just-marker-${uuid.v4()}`
let runCmd = bashExec(`sleep 300 && echo "${marker}"`)
let getPid = delay(1e3).then(() => bashExec(`ps aux | grep -v grep | grep '${marker}' | awk '{print $2}'`))
getPid.then(({ stdout: pid }) => bashExec(`kill ${pid}`)),
runCmd.catch(console.error)
/*
{ [Error: bashExec problem - onExit!
cmd: sleep 300 && echo "im-just-marker-301849df-ab38-4917-882b-00319d55ceb2"
code: null
signal: SIGTERM
stdout:
stderr:
cmdId: 1]
cmd: 'sleep 300 && echo "im-just-marker-301849df-ab38-4917-882b-00319d55ceb2"',
code: null,
signal: 'SIGTERM',
stdout: '',
stderr: '',
cmdId: 1 }
*/
If you want to perform some example:
$(npm bin)/babel-node examples/ex0.es6
If you want to perform auto test:
npm test
If you want to debug the process:
DEBUG=libs-bash-exec*,debugEvents* node you-app.js
FAQs
like spawn('bash', [ '-c', cmd ]), but with better error handling
The npm package bash-exec receives a total of 5 weekly downloads. As such, bash-exec popularity was classified as not popular.
We found that bash-exec 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.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.