How do I run a Node.js app as a systemd service? - node.js

So I have a node.js app that I am trying to deploy and run as a systemd service.
Here's the .service file:
[Unit]
Description=My app
[Service]
ExecStart=/usr/local/bin/node /var/www/html/schema.js
Restart=always
User=root
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/html
[Install]
WantedBy=multi-user.target
Unfortunately, it just does not work.... I keep getting an error:
Feb 12 09:56:49 myswerth systemd[1]: Started my-app app.
-- Subject: Unit my-app.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit my-app.service has finished starting up.
--
-- The start-up result is done.
Feb 12 09:56:49 my-server systemd[23765]: Failed at step GROUP spawning /usr/local/bin/node: No such process
-- Subject: Process /usr/local/bin/node could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /usr/local/bin/node could not be executed and failed.
--
-- The error number returned by this process is 3.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service: main process exited, code=exited, status=216/GROUP
Feb 12 09:56:49 myswerth systemd[1]: Unit my-app.service entered failed state.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service failed.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service holdoff time over, scheduling restart.
Feb 12 09:56:49 myswerth systemd[1]: Stopped My app app.
-- Subject: Unit my-app.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit my-app.service has finished shutting down.
I'm not really sure why it is kicking up this error. I have tried to access the file manually and can confirm it is located in /usr/local/bin/node and furthermore that I can open node by running it (which I can?) so I have no clue why it cant execute? I have also checked the execute permissions. All user groups can execute so ya.... I'm at a loss here...

Make sure node is in the path by running which node
Try the following:
in your schema.js file add the following line at the top
#!/usr/bin/env node
this will remove the need to specify the node in the ExecStart
Also make sure to
chmod +x /var/www/html/schema.js
Additional options worth considering adding to the Service definition:
PIDFile=/tmp/your-app-name.pid
KillSignal=SIGQUIT
WorkingDirectory=/var/www/html/
Try to fix to use:
Group=root

Related

Error installing docker on Arch Linux: error initializing graphdriver: loopback attach failed

So I stupidly got a laptop with the latest and greatest hardware, so I had to install ArchLinux (version 5.19.13-arch1-1) instead of debian. I'm only vaguely familiar with linux, my fiance has been helping but this has him stumped too so here we are.
I followed the wiki instructions for installing docker, but consistently get the following error when attempting to start the service:
sudo journalctl --since "5 minutes ago" -xeu docker.service
Oct 26 14:37:34 werk systemd[1]: docker.service: Start request repeated too quickly.
Oct 26 14:37:34 werk systemd[1]: docker.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ The unit docker.service has entered the 'failed' state with result 'exit-code'.
Oct 26 14:37:34 werk systemd[1]: Failed to start Docker Application Container Engine.
░░ Subject: A start job for unit docker.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit docker.service has finished with a failure.
░░
░░ The job identifier is 10988 and the job result is failed.
lines 334-369/369 (END)
sudo journalctl --since "5 minutes ago" -ru docker.service
Oct 26 14:37:34 werk systemd[1]: Failed to start Docker Application Container Engine.
Oct 26 14:37:34 werk systemd[1]: docker.service: Failed with result 'exit-code'.
Oct 26 14:37:34 werk systemd[1]: docker.service: Start request repeated too quickly.
Oct 26 14:37:34 werk systemd[1]: Stopped Docker Application Container Engine.
Oct 26 14:37:34 werk systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
Oct 26 14:37:34 werk systemd[1]: Failed to start Docker Application Container Engine.
Oct 26 14:37:34 werk systemd[1]: docker.service: Failed with result 'exit-code'.
Oct 26 14:37:34 werk systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE
Oct 26 14:37:34 werk dockerd[71427]: failed to start daemon: error initializing graphdriver: loopback attach failed
I don't really understand loopback devices, but the result of losetup -f seemed sus:
losetup -f
losetup: cannot find an unused loop device: Permission denied
[me#werk ~]$ sudo losetup -f
losetup: cannot find an unused loop device: No such device
We also theorized that the docker user didn't have sufficient permissions to do what it needed to do, but could not find any user specified in the docker.service file (located at /etc/systemd/system/multi-user.target.wants/docker.service). We tried to run the ExecStart command line specified in the docker.service file as root, but got the following error:
[me#werk ~]$ sudo /usr/bin/dockerd -H fd://
[sudo] password for me:
INFO[2022-10-26T14:56:00.483059782-06:00] Starting up
failed to load listeners: no sockets found via socket activation: make sure the service was started by systemd
So that was a bit of a bust.
To be extra clear, installation was as follows:
pacman -Syu docker
systemctl enable docker.service
systemctl start docker.service
And in case it matters, I'm on a Framework laptop with 12th gen Intel core processors.

Not able to start redhat httpd service

When I try to start the httpd service it is failing with the error :
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
output of journalctl -xe
The result is failed.
Dec 08 04:09:49 uls-**** systemd[1]: Unit httpd.service entered failed state.
Dec 08 04:09:49 uls-******** systemd[1]: httpd.service failed.
Dec 08 04:09:49 uls-******** sudo[67525]: pam_unix(sudo:session): session closed for user root
Dec 08 04:09:49 uls-******** polkitd[854]: Unregistered Authentication Agent for unix-process:67526:3062933569 (system bus name :1.159161, object path /org/
Dec 08 04:10:01 uls-******** systemd[1]: Started Session 78106 of user root.
-- Subject: Unit session-78106.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-78106.scope has finished starting up.
--
-- The start-up result is done.
Dec 08 04:10:01 uls-******** systemd[1]: Started Session 78107 of user root.
-- Subject: Unit session-78107.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-78107.scope has finished starting up.
--
-- The start-up result is done.
Dec 08 04:10:01 uls-******** CROND[67561]: (root) CMD (/usr/share/spamassassin/sa-update.cron 2>&1 | tee -a /var/log/sa-update.log)
Dec 08 04:10:01 uls-******** CROND[67562]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 08 04:20:01 uls-******** systemd[1]: Started Session 78109 of user root.
-- Subject: Unit session-78109.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-78109.scope has finished starting up.
--
-- The start-up result is done.
Dec 08 04:20:01 uls-******** systemd[1]: Started Session 78108 of user root.
-- Subject: Unit session-78108.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-78108.scope has finished starting up.
--
-- The start-up result is done
.
output of systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2021-12-08 04:26:51 PST; 53s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 68719 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Main PID: 68719 (code=exited, status=1/FAILURE)
I tried to change the port number in httpd.conf file. But I got the same error. Can anyone please help?
try check your config, validate from command apachectl configtest

Unable to run node app using systemd

This going to be my first node deployment. I have this app which runs fine on my Debian 9 server using this command:
cd /srv/myapp && NODE_ENV=production yarn start
And print out this message:
yarn run v1.6.0
$ babel-node index.js
In order to demonize the app using systemd, I created `/lib/systemd/system/myapp.service:
[Unit]
Description=Myapp
[Service]
ExecStart=/home/john/start.sh
Type=simple
User=john
Restart=on-failure
[Install]
WantedBy=multi-user.target
and in start.sh I have:
cd /srv/myapp && NODE_ENV=production yarn start
Hoewever, when I run systemctl start myapp the node app does not start to listen on port 3000, as expected (netstat -tulpn | grep :3000
returns no results)
# systemctl status myapp
● myapp.service - Myapp
Loaded: loaded (/lib/systemd/system/myapp.service; disabled; vendor preset: enabled
Active: failed (Result: exit-code) since Sun 2018-05-13 06:14:04 EDT; 5s ago
Process: 8852 ExecStart=/home/bob/start.sh (code=exited, status=203/EXEC)
Main PID: 8852 (code=exited, status=203/EXEC)
May 13 06:14:04 9606 systemd[1]: myapp.service: Unit entered failed state.
May 13 06:14:04 9606 systemd[1]: myapp.service: Failed with result 'exit-code'.
May 13 06:14:04 9606 systemd[1]: myapp.service: Service hold-off time over, scheduling
May 13 06:14:04 9606 systemd[1]: Stopped myapp.
May 13 06:14:04 9606 systemd[1]: myapp.service: Start request repeated too quickly.
May 13 06:14:04 9606 systemd[1]: Failed to start myapp.
May 13 06:14:04 9606 systemd[1]: myapp.service: Unit entered failed state.
May 13 06:14:04 9606 systemd[1]: myapp.service: Failed with result 'exit-code'.
I have other variations on myapp.service but could not manage to run node.
What could be wrong here? How can I fix it?
Process: 8852 ExecStart=/home/bob/start.sh (code=exited, status=203/EXEC)
According to systemd.exec(5), this means systemd could not execute the specified file:
203 EXIT_EXEC The actual process execution failed (specifically, the execve(2) system call). Most likely this is caused by a missing or non-accessible executable file.
You should check if /home/bob/start.sh is executable and has correct shebang specified (that is, the first line of your script must be #!/bin/bash).

Systemd Zookeeper service failed

I've created the following systemd service script for running Zookeeper using Ansible:
[Unit]
Description=ZooKeeper
[Service]
User=root
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/tmp/zookeeper-3.4.9/bin/zkServer.sh start
ExecStop=/tmp/zookeeper-3.4.9/bin/zkServer.sh stop
TimeoutSec=300
[Install]
WantedBy=multi-user.target
But, while trying to run the script using sudo service zookeeper start I get the following error:
Nov 15 22:00:35 sharedservicesprovider systemd[1]: Starting ZooKeeper...
-- Subject: Unit zookeeper.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit zookeeper.service has begun starting up.
Nov 15 22:00:35 sharedservicesprovider systemd[15287]: zookeeper.service: Failed at step USER spawning /tmp/zookeeper-3.4.9/bin/zkServer.sh: No such process
-- Subject: Process /tmp/zookeeper-3.4.9/bin/zkServer.sh could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /tmp/zookeeper-3.4.9/bin/zkServer.sh could not be executed and failed.
--
-- The error number returned by this process is 3.
Nov 15 22:00:35 sharedservicesprovider systemd[1]: zookeeper.service: Control process exited, code=exited status=217
Nov 15 22:00:35 sharedservicesprovider systemd[1]: Failed to start ZooKeeper.
-- Subject: Unit zookeeper.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Any hint onto why I am unable to start Zookeeper properly since I'm quite new to system services?
Maybe so late... but use as ExecStart:
/tmp/zookeeper-3.4.9/bin/zkServer.sh start-foreground
[Unit]
Description=Zookeeper
After=syslog.target
[Service]
Environment=ZOO_LOG_DIR=/zookeeper/logs
SyslogIdentifier=zookeeper
TimoutStartSec=10min
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/zookeeper/bin/zkServer.sh start
ExecStop=/zookeeper/bin/zkServer.sh stop
[Install]
WantedBy=multi-user.target
ZOO_LOG_DIR is for permission require
this sounds like basics to check to me
first, make sure the file exists
ls -ltrah /tmp/zookeeper-3.4.9/bin/zkServer.sh
if it doesn't find where you extracted the zookeeper startup script. On my instance, it's /opt/kafka/bin/zookeeper-server-start.sh
Then verify that the user you have specified (you've specified two users, root and zookeeper. I'm assuming the last value is what's valid/desired) can access the script and has executable rights to it.
This would be done with chown and chmod commands, respectively.

How to port an RPM package consist of SysV init script to systemd?

I have created an RPM package for my daemon which on installation create service init script under /etc/init.d/
Now I wanted to port this RPM package for CentOS7.1 and use systemd framework for service startup through boot as well as by admin through start/stop command.
I couldn't find any tutorial. Please help.
EDIT:
I tried the suggestion given by msuchy.
I tried and here is my observation
On sysV based framework (CentOS6.5)
[root#adil work]# /etc/init.d/daemon_script status
Service daemon: Stopped
[root#adil work]# /etc/init.d/daemon_script start
Starting daemon: Initializing daemon... [ OK ]
[root#adil work]#
[root#adil work]# /etc/init.d/daemon_script status
Service daemon: Running
[root#adil work]#
[root#adil work]# /etc/init.d/daemon_script stop
Shutting down parent daemon: [ OK ]
[root#adil work]# /etc/init.d/daemon_script status
Service daemon: Stopped
[root#adil work]#
===========
On systemd based framework, I install the same RPM on CentOS7.1
[root#localhost x86_64]# /etc/init.d/daemon_script
Usage: /etc/init.d/daemon_script {start|stop|restart|status}
[root#localhost x86_64]# /etc/init.d/daemon_script start
Starting daemon_script (via systemctl): Warning: Unit file of daemon_script.service changed on disk, 'systemctl daemon-reload' recommended.
Job for daemon_script.service failed. See 'systemctl status daemon_script.service' and 'journalctl -xn' for details.
[FAILED]
[root#localhost x86_64]# systemctl daemon-reload
[root#localhost x86_64]# systemctl status daemon_script.service
daemon_script.service - SYSV: start and stop Test daemon service.
Loaded: loaded (/etc/rc.d/init.d/daemon_script)
Active: failed (Result: exit-code) since Fri 2015-09-11 15:30:44 IST; 32s ago
Sep 11 15:30:44 localhost.localdomain systemd[1]: Starting SYSV: start and st...
Sep 11 15:30:44 localhost.localdomain systemd[1]: daemon_script.service: cont...
Sep 11 15:30:44 localhost.localdomain systemd[1]: Failed to start SYSV: start...
Sep 11 15:30:44 localhost.localdomain systemd[1]: Unit daemon_script.service ...
Hint: Some lines were ellipsized, use -l to show in full.
[root#localhost x86_64]# systemctl status daemon_script.service -l
daemon_script.service - SYSV: start and stop Test daemon service.
Loaded: loaded (/etc/rc.d/init.d/daemon_script)
Active: failed (Result: exit-code) since Fri 2015-09-11 15:30:44 IST; 46s ago
Sep 11 15:30:44 localhost.localdomain systemd[1]: Starting SYSV: start and stop Test daemon service....
Sep 11 15:30:44 localhost.localdomain systemd[1]: daemon_script.service: control process exited, code=exited status=203
Sep 11 15:30:44 localhost.localdomain systemd[1]: Failed to start SYSV: start and stop Test daemon service..
Sep 11 15:30:44 localhost.localdomain systemd[1]: Unit daemon_script.service entered failed state.
[root#localhost x86_64]#
output of journalctl -xn
-- Logs begin at Fri 2015-09-11 14:50:35 IST, end at Fri 2015-09-11 15:40:01 IST. --
Sep 11 15:31:03 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/dm-event.socket:10] Unknown lvalue 'RemoveOnStop' in section 'Socket'
Sep 11 15:39:33 localhost.localdomain systemd[1]: Starting SYSV: start and stop Test daemon service....
-- Subject: Unit daemon_script.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit daemon_script.service has begun starting up.
Sep 11 15:39:33 localhost.localdomain systemd[8509]: Failed at step EXEC spawning /etc/rc.d/init.d/daemon_script: Exec format error
-- Subject: Process /etc/rc.d/init.d/daemon_script could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /etc/rc.d/init.d/daemon_script could not be executed and failed.
--
-- The error number returned while executing this process is 8.
Sep 11 15:39:33 localhost.localdomain systemd[1]: daemon_script.service: control process exited, code=exited status=203
Sep 11 15:39:33 localhost.localdomain systemd[1]: Failed to start SYSV: start and stop Test daemon service..
-- Subject: Unit daemon_script.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit daemon_script.service has failed.
--
-- The result is failed.
Sep 11 15:39:33 localhost.localdomain systemd[1]: Unit daemon_script.service entered failed state.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Created slice user-0.slice.
-- Subject: Unit user-0.slice has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit user-0.slice has finished starting up.
--
-- The start-up result is done.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Starting Session 7 of user root.
-- Subject: Unit session-7.scope has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-7.scope has begun starting up.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Started Session 7 of user root.
-- Subject: Unit session-7.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-7.scope has finished starting up.
--
-- The start-up result is done.
Sep 11 15:40:01 localhost.localdomain CROND[8528]: (root) CMD (/usr/lib64/sa/sa1 1 1)
[root#localhost x86_64]#
You do not need to migrate your scripts.
It is probably better and you can utilize some interesting functions of Systemd, your files will be smaller. But you do not need to migrate. Systemd works correctly with SysV init files too.
You can not locate any SysV init files on CentOS 7 installation because Red Hat packagers (who created CentOS) put an effort into packaging and migrated all SysV files to unit files. But you do not need to.
The is only one task you need to do. Once you place new SysV file, you must reload the systemd manager configuration using
# systemctl daemon-reload
That is all.
I give you small example
# cat /etc/init.d/foo
#!/usr/bin/sh
echo ahoy
# chmod a+x /etc/init.d/foo
# systemctl start foo
Failed to start foo.service: Unit foo.service failed to load: No such file or directory.
# systemctl daemon-reload
# systemctl start foo
# journalctl -xn
-- Subject: Unit foo.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit foo.service has finished starting up.
--
-- The start-up result is done.
# service foo start
ahoy
So you can use all command (chkconfig, service) as you are used to from CentOS 6. And when you have time, you can study man systemd.unit(5) and bunch of other man pages (see "SEE ALSO" of that man page).

Resources