【发布时间】:2016-08-31 15:48:53
【问题描述】:
我有一个名为 update.sh 的脚本来更新服务器。
问题是;当我停止 PM2 进程时,它会在那里结束 bash 进程(?)。
我不知道如何从未链接到父级的父级生成进程。 :-S
update.sh
#!/bin/bash
# Check we are not already updating.
if [ -f update.lock ]; then
echo "Already updating. Locked."
exit
fi
# Make the log file.
rm update.log
touch update.log
# Create update lock.
touch update.lock
echo "Cleaning up" | tee update.log
rm -rf the-last-word
rm -rf Server
echo "Retrieving files from BitBucket" | tee update.log
git clone git@bitbucket.org:dotdot/dot-project.git
# Remove all directories except Server (So we don't have game source code on the Server)
echo "Removing game source code" | tee update.log
cd the-last-word
# Move the Server directory one up.
mv Server ../
cd ../
rm -rf the-last-word
# Stop the previous server.
# == PM2 stop some server name.
echo "Stopping server & removing" | tee update.log
pm2 delete "Some App" | tee update.log
# Update the current & run.
echo "Installing node modules" | tee update.log
cd Server
npm install | tee update.log
# Start the server again
# == PM2 start some server name.
echo "Starting node server" | tee update.log
NODE_ENV=production pm2 start bin/www.js --name "Some App" | tee update.log
# Remove lock file.
cd ..
rm update.lock
现在我使用节点(快速路线)运行此脚本。
admin.js
// Update server
app.use("/admin/update", authMiddleware, function(req, res, next) {
console.log('Updating server from script.');
const { spawn } = require('child_process');
const deploySh = spawn('sh', [ 'update.sh' ], {
cwd: '/www/thelastword/',
env: Object.assign({}, process.env, { PATH: process.env.PATH + ':/usr/local/bin' })
});
res.status(200).json('Updating Server');
});
【问题讨论】:
-
您可以尝试使用
& or hupup在子shell 中启动pm2 delete命令,例如:pm2 delete "Some App" | tee update.log &一旦文件被写入子进程,将被删除而不是原来的bash进程并希望执行将继续。
标签: node.js bash shell express pm2