I have a node server running within a vagrant. The script that starts node 'start.sh' can pick up a debug flag from a file, which I named debug.mode
On the local side I have a script 'startdebug.sh' which logs into vagrant over ssh, writes to the debug.mode file, restarts the script, waits till that is done, then tunnels 5858.
If I run the start.sh file with debug.mode containing '--debug' node opens 5858 and the port is available (I'm checking within vagrant using telnet).
If I do the same using startdebug.sh node says it's opened the debugging port, however the 5858 port it unavailable when I try telnet'ing within the VM.
Any idea? :)
startdebug.sh
/usr/bin/vagrant ssh-config > /tmp/vagrant-ssh-config
ssh -F /tmp/vagrant-ssh-config nodejs "cd /var/www/sportsbook-api && echo $mode > debug.mode && export TERM=linux && sudo ./scripts/restart.sh"
sleep 2.5s
ssh -N -F /tmp/vagrant-ssh-config -L 5858:127.0.0.1:5858 nodejs &
start.sh
if [ -e "debug.mode" ]; then
debug=$(cat "debug.mode")
echo "\nNode $debug mode activated."
fi
nohup node src/main/apps/api & echo $! > run.pid &
Related
I can run a local script on a remote server using the -s option, like so:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "bash -s" < myLocalScript.sh;
And I can run a remote script using nohup, like so:
# run a script on server with nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash myRemoteScript.sh > results.out 2>&1 &"
But can I run my local script with nohup on the remote server? I expect the script to take many hours to complete so I need something like nohup. I know I can copy the script to the server and execute it but then I have to make sure I delete it once the script is complete, would rather not have to do that if possible.
I've tried the following but it's not working:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash -s > results.out 2>&1 &" < myLocalScript.sh;
You shouldn't have to do anything special - Once you kick off a script on another machine, it should finish running even if you terminate the connection:
For example
ssh $SSH_USER#$SSH_HOST "bash -s > results.out 2>&1" < myLocalScript.sh &
# Please wait a few seconds for the connection to be established
kill $! # Optional: Kill the last process
If you want to test it, try a simple script like this
# myLocalScript.sh
echo 'File created - sleeping'
sleep 30
echo 'Finally done!'
The results.out file should immediately be created on the other machine with "File created - sleeping" in it. You can actually kill the local ssh process with kill <your_pid>, and it will still keep running on the other machine, and after 30 seconds, print "Finally done!" into the file, and exit.
I am using NodeJs in Raspberrypi on startup (rc.local). i have some issue my ftp client will not able to download data, if the NodeJs run on startup.
but if i run through command line at my own, it will work great and can able to download files through FTP client.
is there any way to see console logs while using nodejs on startup.
My project is already very delayed, please help.
Thanks in advance.
rc.local
`#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#sudo neo4j start &
#sudo su pi -c 'sudo neo4j start < /dev/null &'
#sudo su pi -c 'sudo /etc/init.d/mysql start < /dev/null &'
sudo su pi -c 'node /home/pi/Desktop/RaspberyryPiLearning/bin/www <
/home/pi/Desktop/error.log &'
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
exit 0
You should specify the whole path of node binary file and of your node app, for example : sudo su pi -c '/usr/bin/node /home/pi/node_app/index.js.
You can type which node to get the path of node binary
But, a better option would be to use PM2. It will handle the startup loading of your node apps, handle/display their logs, restart them in case of error. You can monitor them too, stop, restart...
I need to check in ubuntu terminal if process node is running so that i can use two different commands then if is running or not.
i try copy paste this code in ubuntu terminal:
if pgrep -x "node" > /dev/null then echo "Running" else echo "Stopped" fi
in terminal i get only > and no output...and i was expacting to output: Running or Stopped
But it stucks on >
How can i check in terminal without using external sh file to get information if node process is running or not? I im executing this in node ssh command on another server so i try copy paste this code on my server local but is not go.
You've got three missing semicolons:
if pgrep -x "node" > /dev/null; then echo "Running"; else echo "Stopped"; fi
I'm trying to launch an express app when my ec2 machine starts. I've a startup script that is:
#!/bin/bash
echo "will reroute traffic" >> /home/ubuntu/log.logs
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
echo "will kill node" >> /home/ubuntu/log.logs
if pgrep node &> /dev/null ; then killall -KILL node ; fi
if pgrep nodejs &> /dev/null ; then killall -KILL nodejs ; fi
echo "will run node server" >> /home/ubuntu/log.logs
cd server && npm install && npm run build && npm run start </dev/null &>/dev/null &
echo "has run node server" >> /home/ubuntu/log.logs
If I launch it from the console, it starts the server, exits and the server runs fine.
To launch it, I've added those lines to /etc/rc.local:
rm -f /home/ubuntu/log.logs
echo "will run" >> /home/ubuntu/log.logs
/bin/bash /home/ubuntu/startup.sh
echo "has run" >> /home/ubuntu/log.logs
After rebooting, the server is not responding and it looks like it has not started (the server logs ticks when running that are not there)
the output in log.logs looks fine:
will run
will will reroute traffic
will kill node
will run node server
has run node server
has run
so everything seems to have been executed, but the node app is not running, which I confirmed by running top | grep node that returns nothing.
I found that the cheap (or free) AWS VMs got CPU/network throttled causing npm installs etc. to fail. Maybe use a VPS that is a better value or try yarn. Also make it log the npm stuff to a file instead of dev/null.
It turned out that I installed npm and node through nvm, and that nvm adds a script to .bashrc that will load those libraries. To start my script on reboot, I was using cron that is not sourcing .bashrc. Additionally, the default .bashrc on AWS EC2 ubuntu instances starts with a check on wether it's been run from a terminal or not, and escape it it's not been run from a terminal. So sourcing it from cron has no effect.
I didn't see that since the failing line
cd server && npm install && npm run build && npm run start
was not logging anything
I ended up manually sourcing the path to npm and node
I have a shell script that is used to setup the network settings on a linux machine in bash, it will mainly be used over SSH. Here are the last few lines of the script.
service network stop
rm -rf $NETWORKFILE
touch $NETWORKFILE
echo NETWORKING=yes > $NETWORKFILE
echo HOSTNAME=$HOSTNAME >> $NETWORKFILE
mv $ETHFILE /etc/sysconfig/network-scripts/ifcfg-eth0
service network start
As you can see, to apply the network settings it has to stop then start the network and apply the settings while the network is down. This would then cause the SSH session to be disconnected on the first line of the code I have shown and the script to thus stop and the settings to not be applied. How can I have the shell script run these last few lines after the SSH session is disconnected that started the shell script? Also, it needs to be done in the code and not through a screen or nohup command when starting the script.
Try completely disconnecting the script from the terminal, by redirecting all standard streams and putting it in background:
nohup script < /dev/null > script.log 2>&1 &
Also you can put "sleep 2" as the first line of the script, so that after putting the script in background, you can quickly disconnect cleanly, before the server closes it forcibly. This is just for convenience.
Maybe if you get your script PID, and then disown the process after stopping the network, your script will continue running after the ssh session is disconnected.
./script &
pid=$$
disown -h $pid
service network stop
rm -rf $NETWORKFILE
touch $NETWORKFILE
echo NETWORKING=yes > $NETWORKFILE
echo HOSTNAME=$HOSTNAME >> $NETWORKFILE
mv $ETHFILE /etc/sysconfig/network-scripts/ifcfg-eth0
service network start