pyt*_*hon 5 django bash kill-process gulp gulp-watch
我创建了一个gulpfile.js启动我的服务器,其内容可以在下面看到.
gulp.task('default', function () {
if(!fs.statSync('/etc/aptly.conf').isFile()){
process.exit();
return;
}
console.info('Starting static file server SimpleHTTPServer on 0.0.0.0:8080');
aptly_static = spawn('python', ['-m', 'SimpleHTTPServer', '8080'], {'cwd': '/opt/aptly/public', 'stdio': 'inherit'});
console.info('Starting Django runserver on 0.0.0.0:8000');
django = spawn('python', ['manage.py', 'runserver', '0.0.0.0:8000'], {'stdio': 'inherit'});
console.info('Starting Aptly api serve on 0.0.0.0:4416');
aptly_api = run('aptly api serve -listen="0.0.0.0:4416"').exec().pipe(gulp.dest('/tmp/aptlylog'));
return watchLess('src/**/*.less')
.pipe(debug())
.pipe(reLess)
.pipe(gulp.dest('dist/dist'));
Run Code Online (Sandbox Code Playgroud)
问题是如果由于任何原因导致较少的预处理器崩溃,gulpfile.js守护程序退出的很差.子进程python manage.py runserver python -m SimpleHTTPServer aptly api serve仍将运行.
我不得不通过使用ps -aux | grep runserver类似的方法来煞费苦心地终止这些,以找到要删除的PID sudo kill -9 $PID.
如果我的gulpfile.js意外崩溃,有没有办法直接杀死所有进程?
使用递归函数向所有子进程发送终止信号。创建以下 ./killChilds.sh 文件
#!/usr/bin/env bash
function KillChilds {
local pid="${1}" # Parent pid to kill childs
local self="${2:-false}" # Should parent be killed too ?
if children="$(pgrep -P "$pid")"; then
for child in $children; do
KillChilds "$child" true
done
fi
# Try to kill nicely, if not, wait 15 seconds to let Trap actions happen before killing
if ( [ "$self" == true ] && kill -0 $pid > /dev/null 2>&1); then
kill -s TERM "$pid"
if [ $? != 0 ]; then
sleep 15
kill -9 "$pid"
if [ $? != 0 ]; then
return 1
fi
else
return 0
fi
else
return 0
fi
Run Code Online (Sandbox Code Playgroud)
}
在 bash 中使用以下命令获取文件
source ./killChilds.sh
Run Code Online (Sandbox Code Playgroud)
您可以使用它来杀死控制台中的整个进程树
killChilds $pid
Run Code Online (Sandbox Code Playgroud)
其中$pid是主进程。您可能想将该文件包含到 ~/.bashrc 中,这样您就不必每次都获取它。
| 归档时间: |
|
| 查看次数: |
1675 次 |
| 最近记录: |