-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f438f34
Showing
7 changed files
with
552 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 4 | ||
indent_style = space | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
[**.yml,**.ymal,**.styl] | ||
indent_size = 2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules | ||
.DS_Store | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
.yarn | ||
.eslintcache | ||
yarn-error.log |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 是否需要分号 | ||
semi: false | ||
# 单引号 | ||
singleQuote: true | ||
# 在对象,数组括号与文字之间加空格 "{ foo: bar }" | ||
bracketSpacing: true | ||
# 在对象或数组最后一个元素后面是否加逗号(在ES5中加尾逗号) | ||
trailingComma: all | ||
# 默认值。因为使用了一些折行敏感型的渲染器(如GitHub comment)而按照markdown文本样式进行折行 | ||
proseWrap: preserve | ||
# tab | ||
tabWidth: 4 | ||
# 一行的字符数,如果超过会进行换行,默认为80 | ||
printWidth: 120 | ||
|
||
# vueIndentScriptAndStyle: true | ||
jsxBracketSameLine: false | ||
jsxSingleQuote: false | ||
htmlWhitespaceSensitivity: strict | ||
endOfLine: lf | ||
|
||
# 覆盖配置 | ||
overrides: | ||
- files: | ||
- '*.yml' | ||
- '*.styl' | ||
options: | ||
tabWidth: 2 |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const os = require('os') | ||
const path = require('path') | ||
const child = require('child_process') | ||
const schedule = require('node-schedule') | ||
const osu = require('node-os-utils') | ||
const logger = require('pino')({ | ||
prettyPrint: true, | ||
}) | ||
|
||
/** | ||
* 休眠 | ||
* @param {number} second 秒 | ||
* @returns Promise | ||
*/ | ||
function sleep(second) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, second * 1000) | ||
}) | ||
} | ||
|
||
/** | ||
* 根据名称杀死进程 | ||
* @param {string} name 进程名称 | ||
* @returns Promise | ||
*/ | ||
function kill(name) { | ||
const cmd = process.platform === 'win32' ? 'tasklist' : 'ps aux' | ||
return new Promise((resolve, reject) => { | ||
child.exec(cmd, (err, stdout) => { | ||
if (err) { | ||
console.error(err) | ||
reject() | ||
return | ||
} | ||
stdout.split('\n').filter((line) => { | ||
const processMessage = line.trim().split(/\s+/) | ||
const processName = processMessage[0] | ||
if (processName === name) { | ||
process.kill(processMessage[1]) | ||
} | ||
}) | ||
|
||
sleep(1).then(resolve) | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* 计算 byte 到 GB | ||
* @param {number} bytes 字节数 | ||
* @returns GB 单位字符串 | ||
*/ | ||
function format(bytes) { | ||
return (bytes / 1024 / 1024 / 1024).toFixed(2) + 'GB' | ||
} | ||
|
||
// 读取命令行传入的参数 | ||
const [file = 'D:\\Applications\\YRMediaServer\\YRMediaServer.exe', cron = '0 0 0 * * *'] = process.argv.splice(2) | ||
|
||
// 主程序 | ||
async function main() { | ||
const cpuUse = await osu.cpu.usage() | ||
const cpuFree = await osu.cpu.free() | ||
|
||
// 打印系统基本信息 | ||
logger.info(`=====================================================`) | ||
logger.warn(`计划任务:${cron}`) | ||
logger.info(`当前时间:${new Date()}`) | ||
logger.info(`CPU:已用 %d%, 可用 %d%`, cpuUse, cpuFree) | ||
logger.info( | ||
`内存:已用 %s, 可用 %s, 共 %s`, | ||
format(os.totalmem() - os.freemem()), | ||
format(os.freemem()), | ||
format(os.totalmem()), | ||
) | ||
|
||
// kill | ||
await kill(path.basename(file)) | ||
logger.info('Kill: %s', path.basename(file)) | ||
|
||
// run | ||
child.exec(`cd ${path.dirname(file)} && start ${path.basename(file)}`) | ||
logger.info('Runing: %s', file) | ||
logger.info(`=====================================================\n`) | ||
} | ||
|
||
// 切换显示中文 | ||
child.execSync('chcp 65001') | ||
|
||
// 运行程序并执行计划任务 | ||
let job | ||
main().then(() => { | ||
function log() { | ||
logger.warn('下一次重启时间: %s\n', job.nextInvocation()) | ||
} | ||
|
||
// 设置定时任务 | ||
job = schedule.scheduleJob(cron, () => { | ||
main().then(log) | ||
}) | ||
|
||
log() | ||
}) | ||
|
||
// 监听 ctrl+c/ctrl+d 的事件 | ||
process.on('SIGINT', async () => { | ||
if (job) { | ||
job.cancel() | ||
} | ||
|
||
await kill(path.basename(file)) | ||
logger.warn('Exit Kill: %s', path.basename(file)) | ||
process.exit(0) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "restart", | ||
"version": "1.0.0", | ||
"description": "重启程序小工具", | ||
"main": "app.js", | ||
"author": "ifzm", | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"build": "pkg app.js --targets=node14-win-x64 --out-path=dist" | ||
}, | ||
"dependencies": { | ||
"node-os-utils": "^1.3.5", | ||
"node-schedule": "^2.0.0", | ||
"pino": "^6.11.3", | ||
"pino-pretty": "^5.0.2" | ||
} | ||
} |
Oops, something went wrong.