Open horner opened 2 months ago
#!/bin/bash
SERVICE_NAME=ptyserver
SERVICE_FILE=/etc/systemd/system/$SERVICE_NAME.service
WORKING_DIRECTORY=$PWD
NODE_EXEC=$(which node) # Find the path to the Node.js executable
# Create the systemd service file
echo "[Unit]
Description=PtyServer Node.js Service
After=network.target
[Service]
WorkingDirectory=$WORKING_DIRECTORY
ExecStart=$NODE_EXEC PtyServer.js
Restart=always
User=$USER
Environment=NODE_ENV=production
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=$SERVICE_NAME
[Install]
WantedBy=multi-user.target
" | sudo tee $SERVICE_FILE > /dev/null
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable the service to start on boot
sudo systemctl enable $SERVICE_NAME.service
# Start the service immediately
sudo systemctl start $SERVICE_NAME.service
echo "Service $SERVICE_NAME has been set up and started."
I installed it on the PI at /etc/systemd/system/ptyserver.service
It is very delicate in its current form:
[Unit]
Description=PtyServer Node.js Service
After=network.target
[Service]
WorkingDirectory=/home/project/remote-serial-pico/src/pi
ExecStart=/home/mieweb/.nvm/versions/node/v20.14.0/bin/node PtyServer.js
Restart=always
User=mieweb
Environment=NODE_ENV=production
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ptyserver
[Install]
WantedBy=multi-user.target
Note the user, the version of nodejs, the path... yuch.
We should have a way to package it up and install it on a new pi.
pm2 may be the best way. becuase we depend on node-pty which compiles against the node version!
See:
mieweb@raspberrypi:/etc $ Sep 15 23:31:01 raspberrypi systemd[1]: Started ptyserver.service - PtyServer Node.js Service.
Sep 15 23:31:01 raspberrypi ptyserver[5939]: innerError Error: Cannot find module '../build/Debug/pty.node'
Sep 15 23:31:01 raspberrypi ptyserver[5939]: Require stack:
Sep 15 23:31:01 raspberrypi ptyserver[5939]: - /home/project/remote-serial-pico/node_modules/node-pty/lib/unixTerminal.js
Sep 15 23:31:01 raspberrypi ptyserver[5939]: - /home/project/remote-serial-pico/node_modules/node-pty/lib/index.js
Sep 15 23:31:01 raspberrypi ptyserver[5939]: - /home/project/remote-serial-pico/src/pi/PtyServer.js
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._load (node:internal/modules/cjs/loader:981:27)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.require (node:internal/modules/cjs/loader:1231:19)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at require (node:internal/modules/helpers:177:18)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Object.<anonymous> (/home/project/remote-serial-pico/node_modules/node-pty/lib/unixTerminal.js:34:15)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._compile (node:internal/modules/cjs/loader:1364:14)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.load (node:internal/modules/cjs/loader:1203:32)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._load (node:internal/modules/cjs/loader:1019:12)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.require (node:internal/modules/cjs/loader:1231:19) {
Sep 15 23:31:01 raspberrypi ptyserver[5939]: code: 'MODULE_NOT_FOUND',
Sep 15 23:31:01 raspberrypi ptyserver[5939]: requireStack: [
Sep 15 23:31:01 raspberrypi ptyserver[5939]: '/home/project/remote-serial-pico/node_modules/node-pty/lib/unixTerminal.js',
Sep 15 23:31:01 raspberrypi ptyserver[5939]: '/home/project/remote-serial-pico/node_modules/node-pty/lib/index.js',
Sep 15 23:31:01 raspberrypi ptyserver[5939]: '/home/project/remote-serial-pico/src/pi/PtyServer.js'
Sep 15 23:31:01 raspberrypi ptyserver[5939]: ]
Sep 15 23:31:01 raspberrypi ptyserver[5939]: }
Sep 15 23:31:01 raspberrypi ptyserver[5939]: /home/project/remote-serial-pico/node_modules/node-pty/lib/unixTerminal.js:40
Sep 15 23:31:01 raspberrypi ptyserver[5939]: throw outerError;
Sep 15 23:31:01 raspberrypi ptyserver[5939]: ^
Sep 15 23:31:01 raspberrypi ptyserver[5939]: Error: The module '/home/project/remote-serial-pico/node_modules/node-pty/build/Release/pty.node'
Sep 15 23:31:01 raspberrypi ptyserver[5939]: was compiled against a different Node.js version using
Sep 15 23:31:01 raspberrypi ptyserver[5939]: NODE_MODULE_VERSION 115. This version of Node.js requires
Sep 15 23:31:01 raspberrypi ptyserver[5939]: NODE_MODULE_VERSION 108. Please try re-compiling or re-installing
Sep 15 23:31:01 raspberrypi ptyserver[5939]: the module (for instance, using `npm rebuild` or `npm install`).
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._extensions..node (node:internal/modules/cjs/loader:1460:18)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.load (node:internal/modules/cjs/loader:1203:32)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._load (node:internal/modules/cjs/loader:1019:12)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.require (node:internal/modules/cjs/loader:1231:19)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at require (node:internal/modules/helpers:177:18)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Object.<anonymous> (/home/project/remote-serial-pico/node_modules/node-pty/lib/unixTerminal.js:29:11)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._compile (node:internal/modules/cjs/loader:1364:14)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module.load (node:internal/modules/cjs/loader:1203:32)
Sep 15 23:31:01 raspberrypi ptyserver[5939]: at Module._load (node:internal/modules/cjs/loader:1019:12) {
Sep 15 23:31:01 raspberrypi ptyserver[5939]: code: 'ERR_DLOPEN_FAILED'
Sep 15 23:31:01 raspberrypi ptyserver[5939]: }
Sep 15 23:31:01 raspberrypi ptyserver[5939]: Node.js v18.20.3
Sep 15 23:31:01 raspberrypi systemd[1]: ptyserver.service: Main process exited, code=exited, status=1/FAILURE
Sep 15 23:31:01 raspberrypi systemd[1]: ptyserver.service: Failed with result 'exit-code'.
Sep 15 23:31:02 raspberrypi systemd[1]: ptyserver.service: Scheduled restart job, restart counter is at 1.
Sep 15 23:31:02 raspberrypi systemd[1]: Stopped ptyserver.service - PtyServer Node.js Service.
Look at https://nodered.org/docs/getting-started/raspberrypi