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
Related
I am trying to run a process with nobody user in Linux, currently this is being run as a root user but since this process doesn't require the root access so I want to use nobody with gosu. The problem is even after activating the nobody user and running the process with that, when I do " ps aux" it shows that all processes are being run by root. Do I need to do something more after activating the nobody user to make it possible to run the process. The process I am trying to run with nobody is rails s -b 0.0.0.0
Below is my dockerfile
FROM ruby:3.0.1
EXPOSE $PORT
WORKDIR /srv
COPY Gemfile Gemfile.lock /srv/
COPY . /srv
RUN apt-get update -qq && apt-get install -y build-essential iproute2 libpq-dev nodejs && apt-
get clean && bundle install --no-cache
#activating the nobody user account
RUN chsh -s /bin/bash nobody
RUN set -eux; \
apt-get install -y gosu; \
rm -rf /var/lib/apt/lists/*; \
gosu nobody true
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["server"]
Here is the docker-entrypoint.sh
#!/bin/sh
export BASH_SHELL=$(cat /etc/shells | grep /bash)
export ASH_SHELL=$(cat /etc/shells | grep /ash)
#Setting available Shell to $SHELL_PROFILE
if [ -n "$BASH_SHELL" ];
then
SHELL_PROFILE=$BASH_SHELL
elif [ -n "$ASH_SHELL" ];
then
SHELL_PROFILE=$ASH_SHELL
else
SHELL_PROFILE=sh
fi
rm -f tmp/pids/puma.5070.pid tmp/pids/server.pid
XRAY_ADDRESS="$(ip route | grep default | cut -d ' ' -f 3):2000"
export AWS_XRAY_DAEMON_ADDRESS=$XRAY_ADDRESS
echo "export AWS_XRAY_DAEMON_ADDRESS=$XRAY_ADDRESS" >> /root/.bashrc
case "$*" in
shell)
exec $SHELL_PROFILE
;;
server)
# gosu command to run rails s -b 0.0.0.0 process as nobody user
gosu nobody:nogroup bundle exec rails s -b 0.0.0.0
;;
*)
exec $#
;;
esac
Don't bother installing gosu or another tool; just set your Docker image to run as the nobody user (or some other non-root user). Do this at the very end of your Dockerfile, where you otherwise declare the CMD.
# Don't install gosu or "activate a user"; but instead
USER nobody
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]
In turn, that means you can remove the gosu invocation from the entrypoint script. I might remove most of it and trim it down to
#!/bin/sh
# Clean up stale pid files
rm -f tmp/pids/*.pid
# (Should this environment variable be set via `docker run`?)
export AWS_XRAY_DAEMON_ADDRESS="$(ip route | grep default | cut -d ' ' -f 3):2000"
# Run whatever the provided command was, in a Bundler context
exec bundle exec "$#"
If you need an interactive shell to debug the image, you can docker run --rm -it the-image bash which works on many images (provided they (a) honor CMD and (b) have bash installed); you don't need a special shell artificial command and you don't need to detect what's installed in the (fixed) image.
I need to execute some commands as root and some commands as different user when building image. All of the commands should be executed inside of one RUN statement to preserve a background service created by the first command. I tried:
USER root
RUN sudo -i -u postgres "pg_ctl -w start" \
&& cd /home/backend && npm run server & sleep 50 \
&& cd /home/frontend && npm run test
It gives me error:
-bash: pg_ctl -w start: command not found
The command '/bin/sh -c sudo -i -u postgres "pg_ctl -w start" && cd /home/backend && npm run server & sleep 50 && cd /home/frontend && npm run test' returned a non-zero code: 127
If I do:
USER postgres
RUN pg_ctl -w start \
&& cd /home/backend && npm run server & sleep 50 \
&& cd /home/frontend && npm run test
Everything works fine. So, what is the problem with previous code?
The Dockerfile is created from node:12.18.0 which is based on Debian 9.
Remove quotes, the executable is pg_ctl not pg_ctl -w start.
RUN sudo -i -u postgres pg_ctl -w start \
what is the problem with previous code?
The same as if you would do:
RUN "pg_ctl -w start" \
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
Need to increase watchers in docker image, as it fails on expo publish with the error
[11:39:08] Error: ENOSPC: System limit for number of file watchers reached, watch '/__w/mevris-client-app-products/mevris-client-app-products/node_modules/update-notifier/node_modules/camelcase'
[11:39:08] at FSWatcher.start (internal/fs/watchers.js:165:26)
[11:39:08] at Object.watch (fs.js:1258:11)
[11:39:08] at NodeWatcher.watchdir (/__w/mevris-client-app-products/mevris-client-app-products/node_modules/metro/node_modules/sane/src/node_watcher.js:159:22)
[11:39:08] at Walker.<anonymous> (/__w/mevris-client-app-products/mevris-client-app-products/node_modules/metro/node_modules/sane/src/common.js:109:31)
[11:39:08] at Walker.emit (events.js:198:13)
[11:39:08] at /__w/mevris-client-app-products/mevris-client-app-products/node_modules/walker/lib/walker.js:69:16
[11:39:08] at go$readdir$cb (/__w/mevris-client-app-products/mevris-client-app-products/node_modules/#react-native-community/cli/node_modules/graceful-fs/graceful-fs.js:187:14)
[11:39:08] at FSReqWrap.args [as oncomplete] (fs.js:140:20)
Added following lines to Dockerfile
RUN echo "fs.inotify.max_user_instances=524288" >> /etc/sysctl.conf && sysctl -p
results in this error when build
sysctl: setting key "fs.inotify.max_user_watches": Read-only file system
I need to use that docker image in Github Actions
Dockerfile
FROM node:10
RUN echo "fs.inotify.max_user_instances=524288" >> /etc/sysctl.conf
RUN echo "fs.inotify.max_user_watches=524288" >> /etc/sysctl.conf
RUN echo "fs.inotify.max_queued_events=524288" >> /etc/sysctl.conf
RUN apt-get -qq update && apt-get -qq -y install bzip2
RUN yarn global add #bluebase/cli && bluebase plugins:add #bluebase/cli-expo && bluebase plugins:add #bluebase/cli-web
RUN bluebase plugins
RUN npm i -g expo-cli
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["sh", "/entrypoint.sh"]
I had the same issue running Docker on Mac OSX (not docker-for-mac).
Based on the premise that the sysclt settings are shared with the kernel host,
I fixed the problem doing ssh to the docker-machine (boot2docker) and changing the settings there.
$ docher-machine ssh
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p
From this issues/24 and this issues/628
You need to increase the fs.inotify.max_user_watchesparameter on the
host. For example you can create a configuration file in
/etc/sysctl.d. Example /etc/sysctl.d/crashplan.conf with content:
fs.inotify.max_user_watches = 1048576
You can not change at build time is it will not affect and also it will not allow you during build time.
The workaround is to avoid getting this error, set it during run time in the entrypoint.
FROM node:10.16
# set inotify and start the node application, replace yar with your command
RUN echo "#!/bin/sh \n\
echo "fs.inotify.max_user_watches before update" \n\
cat /etc/sysctl.conf\n\
echo "______________________________________________updating inotify ____________________________________" \n\
echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf && sysctl -p \n\
echo "updated value is" \n\
cat /etc/sysctl.conf | grep fs.inotify \n\
exec yarn start:dev \
" >> /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
# EXPOSE TARGET PORT
EXPOSE 3001
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
try to echo "fs.inotify.max_user_watches=524288" >> /etc/sysctl.conf
source: whats-wrong-with-my-simple-react-docker-image
PS. Maybe it is a problem of a parent host? Check param on the host befor run docker. This work to me.
I am getting this error in my Node.js console:
[nodemon] Internal watch failed: watch /home/dell/Downloads/Adaani5.0
(copy).0/node_modules/engine.io/index.js ENOSPC
For it, I have run two commands after an Internet search:
First:
ps -ef | grep node
sudo kill -9
Second:
echo fs.inotify.max_user_watches=582222 | sudo tee --append
/etc/sysctl.conf&& sudo sysctl -p
And the error persists.
sudo nodemon
Resolved for me. Or login to Root user.