I usually access my application via Putty and do exactly what I show below to let my Node script running in the background:
screen
cd /var/www/node
node app.js
I'm using Ubuntu and I have already created an AMI of my instance and now I intend to use it in my Auto Scaling configuration, however, how should I do this Lauch Configuration in Shell Script so when I create a new instance it will automatically run my Node application?
The only thing I know is that I should start with #!/bin/bash
, but I don't know how to write the rest the correct way.
#!/bin/bash
screen
cd /var/www/node
node app.js
If I use it as I showed above, will it work normally or should I do it any other way? If so, can you give an example?
Update: Following Michael Hampton comment, I found this answer. So my code looks like this:
[Unit]
Description=To run Node
[Service]
ExecStart=/var/www/node/app.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/node
[Install]
WantedBy=multi-user.target
But when trying to run the service with systemctl start app
It asks for an authentication:
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'app.service'.
Authenticating as: Ubuntu (ubuntu)
Password:
However, this user does not have a password (not that I know of), so I just hit enter and he gives the following error:
==== AUTHENTICATION FAILED ===
Failed to start app.service: Access denied
So, I tried with sudo systemctl start app
, but it's like nothing happened. How can I run this service?
After running sudo systemctl daemon-reload
, sudo systemctl start app
and sudo systemctl status app
, is returned:
● app.service - App
Loaded: loaded (/etc/systemd/system/app.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Started App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[7645]: app.service: Failed at step GROUP spawning /var/www/node/app.js: No such process
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Main process exited, code=exited, status=216/GROUP
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Unit entered failed state.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Failed with result 'exit-code'.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Service hold-off time over, scheduling restart.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Start request repeated too quickly.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Failed to start App.
Update²:
I've changed the app.service
to (removed the Group
directive and added the node
to run):
[Unit]
Description=App
[Service]
ExecStart=/usr/bin/nodejs /var/www/node/app.js
Restart=always
User=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/node
[Install]
WantedBy=multi-user.target
But, when I access the status, is shown:
● app.service - App
Loaded: loaded (/etc/systemd/system/app.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]: at Function.Module.runMain (module.js:442:10)
Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]: at startup (node.js:136:18)
Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]: at node.js:966:3
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Main process exited, code=exited, status=1/FAILURE
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Unit entered failed state.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Failed with result 'exit-code'.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Service hold-off time over, scheduling restart.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Start request repeated too quickly.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: Failed to start App.