我想在启动时启动我的node js应用程序。因此,我从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.targetserver.js如下所示:
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');
});node.js应用程序应该启动一个读取温度传感器的python脚本。当我通过控制台启动node.js时,一切正常。但是,当我从Systemd启动时,python脚本没有生成。有什么问题吗?我是不是遗漏了什么?
提前感谢,亚历山大
发布于 2017-04-29 00:32:04
一个问题可能是手动运行时当前工作目录与systemd的不同。使用的spawn调用是documented,默认情况下继承当前工作目录。
当通过shell运行时,这将是您当前所在的任何目录。在man systemd.exec中,您可以找到"WorkingDirectory=`指令,它记录了systemd的缺省当前工作目录:“当systemd作为系统实例运行时,缺省为根目录。
因此,如果您的temperature.py位于/var/www/Raspberry-Pi-Status中,则设置:
workingDirectory=/var/www/Raspberry-Pi-Status in your `[Service]` section. https://stackoverflow.com/questions/43682724
复制相似问题