如何使用Node.js访问crontab文件?



我目前正在开发一个电子应用程序,需要它查看crontab文件并解析它.

Node.js(或电子中的其他东西)如何访问主crontab文件并读取它?

我查看了cron-parser库,但它只能读取cron格式的字符串,不能访问文件。

提前感谢。

由于我们在运行时看到的crontab文件只是一个tmp文件,其中显示当前用户创建的任何cron作业(存储在/var/spool/cron/crontabs中),当您通常无法访问这些文件时,我建议您运行shell脚本来获取这些数据:

const { exec } = require("child_process");
exec("crontab -l", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
//Do stuff with the read string on stdout
});

希望这能给你运行节点脚本的用户下的crontab的内容

最新更新