Created
October 18, 2019 14:59
-
-
Save ivanalejandro0/881c97df5afc1fdd1e86b2caf4e2d637 to your computer and use it in GitHub Desktop.
Spawn and kill a bash script from js. Handle signal from bash.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
trap 'handle_signal SIGINT' SIGINT | |
trap 'handle_signal SIGQUIT' SIGQUIT | |
trap 'handle_signal SIGTERM' SIGTERM | |
trap 'handle_signal SIGHUP' SIGHUP | |
trap 'handle_signal SIGKILL' SIGKILL # we can't handle this | |
handle_signal() { | |
echo "received: $1. Exiting..." | |
exit | |
} | |
count=0 | |
while true; do | |
sleep 1 | |
count=$(( $count + 1 )) | |
echo $count | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { spawn } = require('child_process'); | |
const path = require('path'); | |
const script = spawn(path.join(__dirname, 'counter.sh')); | |
// Log the script output | |
script.stdout.on('data', function(data) { | |
console.log(data.toString().trimEnd()); | |
}); | |
setTimeout(function() { | |
// script.kill('SIGTERM'); // works | |
// script.kill('SIGHUP'); // works | |
// script.kill('SIGKILL'); // works but script can't handle it | |
script.kill('SIGINT'); // works | |
}, 3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run this you'll need to:
Example run: