Python-Shell 在 node.js 中而不是 运行 在 Systemd 服务中

Python-Shell in node.js not running in Systemd service

简介

我正在尝试让 alexa 与 dragonboard 通信以打开 LED。我正在使用 aws' iot core javascript sdk to listen for mqtt calls and python-shell 执行 python 脚本来使 LED 闪烁。我将 device-example.js 修改为如下所示

设备-example.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

start.sh

# 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脚本如下

iot.service

[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 

"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 文件目录