防止系统进入睡眠模式的 node.js 程序

7

我想知道是否有可能让一个Node.js程序/脚本阻止操作系统(Windows)进入睡眠或休眠模式?


你可以通过在特定时间间隔内向Node.js服务器发送请求的方式来实现。 - Alok Deshwal
2个回答

3
这将对你有所帮助。 https://www.npmjs.com/package/stay-awake
var stayAwake = require('stay-awake');

// do something not so important 

// prevent auto sleep 
stayAwake.prevent(function(err, data) {
    // handle error 
    console.log('%d routines are preventing sleep', data);
});

// do something which needs the computer to stay awake 

// do something async 
doAsync(function() {
    // do something 
    stayAwake.prevent(function() {}); // second call 
    // do long processing 
    stayAwake.allow(function() {}); // this subroutine no longer needs to prevent sleep 
});

// once done allow the computer to sleep as configured in power management 
stayAwake.allow(function(err, data) {
    if(data == 0) {
        console.log('Will sleep automatically');
    }
}); // allow

1
此软件包不支持Linux操作系统。 - itsezc
这个插件在Node 14上不再起作用(这是我的经验)。 我认为这个仓库的正确URL是:https://github.com/dapetcu21/node-stay-awake。 - riofly

1

这不支持OSX操作系统。 - Karl Adler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接