I'm on the IBM i v7r3
When I run a command/program interactively in Qshell it runs successfully. But when I try to run this in start.sh it fails with Command serve not found.
I start an interactive Qshell session.
STRQSH
$ cd node/vlegacireactjs
$ serve -s build -l 8010
The above starts a react server which I confirm is reachable when I point my browser at port 8010.
I am trying to run a program that will start the server in batch.
SBMJOB CMD(QSH CMD('/home/RROGERSON/node/vlegacireactjs/start.sh')) +
JOB(NODE_REACT) JOBQ(QS36EVOKE) CPYENVVAR(*YES)
And then start.sh contains
#1/usr/bin/sh
cd /home/RROGERSON/node/vlegacireactjs
serve -s build -l 8010
The job NODE_REACT fails (as seen in the QPRINT spool file) with Command serve not found .
Does anyone know what I have to add to start.sh to get this to run?
Thanks,
Rob
I should have paid more attention to the error. It was telling me the command serve was not found.
So I changed to
/QOpenSys/pkgs/lib/nodejs10/bin/serve -s build -l 8010
and the command is found. The module serve was installed globally.
Thanks for the suggestion Mark
Related
I'm trying to build a docker file in which I first download and install the Cloud SQL Proxy, before running nodejs.
FROM node:13
WORKDIR /usr/src/app
RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
RUN chmod +x cloud_sql_proxy
COPY . .
RUN npm install
EXPOSE 8000
RUN cloud_sql_proxy -instances=[project-id]:[region]:[instance-id]=tcp:5432 -credential_file=serviceaccount.json &
CMD node index.js
When building the docker file, I don't get any errors. Also, the file serviceaccount.json is included and is found.
When running the docker file and checking the logs, I see that the connection in my nodejs app is refused. So there must be a problem with the Cloud SQL proxy. Also, I don't see any output of the Cloud SQL proxy in the logs, only from the nodejs app. When I create a VM and install both packages separately, it works. I get output like "ready for connections".
So somehow, my docker file isn't correct, because the Cloud SQL proxy is not installed or running. What am I missing?
Edit:
I got it working, but I'm not sure this is the correct way to do.
This is my dockerfile now:
FROM node:13
WORKDIR /usr/src/app
COPY . .
RUN chmod +x wrapper.sh
RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
RUN chmod +x cloud_sql_proxy
RUN npm install
EXPOSE 8000
CMD ./wrapper.sh
And this is my wrapper.sh file:
#!/bin/bash
set -m
./cloud_sql_proxy -instances=phosphor-dev-265913:us-central1:dev-sql=tcp:5432 -credential_file=serviceaccount.json &
sleep 5
node index.js
fg %1
When I remove the "sleep 5", it does not work because the server is already running before the connection of the cloud_sql_proxy is established. With sleep 5, it works.
Is there any other/better way to wait untill the first command is completely done?
RUN commands are used to do stuff that changes something in the file system of the image like installing packages etc. It is not meant to run a process when the you start a container from the resulting image like you are trying to do. Dockerfile is only used to build a static container image. When you run this image, only the arguments you give to CMD instruction(node index.js) is executed inside the container.
If you need to run both cloud_sql_proxy and node inside your container, put them in a shell script and run that shell script as part of CMD instruction.
See Run multiple services in a container
You should ideally have a separate container per process. I'm not sure what cloud_sql_proxy does, but probably you can run it in its own container and run your node process in its own container and link them using docker network if required.
You can use docker-compose to manage, start and stop these multiple containers with single command. docker-compose also takes care of setting up the network between the containers automatically. You can also declare that your node app depends on cloud_sql_proxy container so that docker-compose starts cloud_sql_proxy container first and then it starts the node app.
I am doing a chat app and integrating it on a website. When i execute teh command 'node index.js' on the local server everything works fine. But when i try installing node js on a dedicated server and try to execute the command 'nohup node index.js &' through ssh it gives following message.
nohup: ignoring input and appending output to `nohup.out'
I had followed the method mentioned in this site for installation of node js on server https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts
Can someone help me, please?
You first need to install Node in a correct way. I wrote a tutorial about it: How to get Node 6.7.0 on Linux (of course you can use newer versions, just change the version in the commands).
Basically it's something like this - change the version to the one you like:
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
I recommend building Node from source and always running make test but you can also install a binary package which is faster - just make sure you understand the issues with paths and hashbang lines if you do so - more info on that and more install options are described in my tutorial.
Then you need to make sure that your application is started every time the server is restarted. I recommend using Upstart if you can.
Using Upstart, save something like this in /etc/init/YOURAPP.conf:
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [06]
# If the process quits unexpectadly trigger a respawn
respawn
# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1
Just change:
YOURAPP to the name of your own app
/opt/node/bin/node to your path to node
/www/YOURAPP/app/app.js to the path of your Node app
/www/YOURAPP/run to where you want your PID file
/www/YOURAPP/log to where you want your logs
--chuid node to --chuid OTHERUSER if you want it to run as a different user than node
(make sure to add a user with a name from --chuid above)
With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run:
start YOURAPP
restart YOURAPP
stop YOURAPP
to start, restart and stop your app - which would also happen automatically during the system boot or shutdown.
For more info see those answers about:
Installing Node
Running Node on servers
You can also use systemd for that but there are some differences as the system is much more complicated and often leads to some problems.
I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:
#reboot /var/www/ghost/launch.sh
The script has the following code:
#!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi
When I sudo reboot the server, and use forever list to find the processes running, I see the following:
data: [0] sHyo /usr/bin/nodejs index.js 1299 1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957
When I nano to that log file, the log says the following:
^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m
Error: Could not locate a configuration file.
at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
at Object.cb [as oncomplete] (fs.js:168:19)
error: Forever detected script was killed by signal: null
It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????
When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:
^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m
What is wrong with my script or crontab that it cannot launch the script properly?
I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.
That's the thing, your cron job is not doing the same:
#reboot /var/www/ghost/launch.sh
This script is executed from your home directory. One way to fix is to change your crontab:
#reboot cd /var/www/ghost; ./launch.sh
Another way is to add this line near the top of launch.sh, anywhere before launching forever:
# change to the directory of this script
cd $(dirname "$0")
Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.
What are the steps required for setting up a Node.js application on Webfaction shared hosting account?
Introduction
The result of the following instructions is a Node.js application that is running continously, produces logfiles and restarts itself even after the reboot of the server machine. The instructions are especially targeted for the shared host accounts on Webfaction but can be used for general purpose also.
Install Node.js
Eventhough Node.js download page offers Linux binaries, it would be more robust to install Node.js from source. Download the most recent source codes and extract them to ~/src/.
(log in to your webfaction console)
> mkdir -p ~/src/
> cd src/
> wget http://nodejs.org/dist/v0.10.18/node-v0.10.18.tar.gz
> tar -xzf node-v0.10.18.tar.gz
The next step is to compile and install the code. For this Python is used. Because the default python version is 2.4 (at least on my Webfaction server web223) and because the Node.js installation requires one of the versions 2.6+, you must temporarily set newer version to be the default. See the following snippet (also see this article for details). Note --prefix=$HOME which is required due Webfaction's environment restrictions (you have access only to your home directory).
> cd node-v0.10.18/
> alias python=python2.7
> python configure --prefix=$HOME
> make
> make install
Node.js installed. Verify the success by
> node -v
v0.10.18
That also installed node package manager npm.
> npm -v
1.3.8
Install Forever
To keep run Node.js application running as long as possible and logging the output for maintenance, you need Forever. For convenience, install it globally (for you) using flag -g.
> cd ~
> npm install -g forever
> forever --version
v0.10.8
You can later update forever by
> npm update -g forever
Start the Application
First, create a custom application via Webfaction Control Panel (Custom app (listening on port)). Name it for example foonode. You may also create a domain and website for the app.
Second, make a note about the socket port number that was given for the app by Webfaction. Let the example be 48888.
Third, copy your application code to the directory of the app i.e. ~/webapps/foonode/. After that the directory contents would for example be the following.
> cd ~/webapps/foonode/
> ls
node_modules/
index.js
Index.js would be something like the snippet below. The key point is using the given port number for the app.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Universe.\n');
}).listen(48888, '127.0.0.1');
console.log('Listening at http://127.0.0.1:48888/');
Now you could run the app by node index.js. However there is a problem: when you exit the shell, also the app exits. Therefore you should run the Node.js as a daemon. That is something where Forever does a lot for you. Start node with forever:
> cd ~/webapps/foonode/
> forever start index.js
Now the app continues running after you logout and, as a very nice thing to have, is quickly restarted if it happens to crash. This is a lot but still not enough. What if the server reboots? Where is the output from the app going? What if the script crashes continously? What if you have multiple scripts with the name index.js? Keep reading.
Use Absolute Paths with Forever
Forever identifies the process by the filename of the script fed to forever start <filename_of_script>. A problem arises when you have multiple applications with the same filename for the script.
For example consider you have two applications /home/foouser/webapps/foonode/index.js and /home/foouser/webapps/barnode/index.js. If you now start both with forever start index.js within the directories of the applications and then run forever stop index.js only once, the result is that both applications become stopped. This happens because they both had the same identity index.js
The solution is to use the absolute filepaths when specifying the script.
> forever start /home/foouser/webapps/foonode/index.js
And to stop execution:
> forever stop /home/foouser/webapps/foonode/index.js
This ensures that only the intended application becomes stopped. This habit of using absolute paths has also positive effects to forever list. See this issue for details.
Logging
To store the application output to somewhere, specify the following arguments.
-l <logfile> = stream all output to this file. Relative to ~/.forever/
-o <logfile> = stream script's stdout to this file. Relative to current dir.
-e <logfile> = stream script's stderr to this file. Relative to current dir.
-a = append to the files instead of overwriting them.
It seems convenient to have a subdirectory for the logs, e.g. logs/. With the arguments, absolute path and the subdirectory logs/ the command becomes the following:
> cd ~/webapps/foonode/
> mkdir -p logs/
> forever start -a -l forever.log -o logs/out.log -e logs/err.log /home/foouser/webapps/foonode/index.js
Detecting and Restarting a Spinning Process
Forever has the parameters --minUptime and --spinSleepTime which are not so well documented currently. The meaning of the parameters is to control situation where the script crashes and almost immediately crashes again after restart. As forever tries to restart the script as soon as possible after the crash, this can lead to a busy loop which may eat lots of resources of the server.
--minUptime specifies the number of milliseconds the script has to be up and running without crashes to be restarted immediately. If the uptime of the crashed script is less than minUptime, then the script is considered spinning i.e. problematic. If uptime is greater then the script is considered non-spinning.
If a spinning script crashes i.e. the uptime of the script is less than --minUptime then forever waits --spinSleepTime number of milliseconds before restarting the script. Otherwise the script is restarted as soon as possible. This prevents the resource-eating loop. See this answer for futher discussion.
I personally use 5000 for --minUptime to make sure that Node is fully started before declaring it non-spinning. 2000 would be a good one for --spinSleepTime to avoid the loop but still trying to start the script quickly after the problematic situation resolves.
> forever start -a -l forever.log -o logs/out.log -e logs/err.log --minUptime=5000 --spinSleepTime=2000 /home/foouser/webapps/foonode/index.js
Manage the Application
As the commands grow, memorizing and writing them becomes more and more cumbersome. Therefore it is convenient to copy them into a Makefile. The makefile also becomes handy later on when you need to specify a command to run after the server reboot.
> cd ~/webapps/foonode/
> cat Makefile
foreverstart:
# Run server forever (until reboot)
mkdir -p logs
forever start -a -l forever.log -o logs/out.log -e logs/err.log --minUptime 5000 --spinSleepTime 2000 /home/foouser/webapps/foonode/index.js
foreverstop:
forever stop /home/foouser/webapps/foonode/index.js
Keeping the Application Running
Forever does not cover the case where the server reboots. For that you need to specify a cronjob with #reboot rule that start the forever after reboot.
> export EDITOR=nano # Editor to use to edit crontab. A matter of taste.
> crontab -e
Add the following line and save.
#reboot make -C ~/webapps/foonode/ -f ~/webapps/foonode/Makefile foreverstart
The line ensures that foonode is started immediately after the server reboot. The flag -C specifies the directory to run the makefile from and -f the actual location of the makefile. See this answer for details about using #reboot.
Conclusion
Now you have a node process running truly forever or at least as long as there is maintenance guys feeding the server with electricity. Lots of things done but maybe more will come. Things you may like to do in the future which were not covered here includes the following.
Watching for file changes and automatically restarting when detected (See --watch in forever --help)
Using Grunt.js instead of Make (See bustardcelly/grunt-forever)
Backupping the application once in a while.
See also
Setting up Redis on Webfaction
Any ideas, comments or corrections?
Well, you probably already followed all the steps in the other answer (wow! such detail! great), but it is now a lot easier: they now have a one-click installer for node.js.
Another option is to install nvm (Node Version Manager) and type:
nvm install node
It's not only valid for Webfaction.
I am trying to run node server using forever command.
I installed forever globally using:
npm install forever -g
After installing forever I try to run my node script by using below command:
node_modules\.bin\forever start app.js
Below is my console:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info: Forever processing file: app.js
Please help me to resolve this issue!
There is no problem here other than warnings for configs forever recommends you declare. If you see the final message there it tells you it has processed your script. Just run forever list and you should see your script running.
I ran into this same thing when installing npm via yum repository ( yum install npm ) and then installing forever whereas when I install node and npm via shell scripts and then install forever it doesn't occur. It must have something to do with the formulas for the package installer or potentially missing alias with flags with installer to set those values behind the scene.
Those don't mean it's not working. See below I created a js file using sample code from node's site and ran it manually (I flushed firewall to open port for app temporarily but you don't need that):
[root#app1 ~]# vi example.js
[root#app1 ~]# apf -f
apf(23924): {glob} flushing & zeroing chain policies
apf(23924): {glob} firewall offline
[root#app1 ~]# node example.js
Server running at http://127.0.0.1:1337/
I then start app using forever:
^C[root#app1 ~]# forever start example.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: example.js
Now I check to see if my app is running:
[root#app1 ~]# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] dan1 /usr/bin/node example.js 23976 23978 /root/.forever/dan1.log 0:0:0:27.320
[root#app1 ~]#
This solved my issue:
forever start -c node [path/to/app]
"-c" means - Run commnad; and then just run via nodejs
This way - you get the Respawn by default of min. 1000ms uptime
Taken from: https://github.com/nodejitsu/forever/issues/422, by "Basarat"
If you are using node js with express framework then script will not start using :
forever start app.js
First stop all running apps:
forever stopall
When this Express framework used it must be started with:
forever start ./bin/www
First stop all running apps:
forever stopall
then use this command. It works for me and solved my issue:
forever -w ./bin/www
and you should find this in package.json file:
"scripts": {
"start": "node ./bin/www"
}
I hope it helps you.
One thing that also produces same kind of output, but doesn't start the application is if forever is unable to write to the specified log file. I had a case where the log file had become too big and this prevented the process from starting.
Firstly change your package.json scripts like
"scripts": {
"start": "forever ./bin/www.js"
}
than start this command on linux console:sudo npm start
for windows just :npm start
I just ran into this today on an AWS Lightsail server, and NONE of the answers here or elsewhere had any effect. Everything worked fine until upgrading from NodeJS 10.x to 13.x. I tried removing and reinstalling forever, changing the permissions on the files and directories, etc, and I kept getting the EACCES error. The issue seemed to be that forever could not create directories within its .forever directory. The only thing that worked was to do the following:
1) Remove the .forever folder and all its subfolders and contents. For me, this was accomplished as follows:
sudo rm -rf /home/bitnami/.forever
2) Manually recreate the .forever folder:
sudo mkdir /home/bitnami/.forever
3) Manually set the permissions on the .forever folder:
sudo chmod -R o+rwx /home/bitnami/.forever
4) Manually recreate the .forever/pids folder:
sudo mkdir /home/bitnami/.forever/pids
5) Manually set the permissions on the .forever/pids folder:
sudo chmod -R o+rwx /home/bitnami/.forever/pids
6) Manually recreate the .forever/sock folder:
sudo mkdir /home/bitnami/.forever/sock
7) Manually set the permissions on the .forever/sock folder:
sudo chmod -R o+rwx /home/bitnami/.forever/sock
8) Run my NodeJS app via forever again with the sudo command.
9) List the processes forever is running, and verify that my app was there.
I'm not sure why I had to go through all of this, as setting the permissions recursively should have done the same thing, but after doing this, forever started running perfectly as it did before.
Hope this helps someone.
forever stopall
cd /<app-folder>
forever -w ./bin/www
This just worked for me in a Google Cloud Bitnami VM
If you setup a module globaly (-g option) "forever" is in the $path
forever start app.js
should work.