NodeJS forever after #reboot - node.js

I am working with React and need my web page to be alive after reboot.
So I need forever after crontab.
What I have tried.
crontab -e
#reboot ~/reboot.sh
#reboot sudo service nginx restart
#!/bin/bash
cd ~/lacirolnikdev && sudo ~/.nvm/versions/node/v14.13.1/bin/forever start -c "/.nvm/versions/node/v14.13.1/bin/npm start" .
cd ~/coinwork && sudo ~/.nvm/versions/node/v14.13.1/bin/forever start -c "/.nvm/versions/node/v14.13.1/bin/npm start" .
I tried absolute paths, sudo and moving to directories.
Commands works fine besides crontab.
Thank you Laci

A better way to start processes after boot would be a systemd unit file, the de-facto standard init daemon on most distributions. systemd takes care of starting your application, is capable of monitoring and restarting it on crashes.
Have a look at the documentation for unit files here:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
This is an example for nginx:
/lib/systemd/system/nginx.service
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target

Related

Systemd ExecStart with arguments

I have process, that i run in this way :
sudo RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
I would like to lunch it on raspberry boot with systemctl like that :
[Unit]
Description=Basic station secure websocket
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
ExecStart= RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
[Install]
WantedBy=multi-user.target
Alias=basic_station.service
So i want to know how put the argument
RADIODEV=/dev/spidev0.0
-d
-h /opt/basicstation/build-rpi-std/bin
because wheni just put :
ExecStart= RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
That's not work
I already check some issue like :
issue systemd
But i can't reproduce what they propose.

Could not edit systemd service file

Need to edit following entries:
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
in /lib/systemd/system/docker.service file
$ sudo -E systemctl edit docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
did not update the service file after restart(sudo systemctl restart docker.service)
Edit
On AWS EC2, below is the issue:
$ nano /etc/systemd/system/docker.service.d/override.conf
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
/lib/systemd/system/docker.service still shows unmodified
1) What is the recommended approach to edit service file(docker.service)?
2) Why /lib/systemd/system/docker.service cannot be edited directly?
You need to create a systemd drop-in file for docker.service.
Create /etc/systemd/system/docker.service.d/override.conf file with contents
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
Reload systemd unit files.
systemctl daemon-reload
NOTE: Reloading systemctl daemon is required after editing any systemd unit file that's as per systemd design. For more info check this.
Restart docker daemon.
systemctl restart docker
You need to restart docker daemon to pick up the latest updated systemd unit file.
For more info check this.

How to run Nginx within a Docker container and set it up to reload its configuration every 6h?

I'm runnign NGINX in a Docker container, and I'm trying to find a way to start it up using docker run in a way that would order my NGINX to reload its configuration every 6h. I need this to be done, because I also use Certbot in another container, and I'd like the SSL certificate renewal process to be automated and applied, and for that I neen NGINX to reload its configuration on schedule.
At this point I use the folowing docker run sequence (more or less):
docker run --restart=always -d -p 80:80 -p 443:443 \
--name=nginx_RevPr nginx:latest \
/bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"'
The container created this way is running and working fine. The only problem I have is that I actually don't know if NGINX is really realoading its configuration. docker logs doesn't show anything.
I'd like to approach this problem in a twofold way:
How can I output to terminal / docker logs (echo?) the succcessful nginx -s reload modifying my /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"'?
In case I miss something (I'm a beginner for both Docker and NGINX): do you see a way to achieve the results I'd like to get in a more reasonable way?
Please don't suggest Docker Compose.
Thank you for your attention.
OK, I got it working.
I changed the CMD args line to:
/bin/sh -c 'while :; do sleep 6h & wait ${!}; nginx -s reload && echo NGINX config reload for Certbot - OK; done & nginx -g "daemon off;"'
My main mistake was to enter the CMD using /bin/bash, where I should do /bin/sh. And the following nginx -s reload && echo NGINX config reload for Certbot - OK solved the problem with notyfying docker logs about config reload.

Use "npm start" as command to forever in .sh script

I have a .sh script in my /etc/init-d/forever to configure how forever starts and stops my Node.js app.
I wanted to start forever with the command npm start, so I can trigger my scripts from there. Is this possible?
I tried
sudo forever start --sourceDir /home/my-app -c npm start
but its gets wrong interpreted...
info: Forever processing file: start
error: Cannot start forever
error: script /root/start does not exist.
My script so far is:
NAME=nodeapp
SOUREC_DIR=/home/nodeapp
COMMAND="npm start"
SOURCE_NAME=index.js
USER=root
NODE_ENVIROMENT=production
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever=forever
start() {
export NODE_ENV=$NODE_ENVIROMENT
echo "Starting $NAME node instance : "
touch $logfile
chown $USER $logfile
touch $pidfile
chown $USER $pidfile
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo -H -u $USER $forever start --pidFile $pidfile -l $logfile -a --sourceDir $SOUREC_DIR -c $COMMAND
RETVAL=$?
}
So I found the answer.
Both --sourceDir and the path parameter after the "npm start"command where necessary:
sudo forever --sourceDir /home/my-app -c "npm start" /
The below command run the forever command in the background with logs in forever.
forever start -c "ng serve " ./
Note the ./
Then you can
forever list
And will be able to see the status and the location of the log file.
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] wOj1 ng serve 29500 24978 /home/user/.forever/wOj1.log 0:0:25:23.326

How can I run this script automatically on startup

I have a game server on my VPS, but i have a strong problem. When it reboots(for technical reasons or something) the game server doesn't start automatically. I use this script, which is located in /home/steam/csgo-ds:
#!/bin/sh
ln -s /home/steam/csgo-ds/csgo/*.dem /var/www/html/
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
cd /home/steam/csgo-ds
chmod 777 * -R
screen -S "CS:GO Server" ./srcds_run -game csgo -usercon +game_type 0 +game_mode 0 -tickrate 64 -maxplayers 24 -maxplayers_override 24 +ip 188.116.46.148 -port 27015 +sv_setsteamaccount "XXXXX" -exec server.cfg +tv_enable 1 +tv_maxclients 0 +tv_port 27020 +tv_advertise_watchable 0 +map jb_dust2_final2
I have tried adding it to crontab, startup files and a lot more and nothing worked.
Operating system on the VPS is Ubuntu Server 64-bit 14.04 upgraded to 16.04
Ubuntu 16.04 uses systemd as init system, Follow these steps:
chmod 744 /path/to/script
Now create a unit file:
vim /etc/systemd/system/csgo.service
[Unit]
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/path/to/script
[Install]
WantedBy=default.target
Set permissions:
chmod 664 /etc/systemd/system/csgo.service
Reload and enable the service:
systemctl daemon-reload
systemctl enable csgo.service
Now reboot and test it out.
there are different ways of doing this , the easiest way is to put 5 line of your code in :
/etc/rc.local
it will be executed automatically on each os boot
you should put your lines of code under this line:
exit 0

Resources