-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcron_exec_process.js
More file actions
60 lines (53 loc) · 1.67 KB
/
cron_exec_process.js
File metadata and controls
60 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ### Модуль запуска процесса по времени
*
* @module cron_exec_process
*
* Created 02.07.2019
*/
'use strict';
const { CronJob } = require('cron');
const { exec } = require('child_process');
const moment = require('moment');
const yargs = require('yargs')
.usage('Usage: $0 [options] <command>')
.demand(1)
.strict()
.alias('i', 'interval').nargs('i', 1).describe('i', 'Interval or the time at which the cron job is needed to run')
.version('v', 'Show version', '0.0.1').alias('v', 'version')
.help('h').alias('h', 'help')
.example('node cron_exec_process job echo "Hi"', 'Execute process with job `echo "Hi"`')
.command(
'job [name]',
'Execute process with job `name`',
yargs => yargs.positional('name', {type: 'string', describe: 'Empty job name'}),
args => {
const { i, name } = args;
if (name) {
// по умолчанию каждые 5 минут
const interval = i || '1 */5 * * * *';
function exec_process() {
console.log(`execute at ${moment().format('DD MMMM YYYY, HH:mm:ss')}`);
exec(name, {env: process.env}, (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
}
console.log('cron job started');
new CronJob(interval, exec_process, null, true);
}
else {
yargs.showHelp();
process.exit(1);
}
}
)
.epilog('\nMore information about the library: https://github.com/oknosoft/windowbuilder');
const {argv} = yargs;
if(!argv._.length){
yargs.showHelp();
process.exit(1);
}