Python洞节点.js未在Systemd servi中运行

2024-05-13 22:48:58 发布

您现在位置:Python中文网/ 问答频道 /正文

简介

我想让一个alexa和一个dragonboard通信来打开一个LED。我使用aws' iot core javascript sdk来监听mqtt调用,python-shell来执行python脚本来闪烁LED。我修改了设备-示例.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

开始.sh

^{pr2}$

问题

当我执行脚本时,每个组件都会运行,脚本接收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

运行结果/开始.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.

Tags: theto脚本awslogtopiclicenseon
1条回答
网友
1楼 · 发布于 2024-05-13 22:48:58

不知道到底是什么解决了这个问题,但这里是我所做的让它工作

  1. 将python脚本的路径设置为绝对浴
  2. 更改服务脚本以调用javascript文件目录

相关问题 更多 >