Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
Nodejs App doesnt spawn python child process when Nodejs app started using Systemd

I'd like to start my node js application on boot. Therefore I start a service from Systemd:

[Unit]
Description=Node.js server
After=network.target

[Service]
ExecStart=/usr/bin/node /var/www/Raspberry-Pi-Status/js/server.js
Restart = always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-server
Environment=NODE_ENV=production PORT=8000
Environment=PYTHONPATH=/usr/bin/python

[INSTALL]
WantedBy=multi-user.target

The server.js looks like this:

var util = require('util'),
spawn = require('child_process').spawn,
py = spawn('python',['temperature.py'],{detached: true});
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'monitor',
password : 'password',
database : 'temps'});


var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  fs = require('fs'),
  sys = require('util'),
  exec = require('child_process').exec,
  child;

// Listen on port 8000
app.listen(8000);
// If all goes well when you open the browser, load the index.html file
function handler(req, res) {

  fs.readFile(__dirname+'/../index.html', function(err, data) {
    if (err) {
    // If no error, send an error message 500
        console.log(err);
        res.writeHead(500);
        return res.end('Error loading index.html');
    }
    res.writeHead(200);
    res.end(data);
  });

}


py.stdout.on('data', function(data){
  console.log('testing');
  date = new Date().getTime();
  temp = parseFloat(data);
  io.sockets.emit('temperatureUpdate',date,temp);
});


// When we open the browser establish a connection to socket.io.
// Every 5 seconds to send the graph a new value.

io.sockets.on('connection', function(socket) {
   console.log('user connected');
});

The node.js application should start a python script which reads out a temperature sensor. When I start node.js via the console everything works fine. However, when I start from Systemd, the python script is not spawned. What's the problem there? Am I missing something?

Thanks in advance Alexander

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

An issue could be the difference in the current working directory when run manually vs systemd. The spawn call used is documented has a default to inherit the current working directory.

When run via the shell, that would be whatever directory you are currently in. In man systemd.exec, you find the "WorkingDirectory=` directive, which documents systemd's default current working directory: "defaults to the root directory when systemd is running as a system instance".

So if your temperature.py is located in /var/www/Raspberry-Pi-Status, then set:

  workingDirectory=/var/www/Raspberry-Pi-Status in your `[Service]` section. 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!