Skip to content

Instantly share code, notes, and snippets.

@iamspark1e
Last active February 18, 2025 06:41
Show Gist options
  • Select an option

  • Save iamspark1e/daef45c52772674bb745cd2a30de8f1a to your computer and use it in GitHub Desktop.

Select an option

Save iamspark1e/daef45c52772674bb745cd2a30de8f1a to your computer and use it in GitHub Desktop.

README

Reference: https://github.com/hkfires/Keep-Serv00-Alive

这个方案利用了Serv00自带Apache服务器的Phusion Passenger插件功能,每次访问网页时可以唤醒Nodejs程序,因而不需要借助Cron就能够进行保活,自然没有了被杀Cron计划任务的烦扰

  1. 删除自带的域带有的网站,<your_username>.serv00.net,勾选“Delete(purge website files)”
  2. 重新添加<your_username>.serv00.net,选择Advanced,类型选择Nodejs,使用最高版本如Node V22
  3. SSH,前往目录 ~/domains/<your_username>.serv00.net/public_nodejs
  4. 由于Serv00的Apache设置的是静态优先,因而此处public文件夹下不能有index.html,否则会显示静态页面,而不会执行nodejs程序,我选择的是直接将public改名为static,执行mv public static
  5. npm22 install express,并在此目录创建app.js
  6. 部署完成,可以访问一次<your_username>.serv00.net来激活,在对应的website可以看到log
const express = require("express");
const path = require("path");
const exec = require("child_process").exec;
const app = express();
const port = 3000;
app.use(express.static(path.join(__dirname, "static")));
function isProcessRunning(processName, callback) {
exec(`ps -ax | grep ${processName} | grep -v grep`, (error, stdout) => {
if (error) {
callback(false);
return;
}
callback(stdout.trim().length > 0);
});
}
function keepWebAlive(pName, startupCmd) {
return function () {
const currentDate = new Date();
const formattedDate = currentDate.toLocaleDateString();
const formattedTime = currentDate.toLocaleTimeString();
isProcessRunning(pName, (isRunning) => {
if (isRunning) {
console.log(`${formattedDate}, ${formattedTime}: Web Running`);
} else {
exec(startupCmd, (err, stdout) => {
console.log(stdout);
if (err) {
console.log(
`${formattedDate}, ${formattedTime}: Keep alive error: ${err}`
);
} else {
console.log(
`${formattedDate}, ${formattedTime}: Keep alive success!`
);
}
});
}
})
};
}
// 监测pm2
// 原则上 `nohup alist server >/dev/null 2>&1 &` 也应该支持,但是不知道为什么alist不能这样启动,pm2可以
setInterval(
keepWebAlive("pm2", `pm2 resurrect`),
10 * 1000
);
app.listen(port, () => {
console.log(`Server is listening on port ${port}!`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment