pythonnode.jslinuxsystemddragonboard

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


Intro

I am trying to have an alexa communicate with a dragonboard to turn on an LED. I am using aws' iot core javascript sdk to listen for mqtt calls and python-shell to execute a python script to blink the LED. I modified device-example.js to look as follows

device-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);
}

I wrote a simple script to execute the 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

Question

When I execute the script, every component functions, the script receives mqtt calls, then turns on the LED. When I try to turn the script into a basic systemd service, the python is not executed. The systemd script is below

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

As a service, MQTT calls are received, but the python is not called at all. No error is thrown. How can I have the python be called when the script is called as a systemd service?

Additional Information: System: Debian jessie

Result of running ./start.sh (not service)

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

"finished" comes from the python script

Result of running as a service

● 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.

Solution

  • Not sure what exactly fixed the problem but here is what I did to make it work

    1. Set the path to the python script to be the absolute bath
    2. Changed the service script to call the javascript file directory