Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autostart Sonos Api in a Raspberry Pi #20

Closed
wadoline opened this issue Aug 7, 2014 · 15 comments
Closed

Autostart Sonos Api in a Raspberry Pi #20

wadoline opened this issue Aug 7, 2014 · 15 comments

Comments

@wadoline
Copy link

wadoline commented Aug 7, 2014

Hi

First of all I thank you for your effort.

I'm trying to autostart this API on a Raspberry Pi, but have not been successful. I can start it manually
Could you tell me how?

Best regards

@jishi
Copy link
Owner

jishi commented Aug 8, 2014

I'm using pm2 to handle all of this. https://github.com/Unitech/pm2

Basically, install pm2:

$ npm install -g pm2
// this will install it as a init.d script
$ pm2 startup
$ cd /your/http-api/folder/
// start the http api through pm2 instead
$ pm2 start server.js -x --name "http-api"
// save active processes so it will restart them upon restart (if the pi crashes, it would save it on shutdown automatically)
$ pm2 dump

Untested steps, I might have slipped on some commands, but you probably get the drift.

@jishi jishi closed this as completed Aug 8, 2014
@wadoline
Copy link
Author

wadoline commented Aug 8, 2014

It works like a charm!

This is my code
$ sudo npm install -g pm2
$ pm2 startup raspberry
$ sudo env PATH=$PATH:/usr/local/bin pm2 startup raspberry -u pi
$ cd /your/http-api/folder/
$ pm2 start server.js -x --name "http-api"
$ pm2 dump

Thankyou very much.

Good Job

@ghost
Copy link

ghost commented Jan 17, 2017

Hi there, I've been following these steps, and they seem to work fine without any fatal errors or anything, but after restarting the Pi, the service on port 5005 isn't running.

Is there anywhere I can go to check for errors?

Your suggestions are most welcome.

@jishi
Copy link
Owner

jishi commented Jan 17, 2017

I've had some experiences that the pm2.dump file is cleared when rebooting for some odd reason, and I'm not alone: Unitech/pm2#1035

However, this is not consistent, and my workaround was to run pm2 dump and then make the dump file immutable. You can find instructions on how to do that in that thread, but basically:

sudo chattr +i /your/path/to/dump.pm2

@jishi
Copy link
Owner

jishi commented Jan 17, 2017

I thought that they had fixed it, but apparently it is still an open issue.

@dghenderson
Copy link

Even making the pm2.dump immutable didn't help on reboot for me. Any additional suggestions?

@GregRocket
Copy link

@dghenderson The way I have been using PM2 with raspberry pi 3 works for me. After a reboot the sonos api autostarts.

cd /your/http-api/folder/
pm2 start npm -- start
pm2 save

I'm not an expert but this is how I managed to get it to work.

@jishi
Copy link
Owner

jishi commented Jan 25, 2017

@dghenderson Did you install the startup scripts using pm2 startup?

If you see the PM2 process in your process list after bootup, the startup works, but the resurrection does not. If you don't see the PM2 process at all, startup hasn't been installed correctly.

It might identify the incorrect startup system, for a clean debian/raspbian jessie, it should be systemd, you can specify it manually when calling pm2 startup systemd. For an upgraded raspbian/debian, it might still use systemv (or more commonly known as SysV ).

@ghost
Copy link

ghost commented Jan 25, 2017

Just to let you guys know, I was a bit impatient saying it won't load on boot, because it took a few seconds, but I noticed the script was running after a reboot of the Pi by itself, and I was able to access the index page on port 5005.

@dghenderson
Copy link

Ah @jishi I think I did NOT do the startup scripts correctly, and needed to specify systemd. Working on reboot now, thanks! Haven't needed to keep dump.pm2 immutable either (so far).

@ghost
Copy link

ghost commented Mar 22, 2018

This is no longer working anymore, I did what @GregRocket described, rebooted the Pi and the service doesn't run anymore.

Is there a solid way of autostart?

@joohann
Copy link

joohann commented Mar 22, 2018

https://www.axllent.org/docs/view/nodejs-service-with-systemd/

This works for me on the RP.

@ghost
Copy link

ghost commented Mar 22, 2018

I'll try this, although looking at, I have no idea what to do. Can you output your terminal steps please? I have done what was listed above in this issue, but when restarting the Pi, the service is not running.

@joohann
Copy link

joohann commented Mar 23, 2018

sudo vim (or nano) /etc/systemd/system/sonos.service

File

Description=server.js Sonos Server
#Requires=After=mysql.service       # Requires the mysql service to run first

[Service]
#I'm running this from the local user I renamed pi to johann
ExecStart=/usr/local/bin/node /home/johann/node-sonos-http-api/server.js
# Required on some systems
#WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sonsos-sever
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=5005

[Install]
WantedBy=multi-user.target

On time thingy
ls -s /etc/systemd/system/multi-user.target.wants/sonos.service /etc/systemd/system/sonos.service

Enable the service
systemctl enable nodeserver.service

Start the service
systemctl start sonos.service

Stop the service
systemctl stop sonos.service

Status the service
systemctl status sonos.service

Example respons

● sonos.service - server.js Sonos Server
   Loaded: loaded (/etc/systemd/system/sonos.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-03-23 15:34:52 CET; 1min 27s ago
 Main PID: 326 (node)
   CGroup: /system.slice/sonos.service
           └─326 /usr/local/bin/node /home/johann/node-sonos-http-api/server.js

Mar 23 15:35:22 raspberrypi sonsos-sever[326]: 2018-03-23T14:35:22.313Z INFO Presets loaded: { example:
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:    { players:
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:       [ { roomName: 'Bathroom', volume: 10 },
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:         { roomName: 'Kitchen', volume: 10 },
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:         { roomName: 'Office', volume: 10 },
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:         { roomName: 'Bedroom', volume: 10 },
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:         { roomName: 'TV Room', volume: 15 } ],
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:      playMode: { shuffle: true, repeat: 'all', crossfade: false },
Mar 23 15:35:22 raspberrypi sonsos-sever[326]:      pauseOthers: false } }
Mar 23 15:35:43 raspberrypi sonsos-sever[326]: 2018-03-23T14:35:43.128Z INFO http server listening on 0.0.0.0 port 5005

@ghost
Copy link

ghost commented Mar 24, 2018

@jishi Sorry, I didn't see your comment about instaling the startup scripts. I did this, which I don't remember doing before, have just rebooted, and I believe the Sonos API is running on boot now! Will do a couple more tests, but I believe I'm now sorted, thanks.

FYI I think this should be added onto the main ReadMe file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants