How can I start Docusaurus as a service on Linux (Ubuntu) - node.js

Hi guys I try to start Docusaurus V2 as a service on ubuntu headless. Currently I have a script in my Docusaurus folder as a temporarly solution. It works when I start it with ./start.sh:
/var/www/citro-docs-2/start.sh
#!/bin/bash
npm run serve
I tried to create a service like that:
/etc/systemd/system/docusaurus.service
[Unit]
Description=Docusaurus Service
[Service]
ExecStart=/var/www/citro-docs-2/start.sh
WorkingDirectory=/var/www/citro-docs-2
[Install]
WantedBy=multi-user.target
But when I start it with sudo systemctl start docusaurus.service and check the status I get these errors:
● docusaurus.service - Docusaurus Service
Loaded: loaded (/etc/systemd/system/docusaurus.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2021-08-25 11:53:20 UTC; 4s ago
Process: 3494 ExecStart=/var/www/citro-docs-2/start.sh (code=exited, status=1/FAILURE)
Main PID: 3494 (code=exited, status=1/FAILURE)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at require (node:internal/modules/cjs/helpers:94:18)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Object.<anonymous> (/var/www/citro-docs-2/node_modules/#docusaurus/c>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Module._compile (node:internal/modules/cjs/loader:1101:14)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Module.load (node:internal/modules/cjs/loader:981:32)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/r>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at node:internal/main/run_main_module:17:47
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Main process exited, code=exited, status=1/FAILURE
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Failed with result 'exit-code'.
I found a solution where I can start the .js file directly: How do I run a node.js app as a background service? but I don't know if this is also possible for Docusaurus. At least I don't know which .js file I should start. It would be awesome if someone could help.

Yes, it is possible to run Docusaurus as a service on Ubuntu. It's important to mention that what you're trying to do here is quite normal for Node.js apps. After all, if you don't run your Node.js app as a service, then the service would stop running as soon as you close the shell you used to start the app.
However, it seems like most folks don't run Docusaurus on Ubuntu since it's "just a static site" and static sites don't really need a full blown server to run on. For example, you could leverage GitHub Pages to run your docs site.
That being said, Docusaurus is just a Node.js app. It's similar to all other Node.js apps, and you can certainly run it in Ubuntu if you wanted to.
Take a look at the docs on PM2.
The way I would start your app as a service is:
Install Node.js on your Ubuntu server (if it isn't already installed)
Install pm2 globally on your server via npm install -g pm2
Navigate to your app's directory and run npm install to install the dependencies of your app.
Now you can run pm2 start /var/www/citro-docs-2/start.sh --name myDocsSite
Your Docusaurus app should now be running. To stop it, you can run pm2 stop myDocsSite.

Related

How to fix permission denied issue RedisJson?

When I try to load a redis module with loadmodule in redis.conf, redis-server crashes.
The log shows
Module /etc/modules/librejson.so failed to load: /etc/modules/librejson.so: unable to open shared objects file: Permission denied.
I have read the documentation to install RedisJson, but it does not work for me.
If I comment loadmodule line, the server works fine.
Installation process
sudo dnf update
sudo dnf upgrade
sudo dnf install redis
Configuration
cd /etc/redis
nvim /redis.conf
loadmodule loadmodule /etc/modules/librejson.so
supervised systemd
The rest of the file is kept by default
What I did
I created modules folder inside etc folder
I ran chown redis:root over modules folder
I moved the module to /etc/modules because I had read that homeprotection can block redis when trying to read the module in the /home path.
Then I used ls -l to check permissions and owners:
-rwxr-xr-x. 2 user user user 23668904 Feb 15 09:10 librejson.so
after that, I ran chown redis:root (as redis.conf owners): -rwxr-xr-xr-x. 2 redis root 23668904 Feb 15 09:10 librejson.so
and when I ran sudo systemctl restart redis.service:
Job for redis.service failed because the control process exited with error code.
See "systemctl status redis.service" and "journalctl -xeu redis.service" for details.
systemctl status redis.service:
redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: failed (Result: exit-code) since Thu 2023-02-16 13:21:58 CST; 6min ago
Process: 28947 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf --daemonize no --supervised systemd (code=exited, status=1/FAILURE)
Main PID: 28947 (code=exited, status=1/FAILURE)
Status: "Redis is loading..."
CPU: 10ms
Feb 16 13:21:58 fedora systemd[1]: Starting redis.service - Redis persistent key-value database...
Feb 16 13:21:58 fedora systemd[1]: redis.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 13:21:58 fedora systemd[1]: redis.service: Failed with result 'exit-code'.
Feb 16 13:21:58 fedora systemd[1]: Failed to start redis.service - Redis persistent key-value database.
and nothing, I ran out of ideas.
System specs
Fedora 37
Redis server v=7.0.8

Binary from app using winston package don't run as linux service

I have a very simple node js application that uses the npm Winston package.
The code is only a loop printing date using the Winston package The app runs just fine. I created a binary using
npm run build
or
npx pkg -t linux,macos,win . --out-path dist
and it also runs just fine. the problem is that when I run it as a service using the file
[Unit]
Description=Cryptween daemon service
After=systend-user-sessions.service
[Service]
Type=simple
WorkingDirectory=$CRYPTWEEN_BIN_DIR
ExecStart=$CRYPTWEEN_BIN_DIR/cryptween-daemon-linux
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=CttWBot_Console
[Install]
WantedBy=multi-user.target
I get the error
cryptween.service - Cryptween daemon service
Loaded: loaded (/etc/systemd/system/cryptween.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2022-03-24 10:54:45 -03; 1s ago
Process: 213784 ExecStart=/opt/cryptween/bin/cryptween-daemon-linux (code=exited, status=1/FAILURE)
Main PID: 213784 (code=exited, status=1/FAILURE)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.load (node:internal/modules/cjs/loader:981:32)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.require (node:internal/modules/cjs/loader:1005:19)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.require (pkg/prelude/bootstrap.js:1719:31)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at require (node:internal/modules/cjs/helpers:102:18)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Object.<anonymous> (/snapshot/test-error-daemon/src/config/winston-logger.js:2:16)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module._compile (pkg/prelude/bootstrap.js:1794:22)
mar 24 10:54:45 gintama systemd[1]: cryptween.service: Main process exited, code=exited, status=1/FAILURE
mar 24 10:54:45 gintama systemd[1]: cryptween.service: Failed with result 'exit-code'.
Here is the code:
code
I am using ubuntu 20.04
Thank you in advance

Cannot find module 'http-simple-proxy' - although everything is set correctly

I installed http-simple-proxy globally. I even have it as command in the shell. Then - I created a small JS file with the content from the README (the first section, Installation and basic usage), but when I run it with node {name-of-JS-file} - and it failed with:
$ node {name-of-JS-file}
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'http-simple-proxy'
Require stack:
- {name-of-JS-file}
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> ({name-of-JS-file:3:23)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '{name-of-JS-file' ]
}
I have setup the necessary stuff correctly:
$ node -v
v14.16.0
$ npm -v
7.8.0
$ echo $NODE_PATH
/home/petar/.npm-packages
$ ls -la $NODE_PATH
total 24
drwxr-xr-x 6 petar users 4096 Oct 26 15:19 .
drwxr-xr-x 62 petar users 4096 Apr 21 21:22 ..
drwxr-xr-x 2 petar users 4096 Apr 21 20:56 bin
drwxr-xr-x 2 petar users 4096 Oct 19 2020 etc
drwxr-xr-x 3 petar users 4096 Oct 15 2020 lib
drwxr-xr-x 3 petar users 4096 Oct 26 15:19 share
$ which http-simple-proxy
/home/petar/.npm-packages/bin/http-simple-proxy
$ http-simple-proxy
usage: http-simple-proxy [options]
Starts a http-simple-proxy server using the specified command-line options
options:
--version
--config CONFIGFILE Configuration file (YAML or JSON)
--configloader JS-FILE Provide js file as config loader
--configloader-test Test config configloader by printing out its output
--watch Watch config for changes and automatically reload with zero downtime
--silent Silence the log output
--user USER User to drop privileges to once server socket is bound
--group GROUP Group to drop privileges to once server socket is bound
--show-rules Show all rules upon every config load
-h, --help You're staring at it
See https://github.com/gusnips/http-simple-proxy for further info
Any idea what the issue might be?
I installed http-simple-proxy globally.
Globally installed packages are not available for import/require from Node.js programs. You need to install it locally, or at least in the path to your project. Note that the README say to npm install --save http-simple-proxy and does not say to install it globally. Install it globally to get the executable available to your shell, but not to use in a Node.js program itself.

Systemd node app failing to start

I have a node app, that I'm trying to add as a service, the app works fine if I go to the directory where it's installed and do node start.js however when I do systemctl start app it just hangs for a few mins then journalctl -u darknet shows:
Dec 24 01:46:33 Skynet systemd[1]: Started darknet.
Dec 24 01:46:33 Skynet systemd[1]: Starting darknet...
Dec 24 01:46:34 Skynet darknet[32246]: module.js:434
Dec 24 01:46:34 Skynet darknet[32246]: return process.dlopen(module, path._makeLong(filename));
Dec 24 01:46:34 Skynet darknet[32246]: ^
Dec 24 01:46:34 Skynet darknet[32246]: Error: Module version mismatch. Expected 46, got 51.
Dec 24 01:46:34 Skynet darknet[32246]: at Error (native)
Dec 24 01:46:34 Skynet darknet[32246]: at Object.Module._extensions..node (module.js:434:18)
Dec 24 01:46:34 Skynet darknet[32246]: at Module.load (module.js:343:32)
Dec 24 01:46:34 Skynet darknet[32246]: at Function.Module._load (module.js:300:12)
Dec 24 01:46:34 Skynet darknet[32246]: at Module.require (module.js:353:17)
Dec 24 01:46:34 Skynet systemd[1]: darknet.service: main process exited, code=exited, status=1/FAILURE
Dec 24 01:46:34 Skynet systemd[1]: Unit darknet.service entered failed state.
Dec 24 01:46:34 Skynet systemd[1]: darknet.service failed.
This is my .service file:
[Unit]
Description=darknet
After=network.target
[Service]
ExecStart=/usr/bin/node /home/botty/Darknet/start.js
Restart=always
RestartSec=180
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=darknet
User=botty
Group=botty
Environment=NODE_ENV=production
WorkingDirectory=/home/botty/Darknet
[Install]
WantedBy=multi-user.target
I tried running the process both as regular user and root and it works fine .. but no matter what I try it gives errors if started with systemd.
I had this problem. I wasn't using the same node version in systemd as when running locally.
$ which node
/usr/local/bin/node
Replacing /usr/bin/node with /usr/local/bin/node (or the equivalent in which node, such as when using nvm) in ExecStart fixed it for me.
Contrary to a popular misconception, systemd is very different from traditional Unix concept of init scripts. It is explained in much more detail here and here. I have noticed that a lot of people here are asking about problems with running Node with systemd and they rarely get any solutions. My recommendation would be to use traditional SysV init scripts or Upstart that also works with no surprises whenever you see that you cannot do something with systemd. I have never seen situations where a Node application couldn't be easily run with Upstart or SysV init scripts but I've seen problems with systemd. I've heard that it is especially problematic with clustering and other more complicated deployment scenarios. Just follow the Unix philosophy and use simple tools that do one thing and do it well. Init scripts are not that complicated.
See also:
create systemd nodejs express-generator

systemd cannot run service after running commands

I tried to run systemd using the commands systemctl enable photogrid.service & systemctl start photogrid.service in ubuntu 16
The nodejs app itself can run as expected. The service is to ensure that application will auto-start when application crash or server reboot.
The service apparently did not start. So I key in systemctl status photogrid.service to see what happened, the below is what I got from the terminal.
● photogrid.service - Photogrid
Loaded: loaded (/lib/systemd/system/photogrid.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Wed 2016-11-09 04:35:36 UTC; 7s ago
Process: 27523 ExecStart=/usr/local/bin/node /home/ubuntu/photogrid/app.js (code=exited, status=203/EXEC)
Main PID: 27523 (code=exited, status=203/EXEC)
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Main process exited, code=exited, status=203/EXEC
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Unit entered failed state.
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Failed with result 'exit-code'.
This the script that I wrote for the service under the path /lib/systemd/system/photogrid.service
[Unit]
Description=Photogrid
[Service]
Type=simple
Restart=always
RestartSec=10
Environment=NODE_ENV=production
ExecStart=/usr/local/bin/node /home/ubuntu/photogrid/app.js
[Install]
WantedBy=multi-user.target
Basically under ExecStart make sure you point to the correct nodejs executable. For my case it was in a different folder and not /usr/local/bin/node, to check where is your node executable. (Assuming you confirm you have downloaded and install it correctly in linux) use command which node to give you path direction.

Resources