簡體   English   中英

node.js 中的 Python-Shell 未在 Systemd 服務中運行

[英]Python-Shell in node.js not running in Systemd service

介紹

我正在嘗試讓 alexa 與 Dragonboard 通信以打開 LED。 我正在使用aws 的 iot 核心 javascript sdk來監聽 mqtt 調用和python-shell來執行 python 腳本來使 LED 閃爍。 我將 device-example.js 修改為如下所示

設備示例.js

/*
 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

//node.js deps

//npm deps

//app deps
const deviceModule = require('..').device;
const cmdLineProcess = require('./lib/cmdline');

var PythonShell = require('python-shell')


//begin module

function processTest(args) {
   //
   // The device module exports an MQTT instance, which will attempt
   // to connect to the AWS IoT endpoint configured in the arguments.
   // Once connected, it will emit events which our application can
   // handle.
   //
   const device = deviceModule({
      keyPath: args.privateKey,
      certPath: args.clientCert,
      caPath: args.caCert,
      clientId: args.clientId,
      region: args.region,
      baseReconnectTimeMs: args.baseReconnectTimeMs,
      keepalive: args.keepAlive,
      protocol: args.Protocol,
      port: args.Port,
      host: args.Host,
      debug: args.Debug
   });

   var timeout;
   var count = 0;
   const minimumDelay = 250;

   if (args.testMode === 1) {
      device.subscribe('topic_1');
   } else {
      device.subscribe('topic_2');
   }
   if ((Math.max(args.delay, minimumDelay)) !== args.delay) {
      console.log('substituting ' + minimumDelay + 'ms delay for ' + args.delay + 'ms...');
   }
   timeout = setInterval(function() {
      count++;

      if (args.testMode === 1) {
         device.publish('topic_2', JSON.stringify({
            mode1Process: count
         }));
      } else {
         device.publish('topic_1', JSON.stringify({
            mode2Process: count
         }));
      }
   }, Math.max(args.delay, minimumDelay)); // clip to minimum

   //
   // Do a simple publish/subscribe demo based on the test-mode passed
   // in the command line arguments.  If test-mode is 1, subscribe to
   // 'topic_1' and publish to 'topic_2'; otherwise vice versa.  Publish
   // a message every four seconds.
   //
   device
      .on('connect', function() {
         console.log('connect');
      });
   device
      .on('close', function() {
         console.log('close');
      });
   device
      .on('reconnect', function() {
         console.log('reconnect');
      });
   device
      .on('offline', function() {
         console.log('offline');
      });
   device
      .on('error', function(error) {
         console.log('error', error);
      });
   device
      .on('message', function(topic, payload) {
         console.log('message', topic, payload.toString());
     if(payload.toString() === 'on') {
         PythonShell.run('blink_led.py', { uid:0 }, function(err, result) {
         if(err) throw err;
             console.log('finished');
             });
     }
      });

}

module.exports = cmdLineProcess;

if (require.main === module) {
   cmdLineProcess('connect to the AWS IoT service and publish/subscribe to topics using MQTT, test modes 1-2',
      process.argv.slice(2), processTest);
}

我寫了一個簡單的腳本來執行javascript

啟動文件

# stop script on error
set -e
# run pub/sub sample app using certificates downloaded in package
printf "\nRunning pub/sub sample application...\n"
node node_modules/aws-iot-device-sdk/examples/device-example.js --host-name=a1v3w5qj5oveq1.iot.us-east-1.amazonaws.com --private-key=/home/linaro/software/aws-iot/rube.private.key --client-certificate=/home/linaro/software/aws-iot/rube.cert.pem --ca-certificate=/home/linaro/software/aws-iot/root-CA.crt

當我執行腳本時,每個組件都會運行,腳本接收 mqtt 調用,然后打開 LED。 當我嘗試將腳本轉換為基本的 systemd 服務時,不會執行 python。 systemd 腳本如下

物聯網服務

[Unit]
Description=AWS IOT Listener
After=network.target

[Service]
Type=simple
User=linaro
ExecStart=/usr/bin/env sudo /home/linaro/software/aws-iot/start.sh

[Install]
WantedBy=multi-user.target

作為服務,接收 MQTT 調用,但根本不調用 python。 不會拋出任何錯誤。 當腳本作為 systemd 服務調用時,如何調用 python?

附加信息:系統:Debian jessie

運行 ./start.sh 的結果(不是服務)

Running pub/sub sample application...
connect
message topic_1 on
finished 

“完成”來自python腳本

作為服務運行的結果

● iot.service - AWS IOT Listener
   Loaded: loaded (/lib/systemd/system/iot.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-05-28 06:08:42 UTC; 19s ago
 Main PID: 2190 (sudo)
   CGroup: /system.slice/iot.service
           ├─2190 sudo /home/linaro/software/aws-iot/start.sh
           ├─2191 sh /home/linaro/software/aws-iot/start.sh
           └─2192 node node_modules/aws-iot-device-sdk/examples/device-exampl...

May 28 06:08:42 linaro-alip sudo[2190]:   linaro : TTY=unknown ; PWD=/ ; USE...h
May 28 06:08:42 linaro-alip sudo[2190]: pam_unix(sudo:session): session open...)
May 28 06:08:42 linaro-alip env[2190]: Running pub/sub sample application...
May 28 06:08:44 linaro-alip env[2190]: connect
May 28 06:09:00 linaro-alip env[2190]: message topic_1 on
Hint: Some lines were ellipsized, use -l to show in full.

不確定究竟是什么解決了問題,但這是我為使其工作所做的工作

  1. 將python腳本的路徑設置為絕對浴
  2. 更改服務腳本調用javascript文件目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM