I'm using the Mediatek 7688 board running OpenWRT linux to create an IoT device. I have written the app in NodeJS and want it to be executed anytime the board boots up.
I have tried the solution given [here] (How to auto start an application in openwrt?) while this works but the board seems to be unable to complete the boot process (the NodeJS app doesn't exit). I have also tried the pm2 npm module but am running into issues with diskspace during installation.
Is there a way to reduce the "installed" size of the pm2 module? Or maybe a way to fire up the NodeJS scripts upon startup without using the module.
Thanks in advance!
So I was only using the pm2 module to ensure that:
The program started at bootup
The program was restarted in case it crashed
To accomplish the first part and since my program was a node.js program, I made it into an executable file by adding #!/bin/sh env node as the first line in the file. Must ensure that the line ends in a LF line ending and not CRLF as in case of windows systems. Once done, I granted execute permission to the .js file by calling chmod a+x myfile.js.
I then created an init script in the /etc/init.d folder and enabled that script - as explained here
Now to ensure that the process restarted automatically in case it ever crashed, I a "cron script", like so and saved it a restart.sh in the root folder:
#bin/sh
if pgrep -f myfile.js > dev/null
then
#process is already running - do nothing
else
/etc/init.d/myprocess start
fi
And finally setup a crontab -e with * * * * * ~/restart.sh so that the restart.sh gets executed every minute to ensure that the process is running.
Related
I have a small node.js (v13.5.0) server running under PM2 (v4.4.0) on a RHEL 7.6 box. As a part of it's function it writes a small CSV file to the disc for every incoming request. This app is writing these files with the perms (rw-r-----).
The user I am having PM2 executing my process as has umask set to 0022. I have confirmed this is effective as it works in other cases.
So the main problem is that I cannot get my pm2+node combo to use the umask to write files with the right permissions.
Any suggestions where i am going wrong?
Thanks in advance,
So using ps fu -u $USER i figured out that PM2 daemon process had been running since i first started it (started column) - which was way before I made the umask change. As a result it was running with an outdated environment.
I restarted it using pm2 kill && pm2 start && pm2 ping which should return a pong. Subsequent tests proved that this had fixed the issue.
I am using upstart to start my golang application. I have my application folder structure like this,
web-app/
/app
main.go
I built the application as below,
$cd /home/ec2-user/go/src/github.com/dineshappavoo/web-app/app/
$go build ./...
It generated the binary app
And placed the web-app.conf in /etc/init/ folder. Here is the web-app.conf content,
#Web app upstart script
description "start and stop web app"
start on (net-device-up
and local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
respawn limit 5 30
console output
script
chdir /home/ec2-user/go/src/github.com/dineshappavoo/web-app/app
exec ./app
end script
When I tried sudo initctl list, it lists the process as stop/waiting. And I tried to start the process
$sudo initctl start web-app
It shows the process as start/running. But it is not started.
I checked the /var/log/messages logs. It shows,
init: web-app main process (18740) terminated with status 127
I couldn't start the process. I think there is some issue with the chdir. I tried different options for past two days. And I am fairly new to upstart but no luck. Could someone help me with this?
OP eventually solved after fixing a few issues. See comments, notably:
upstart might not recognise environment variables
Amazon Linux Image currently uses an old version of init (upstart 0.6.5), lacking newer features such as console log & nested script tags
status 127 can occur if exec can't find the binary
status 1 can occur if binary runs but fails
substituting a simple program in an upstart script can help diagnose errors
On my raspberry pi, I want my own written server to be started at startup, and to be restarted when it segfaults, so I added it to /etc/inittab. The problem is that the server won't start
The line I added:
1:2345:respawn:/home/gear/lionfish/main /home/gear/lionfish/app
When I run this command from the command line it works just fine, but the server doesn't run. I've checked this with ps aux, and it didn't show up
Have I made some sort of mistake?
EDIT: Small side question. The server needs root privileges, does inittab do this automatically or do I need to add something to it?
Typical problems:
As already mentioned, environment is set up differently. Make sure $PATH iscorrect.
Does your program try to execute in a directory which becomes unmounted? If so, cd to / first.
Access restrictions to files and directories.
Process doesn't detach from stdin/stdout/stderr.
The process runs in foreground instead of background.
Parent process receives a terminating signal such as SIGTERM which kills your process as well. Try ignoring this (and some others) signals by using nohup or sigset/sigignore.
Debugging hint: Let the server start by appending current time to the end of an already existing file in a directory which is guaranteed to be writable. Make sure you flush (and close) the file pointer immediately. Then at least you can see whether it was started at all or not.
We are finishing development of a project, the client is already using it but occasionally some errors occur - crashing the server.
I know I could register a service as 'upstart' script on linux, in order to have my node service restart when it crashes.
But our server is running other stuff, so we can't restart it.
Well, actually, while writing, I realize I have two questions then:
Will 'upstart' work without having to reboot? Something is just whispering yes to me :)
If not, what other option would I have to 'respawn' my node server when it crashes?
Yes, upstart will restart your process without a reboot.
Also, you should look into forever.
PM2 is a Production process manager for Node.js app.
If your focus for automatic restart is an always running application, I suggest to use a process manager. Process manager, in general, handles the node process(es if cluster enabled), and is responsible for the process/es execution. PM leans on the operative system: your node app and the OS are not so strinctly chained because the pm is in the middle.Final trick: put the process manager on upstart. Here is a complete performance improvement path to follow.
Using a shared server and not having root privileges, I can't download or install any of the previously mentioned libraries. What I am able to do is use a simple infinite bash loop to solve my issue. First, I created the file ./startup.sh in the base directory ($ vim startup.sh):
#!/bin/bash
while:
do
node ./dist/sophisticatedPrimate/server/main.js
done
Then I run it with:
$ bash startup.sh
and it works fine. There is a downside to this, which is that is doesn't have a graceful way to end the loop (at least not once I exit the server). What I ended up doing is simply finding the process with:
$ ps aux | grep startup.sh
Then killing it with
$ kill <process id>
example
$ kill 555555
I have just gotten a VPS to bring my first node.js project online, but I am wondering where do I place the node files like app.js if I want it to be accessible at http://www.mywebsite.com:3000?
Right now, to host a website, I am using WHM to create a cPanel account, which creates /home/cpanelusername and my HTML/PHP files all go into /home/cpanelusername/public_html. Where does node.js files go to? Or did I get this step wrong as well?
On my Mac where I developed the node app, I simply cd into the directory containing the node file and run node app.js
You have to execute app.js file using the node binary, just like you do in local development. That means that you should probably make that execution a service call, the details of which depend on your linux distro. If it's not a service call, then executing it in ssh will mean that the app stops working once you log out of ssh.
For example, in Ubuntu server (which I use) I have an Upstart script which automatically runs my node.js app automatically on system start and log to /var/log. An example of the file, named /etc/init/myapp.js.conf is:
description "myapp server"
author "Me"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
script
# We found $HOME is needed. Without it we ran into problems
export HOME="/root"
exec node /home/me/myapp/myapp.js 2>&1 >> /var/log/myapp.log
end script
Replace names, etc. as necessary.
Edit to add: You can then start and stop your service by running:
sudo start myapp.js or sudo stop myapp.js