当前位置:网站首页>NJS triggers system command operation

NJS triggers system command operation

2022-06-24 01:16:00 Huangxitong

NginScript There is no ability to directly invoke operating system commands for the time being , But with fs Object can manipulate the local file system , When local commands need to be triggered, messages can be delivered through the file system , Such as this :

function execCommand(cmd){

var fs = require('fs');

var path;

if(cmd == "start") path='/dev/shm/start-command';

if(cmd == "stop") path='/dev/shm/stop-command';

var file = fs.writeFileSync(path, '');

}

When you need to operate, it is on the memory disk /dev/shm Write an empty file ( Overwrite if it already exists ), The file name is the name of the command you started .

Install a file monitoring tool

yum install inotify-tools -y

#!/bin/bash
inotifywait -m -e open /dev/shm/ |
while read events;
do
    if [ "$events" == "/dev/shm/ OPEN start-command" ]; then 
        echo 'start command';
        do-something-to-start-service;
    fi
    if [ "$events" == "/dev/shm/ OPEN stop-command" ]; then
        echo 'stop command';
        do-something-to-stop-service;
    fi
done

No technical content . It is not beautiful to realize this through the file system , Make do with .

原网站

版权声明
本文为[Huangxitong]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211119173740501w.html