Error setting sh script at startup - Intel Galileo Gen 1 - node.js

I have running a node webserver on Intel Galileo Gen 1 and a normal arduino sketch which saves data from UDP messages.
I've tried to use the call system to set the timezone, date and start the webserver, but doesn't work very well. Then I created a sh script with the same commands, and created the corresponding links to the file in "/etc/init.d" using "update-rc.d startServer.sh defaults".
export TZ=CET-1CEST,M3.5.0,M10.5.0/3
rdate 132.163.4.101 && hwclock --systohc
node /media/mmcb1k0p1/Server/server2.js
It works, but Galileo does not start ssh, since I cannot connect anymore if it's running + sometimes the arduino sketch is not running. It seems like Galileo is currently executing the webserver and waits for it to finish to execute the rest, like an active process, instead of working in background.
Any help?

Run the node server as a background process:
node /media/mmcb1k0p1/Server/server2.js &
Note the & at the end.

Related

Start node app through ssh, stays running?

I have a raspberry PI on which I run a node server. To start and control the terminal on which the server runs I use desktop remote to remote control the raspberry. Now this method is really slow so I was wondering, since I only need a command line anyway if I couldn't just connect to my raspberry pi using ssh for example.
My question now, would be if I do so, can I navigate to my node folder, run my node file and then close the ssh connection? Will my Node server keep running and if so how would I access the terminal with the node session after closing the connection?
The easiest way to do this is something like:
nohup node myapp.js &
This will make the app run in the background, and nohup prevents it from stopping when the connection closes.
This is a cheap and quick way to do this. A more appropriate way might be one of the following:
Using something like docker to manage running applications.
Using something like supervisord to do the same thing.
Writing scripts for initd and turn it into a real 'service'.
Changing the node application to fork & deamonize itself.

Does nohup work as long as the server is running?

I recently decided to migrate my programs from AWS to a local server (Raspberry Pi 3 B+ running Raspbian). I SSH into the now local server where I send the following command (and please correct me if I'm wrong).
nohup ./Eye.sh & nohup ./Hand.sh & nohup ./Foot.sh &
The program does nothing over the weekend (and it is current 2AM on Sunday here) and will not start performing its task until 9:30AM on Monday. I need to know that I will not, hopefully ever, have to SSH back into the server to re-execute the commands, as it is crucial that the programs are running at all times.
Do I have anything to worry about? Will the program run as long as there is power to the system?
Yes. As long as the server is running, you can close out of your SSH connection and your program will continue to run with the nohup command. If your server reboots, for whatever reason, you should consider using respawn.

Daemonize nodejs app on mediatek 7688

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.

In Linux, how to make a jar file a daemon and how to add websocket

actually I have 2 jars and 1 ini file. In terminal I can run this java application like that :
"/usr/java/jre1.8.0_121/bin/java" -cp "./eSign.jar:sqlite-jdbc-3.7.2.jar" ./esign.tubitak.applet.SignApplet /home/ugurcan/Desktop/Linux_sign_test/test.ini
And my gui based program works like this. Now; I need to make this program run without any terminal commands like an exe as a session(which runs in backgraound always). Additionally , I need to add a websocket for the server to program makes a connection between the server and the computer which runs the program.
Thank you for your thoughts !!!
To run a command in background you can use:
nohup COMMAND &. nohup - makes your program immune to terminal hangup, and & runs it in background.
Can't figure out what do you mean 'to add a websocket for the server to program makes a connection between the server and the computer which runs the program'. Websocket support should be added in your application. But without any further detail we can't help you.

Centos 7: Auto restart application if it stopped

I want to auto restart my application "Fiware IoT Agent" if it stopped, the problem is that it depends of Mongo Db Data Base and the Mosquitto broker. My OS is centOS 7
Here is the commands that I use to launch my three application in the following order:
*Mongo:
/usr/local/iot/mongodb-linux-x86_64-3.0.5/bin/mongod --dbpath /usr/local/iot/mongodb-linux-x86_64-3.0.5/data/db$
*Mosquitto broker
/usr/sbin/mosquitto -c /etc/iot/mosquitto.conf &
pid=$!
echo $pid > /var/run/iot/mosquitto.pid
Iot Agent:
than I start my application using this command
export LD_LIBRARY_PATH=/usr/local/iot/lib
/usr/local/iot/bin/iotagent -i 192.168.1.11 -p 80 -v DEBUG -d /usr/local/iot/lib -c /etc/iot/config.json
how can I start my application if it stopped known that it depends of the other two application? If for example Mongo DB stopped, I must be able to restart it and then to restart my application.
CentOS 7 uses systemd. You can create systemd service for each of your applications and specify dependencies between them. And specify "Restart=always" for service which need to be auto restarted.
You can create your own watch dog code. When you start your application get the pid of the process and the pid of mongo DB.
Every couple of second like 10 seconds check that the pid of both process still exist, or you can also make the programs touch a file every couple of seconds as well then check the file modification time to see if the programs are still alive.
If the program hasn't touched the file or if you go jus the pid route and the pid doesn't exist. Then the program has died.
Restart the program and get the new pid and go about again in a forever while loop.

Resources