Closed pnutp0wer closed 7 years ago
How did you start it the first time? You need to setup some sort of bootup script that starts it, and probably something that restarts it if there is some error with it.
The easiest solution is by using pm2 which is a process manager, see https://github.com/jishi/node-sonos-http-api/issues/539 for an example. Or search "pm2" among closed issues.
Hi Jishi thank you for your reply. I already did the pm2 but I keep getting the error. Everytime I need to manually give the command "pm2 startup" as super user. Otherwise the API isn't working, very strange.. ???
This is what I did this morning and now is everything back on: pi@raspberrypi:~ $ pm2 list [PM2] Spawning PM2 daemon with pm2_home=/home/pi/.pm2 pi@raspberrypi:~ $ sudo su root@raspberrypi:/home/pi# pm2 startup [PM2] Init System found: systemd Platform systemd Template [Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target
[Service] Type=forking User=root LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/root/.pm2 PIDFile=/root/.pm2/pm2.pid
ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill
[Install] WantedBy=multi-user.target
Target path /etc/systemd/system/pm2-root.service Command list [ 'chmod +x /etc/systemd/system/pm2-root.service', 'systemctl enable pm2-root', 'systemctl start pm2-root', 'systemctl daemon-reload', 'systemctl status pm2-root' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service [PM2] Making script booting at startup...
Executing chmod +x /etc/systemd/system/pm2-root.service [DONE] Executing systemctl enable pm2-root [DONE] Executing systemctl start pm2-root [DONE] Executing systemctl daemon-reload [DONE] Executing systemctl status pm2-root ● pm2-root.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2017-09-09 11:13:53 CEST; 374ms ago Docs: https://pm2.keymetrics.io/ Main PID: 9027 (PM2 v2.6.1: God) CGroup: /system.slice/pm2-root.service ├─9027 PM2 v2.6.1: God Daemon (/root/.pm2) └─9038 node /home/pi/node-sonos-http-api/server.js
sep 09 11:13:52 raspberrypi pm2[8984]: [PM2] Resurrecting
sep 09 11:13:52 raspberrypi pm2[8984]: [PM2] Restoring processes located in /root/.pm2/dump.pm2
sep 09 11:13:52 raspberrypi pm2[8984]: [PM2] Process /home/pi/node-sonos-http-api/server.js restored
sep 09 11:13:53 raspberrypi pm2[8984]: ┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
sep 09 11:13:53 raspberrypi pm2[8984]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
sep 09 11:13:53 raspberrypi pm2[8984]: ├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
sep 09 11:13:53 raspberrypi pm2[8984]: │ http-api │ 0 │ fork │ 9038 │ online │ 0 │ 0s │ 433% │ 17.1 MB │ root │ disabled │
sep 09 11:13:53 raspberrypi pm2[8984]: └──────────┴────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘
sep 09 11:13:53 raspberrypi pm2[8984]: Use pm2 show <id|name>
to get more details about an app
sep 09 11:13:53 raspberrypi systemd[1]: Started PM2 process manager.
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
root@raspberrypi:/home/pi# pm2 list
┌──────────┬──────┬────────┬───┬─────┬───────────┐
│ Name │ mode │ status │ ↺ │ cpu │ memory │
├──────────┼──────┼────────┼───┼─────┼───────────┤
│ http-api │ fork │ online │ 0 │ 0% │ 55.2 MB │
└──────────┴──────┴────────┴───┴─────┴───────────┘
Use pm2 show <id|name>
to get more details about an app
I think I know what the problem is. I've a wallplug behind my stereo set, which kills the energy of sonos AMP when I don't use it for X hours. So the Sonos AMP is all turned off and not connected anymore to my network. After I turn on the wallplug Sonos AMP will connect again to the network through a network cable. But I think the API doesn't recognize the Sonos anymore.
The reason I kill of my sonos AMP is it's uses much power in standby mode. Anyone had similar issues or knows a solution for this?
It's unclear to me what the actual issue is? Is the pm2 status
output empty after your Sonos AMP has been off for a while, or does it show some sort of info? Is rhe raspberry pi connected to the same wallplug?
One issue in the output you've pasted was that you started pm2 with the pi
user, and then installed it as root, and persisted it as root. This can give you weird side-effects. Either install it to run as the pi-user, or as root user, and also add start the api with the same user, and also run pm2 save
with said user.
But if you could explain more in detail, what you actually observe (and what you expect to happen), I could be of more assistance.
Hi Jishi, I just found out for sure what the problem is. Like I mentioned in my previous post the API/pm2 will turn off after my AMP is completely killed off. I mean that if I turn off the wallplug switch (so no electricity input anymore to the AMP) the pi wont recognize the AMP when the switch is turned on again. The pi isn't connected to the same wallplug switch, so the pi is always turned on but the AMP isn't.
If I start again pm2 and keep my switch on, I don't have any problem. But due power usage I don't want the sonos "always on". Do you've any solution to auto-recognize the sonos amp after I turned it off?
Where is your raspberry pi connected? I mean, the network cable? The API is supposed to survive loosing all of the players, it would fall back to start searching for them, indefinitely. I can't seem to reproduce the behavior you are seeing, so I need detailed information how everything is connected to figure out why it's behaving the way it does.
When the AMP is powered on, does a reboot of your pi bring the API back up again? You should probably make sure that your auto startup actually works as you'd expect it to work.
Hi Jishi, the amp aswell as the pi are connected by network cable through the same network switch to the router. The only difference is that the amp will turn off by the wallplug switch and the pi is always turned on.
When the AMP is powered on, does a reboot of your pi bring the API back up again? You should probably make sure that your auto startup actually works as you'd expect it to work.
This is what the PM2 is for right? I checked it out this morning see down under.
+++++
I just checked the status of pm2 this morning after the wallplug is turned on again: pi@raspberrypi:~ $ pm2 list
[PM2] Spawning PM2 daemon with pm2_home=/home/pi/.pm2 pi@raspberrypi:~ $ sudo su root@raspberrypi:/home/pi# pm2 list [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2 [PM2] PM2 Successfully daemonized ┌──────┬──────┬────────┬───┬─────┬────────┐ │ Name │ mode │ status │ ↺ │ cpu │ memory │ └──────┴──────┴────────┴───┴─────┴────────┘ Use
pm2 show <id|name>
to get more details about an app root@raspberrypi:/home/pi# sudo reboot Connection to 192.168.188.35 closed by remote host. Connection to 192.168.188.35 closed.
After I rebooted the pi it seems to be online again, check this out:
pi@raspberrypi:~ $ sudo su root@raspberrypi:/home/pi# pm2 status
┌──────────┬──────┬────────┬───┬─────┬───────────┐
│ Name │ mode │ status │ ↺ │ cpu │ memory │ ├──────────┼──────┼────────┼───┼─────┼───────────┤ │ http-api │ fork │ online │ 0 │ 0% │ 55.1 MB │ └──────────┴──────┴────────┴───┴─────┴───────────┘ Use
pm2 show <id|name>
to get more details about an app root@raspberrypi:/home/pi#
Okay, so it seems that PM2 is resurrecting the process correctly. It's not super-straightforward how to actually wire up PM2, hence the question.
It sounds to me that pm2 is actually crashing/stopping when the process can't communicate with the AMP. My code actually deals with that (I tested it), but it do spam a lot of noise to the logs. It might be exhausted, and I can probably make it more silent. I'll see what I can do.
The strange thing is that I'm almost sure it worked 2 weeks straight forward. I know for sure that the AMP is turned on/off with the wallplug and still worked. Do you know how to remove cleanly both the API and the pm2? I want to reinstall it as Superuser and see if anything will change.
One explanation might be the excessive logging issue, where pm2 crashes because it can't log anything more (filled up disk space, basically). It might make sense to disable logging completely, to save your SD-card, see https://github.com/Unitech/pm2/issues/2997
Removing the api is just deleting the folder you created for it (where you unzipped or git cloned). Removing pm2 should be sufficient to just run:
pm2 unstartup
npm uninstall -g pm2
and then remove the local folders,
rm -rf /root/.pm2
rm -rf /pi/.pm2
(since you have started it both as pi user and root user, it has created one folder in each $HOME directory)
Actually, that is basically the issue, I see this in your first post:
npm ERR! nospc ENOSPC: no space left on device, write
Should have read more carefully! 😄
You were right! I checked the data usage by "NCDU" and the dump folder of the pm2 logs was 1.8GB, now I deleted it and everything is working fine for now. Tonight I will turn off the wallplug and check if the API is still working tomorrow. Keep you updated!
Crashed again this morning, unless I reboot the pi the ha-bridge isn't working. If i check the logs i get this:
pi@raspberrypi:~ $ tail -f /var/log/syslog Oct 22 08:17:01 raspberrypi CRON[24758]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 09:17:01 raspberrypi CRON[24781]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 10:17:01 raspberrypi CRON[24799]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 11:17:01 raspberrypi CRON[24823]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 12:17:01 raspberrypi CRON[24847]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 12:35:52 raspberrypi systemd[1]: Starting Cleanup of Temporary Directories... Oct 22 12:35:52 raspberrypi systemd[1]: Started Cleanup of Temporary Directories. Oct 22 13:17:01 raspberrypi CRON[24871]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 13:26:03 raspberrypi systemd[1]: Started Session c4 of user pi. Oct 22 13:27:42 raspberrypi systemd[1]: Started Session c5 of user pi.
Are you familiar with this @jishi?
That log doesn't contain anything of interest. I don't think pm2 logs anything to syslog, it would be in /root/.pm2/logs if you run pm2 as root
On Oct 22, 2017 13:32, "pnutp0wer" notifications@github.com wrote:
Crashed again this morning, unless I reboot the pi the ha-bridge isn't working. If i check the logs i get this:
pi@raspberrypi:~ $ tail -f /var/log/syslog Oct 22 08:17:01 raspberrypi CRON[24758]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 09:17:01 raspberrypi CRON[24781]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 10:17:01 raspberrypi CRON[24799]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 11:17:01 raspberrypi CRON[24823]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 12:17:01 raspberrypi CRON[24847]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 12:35:52 raspberrypi systemd[1]: Starting Cleanup of Temporary Directories... Oct 22 12:35:52 raspberrypi systemd[1]: Started Cleanup of Temporary Directories. Oct 22 13:17:01 raspberrypi CRON[24871]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Oct 22 13:26:03 raspberrypi systemd[1]: Started Session c4 of user pi. Oct 22 13:27:42 raspberrypi systemd[1]: Started Session c5 of user pi.
— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jishi/node-sonos-http-api/issues/550#issuecomment-338470501, or mute the thread https://github.com/notifications/unsubscribe-auth/ADBNI7gD6fbBdJisHa2aV8QorUE2kJq3ks5suyfmgaJpZM4PJU7A .
I think it's the memory again, if I check upon this..
pi@raspberrypi:~ $ pm2 startup [PM2] Init System found: systemd [PM2] You have to run this command as root. Execute the following command: sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi pi@raspberrypi:~ $ sudo su root@raspberrypi:/home/pi# pm2 startup [PM2] Init System found: systemd Platform systemd Template [Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target
[Service] Type=forking User=root LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/root/.pm2 PIDFile=/root/.pm2/pm2.pid
ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill
[Install] WantedBy=multi-user.target
Target path /etc/systemd/system/pm2-root.service Command list [ 'chmod +x /etc/systemd/system/pm2-root.service', 'systemctl enable pm2-root', 'systemctl start pm2-root', 'systemctl daemon-reload', 'systemctl status pm2-root' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service [PM2] Making script booting at startup...
Executing chmod +x /etc/systemd/system/pm2-root.service [DONE] Executing systemctl enable pm2-root [DONE] Executing systemctl start pm2-root Job for pm2-root.service failed because of unavailable resources or another system error. See "systemctl status pm2-root.service" and "journalctl -xe" for details. [ERROR] Exit code : 1 [PM2][ERROR] systemctl start pm2-root failed, see error above.
pi@raspberrypi:~ $ sudo su root@raspberrypi:/home/pi# systemctl status pm2-root.service ● pm2-root.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled) Active: failed (Result: resources) since Thu 2016-11-03 18:17:21 CET; 1min 7s ago Docs: https://pm2.keymetrics.io/ Process: 419 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
nov 03 18:17:10 raspberrypi systemd[1]: Starting PM2 process manager... nov 03 18:17:19 raspberrypi pm2[419]: [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2 nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Failed to read PID from file /root/.pm2/pm2.pid: Invalid argumen nov 03 18:17:21 raspberrypi systemd[1]: Failed to start PM2 process manager. nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Unit entered failed state. nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Failed with result 'resources'. ...skipping... ● pm2-root.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled) Active: failed (Result: resources) since Thu 2016-11-03 18:17:21 CET; 1min 7s ago Docs: https://pm2.keymetrics.io/ Process: 419 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
nov 03 18:17:10 raspberrypi systemd[1]: Starting PM2 process manager... nov 03 18:17:19 raspberrypi pm2[419]: [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2 nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Failed to read PID from file /root/.pm2/pm2.pid: Invalid argumen nov 03 18:17:21 raspberrypi systemd[1]: Failed to start PM2 process manager. nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Unit entered failed state. nov 03 18:17:21 raspberrypi systemd[1]: pm2-root.service: Failed with result 'resources'.
I also can't access the Pi anymore on it's own screen? Normally I do everything by using terminal (mac) but if I attach my keyboard and mouse to the pi and reboot it, I only get the rainbow screen and then nothing anymore.....
Hi all,
I am using since 2 weeks the api through my RPi. But since yesterday the api isn't working properly. If I check on the http://IP-ADRESS:5005/living/play I get a blank page.
Then if I login the pi through ssh and give the command "npm start" i get the following: pi@raspberrypi:~ $ npm start npm ERR! Error: ENOENT: no such file or directory, open '/home/pi/package.json' npm ERR! at Error (native) npm ERR! If you need help, you may report this entire log, npm ERR! including the npm and node versions, at: npm ERR! http://github.com/npm/npm/issues
npm ERR! System Linux 4.9.43-v7+ npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "start" npm ERR! cwd /home/pi npm ERR! node -v v4.8.2 npm ERR! npm -v 1.4.21 npm ERR! path /home/pi/package.json npm ERR! syscall open npm ERR! code ENOENT npm ERR! errno -2 npm ERR! nospc ENOSPC: no space left on device, write npm ERR! nospc This is most likely not a problem with npm itself npm ERR! nospc and is related to insufficient space on your system.
npm ERR! System Linux 4.9.43-v7+ npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "start" npm ERR! cwd /home/pi npm ERR! node -v v4.8.2 npm ERR! npm -v 1.4.21 npm ERR! syscall write npm ERR! code ENOSPC npm ERR! errno -28 npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /home/pi/npm-debug.log npm ERR! not ok code 0
I saw this issue #112. When I first do: cd node-sonos-http-api and then do npm start I get the following:
pi@raspberrypi:~ $ cd node-sonos-http-api pi@raspberrypi:~/node-sonos-http-api $ npm start
2017-08-31T19:51:57.387Z INFO Could not find file /home/pi/node-sonos-http-api/settings.json 2017-08-31T19:51:59.433Z INFO Presets loaded: { example: { players: [ { roomName: 'Bathroom', volume: 10 }, { roomName: 'Kitchen', volume: 10 }, { roomName: 'Office', volume: 10 }, { roomName: 'Bedroom', volume: 10 }, { roomName: 'TV Room', volume: 15 } ], playMode: { shuffle: true, repeat: 'all', crossfade: false }, pauseOthers: false, favorite: 'My example favorite' } } 2017-08-31T19:52:00.606Z INFO http server listening on port 5005
Now the API is working, but if I close the terminal the API isn't working anymore after several minutes th and I get a blank page?!
I checked the other issues but couldn't find anything similar..