Run node.js twice using sh script - node.js

I created a bash script named runall.sh with this content:
#!/bin/bash
node ../app1/app.js && node ../app/app.js
I chmod +x ./runall.sh and can run it.
But only ../app1/app.js gets started.
If I run node ../app2/app.js manually it works.

The && operator in shell scripting tries to do a short-circuit AND comparison. That means that it checks to see what the result of the first command is before the second command starts. So it will wait until the first call to node terminates before calling the second one.

You probably want to background with &?
#!/bin/bash
node ../app1/app.js &; node ../app/app.js &

As #daniel said before, the command after the '&&' operator will only be called, after the first command returned from execution.
So you could send a command to the background with the '&' operator or you could probably use some sort of node.js process manager like forever.
That way your runall.sh could look like
#! /bin/bash
forever start ../app1/app.js
forever start ../app2/app.js

Related

How do I execute commands serially inside a shell script?

I want to write a shell script to execute some commands in sequence (the next one starts only after the previous has finished and so on). I've never written a bash script before and I couldn't find the appropriate sequence.
I know that in the terminal you do things like yarn this && yarn that && yarn other and it works but I don't know the equivalent inside a shell script.
#!/bin/sh
direnv allow
# now wait for direnv allow to finish
direnv reload
# now wait for direnv reload to finish
yarn start:server
The shell will execute each command one after another serially as written. Servers often daemonize, i.e. fork() and have the parent return. They usually have -f flag to suppress that behavior, precisely because you may want that serial behavior.

Need to change the directory inside the shell script

I need to change the directory inside the shellscript.i have tried below
#!/bin/bash
sudo -u rv bash
cd /opt/test
source /opt/rv/van.env
./rv.sh |grep "STATUS"
When I tried it didn't cd to that path and not executed th rv.sh.guide me with some ideas.
Fyi rv.sh path /opt/test/
The following line:
sudo -u rv bash
Will launch a bash console as the user rv and then your entire script will halt until the program (i.e.: bash) ends.
Only after bash ends, the program will continue execution and run the last two lines.
The last two lines are running as part of the first script, therefor, they run as your user.
It seems like you're expecting sudo to execute the rest of the script in the users context but it is not interactive. It will invoke a separate bash instance and, once exited, resume the rest of the script afterwards.
You'll have to put together a separate script and run that with sudo. Running sudo inside of scripts is rarely a good idea.
S.a. https://askubuntu.com/questions/425754/how-do-i-run-a-sudo-command-inside-a-script

Can I prevent a subsequent chained bashed command from running?

I want to prevent a bash command from executing that has been chained using ; from running while the previous command is still running.
e.g. I write and submit command a; command b, but while command a is running I change my mind and want to prevent command b from running.
I cannot use kill because the subsequent command is not actually executing. Does bash have a queue of commands that can be manipulated?
To clarify, I am sure it is possible to make a new script or something that would allow me to create a queue, but that is not what this question is about. I specifically want to know if bash can prevent commands after a semicolon from running after I've 'submitted' them.
Consider these two scripts:
runner.sh
#!/bin/bash
while true
do
next_command=$(head -1 next_commands.list)
$next_command
sleep 60 #added to simulate processing time
done
next_commands.list
id
ls
echo hello
You can modify the content of the next_commands.list file to create a type of queue of which commands should be executed next.

Execute a command in a new terminal window

I'm on ubuntu 17.04, and I'm trying to execute some commands in sequence, so I have written this shell script:
#!
sudo java -jar ~/Desktop/PlugtestServer.jar
sudo /opt/lampp/lampp start
sudo node httpServer.js
The problem is that after the first command, it execute PlugtestServer and then stop on it, because it is a server and continue to execute it. There is a command in order to automatically open a new terminal and execute PlutestServer in it?
There is way to open a new terminal window and execute command in it using gnome-terminal
The sample format for command is:
gnome-terminal -e "command you want to execute"
gnome-terminal -e "./your-script.sh arg1 arg2"
Hope that helps!!
Your script stays on the first command showing output, you can make the shell move on by adding "&" to the end of your lines. However this might still not do what you want, if you want PlugTestServer to remain running when you log out. For that you should include "nohup" which will keep the command running while piping output to a file.
So, an example:
#!/bin/sh
nohup java -jar ~/Desktop/PlugtestServer.jar > plugtest.out & #Pipes output to plugtest.out, use /dev/null if you don't care about output.
/opt/lampp/lampp start
node httpServer.js
Notice I removed sudo from the script. It's generally better to invoke the script with "sudo" unless you have specific reason to, at the very least it simplifies the commands.
I'm not sure if your second and third command "fork" or "block", so add "nohup" and "&" if you need to.

Can't get BASH script to wait for PID

I am building a script to make my life easier when setting up servers.
I am having a issue with this line:
# Code to MV/CP/CHOWN files (working as intended)
sudo su $INSTALL_USER -c \
"sh $SOFTWARE_DIR/soa/Disk1/runInstaller \
-silent -response $REPONSE_LOC/response_wls.rsp \
-invPtrLoc $ORA_LOC/oraInsta.loc \
-jreLoc /usr/java/latest" >&3
SOA_PID = pgrep java
wait $SOA_PID
# Code below this which requires this be completed before execution.
I am trying to get my script to wait for the process to complete before it continues on.
The script executes, but instead of waiting, it continues on, and the execution of the installer runs after the script finishes. I have other installer pieces that need this part installed before they start their own process, hence the wait.
I've tried using $! etc, but since this piece get executing by a separate user, I don't know if that would work.
Thanks for any assistance.
The command SOA_PID = pgrep java should result in an error.
Try to capture the PID like this:
SOA_PID=$( pgrep java ) || exit
The || exit forces an exit if pgrep does not return a value,
preventing nonsense happening further on.
An alternative would be to rely on wait to return immediately,
but it's better to be explicit.
When using this in a function you'd use || return instead, depending
on circumstances.

Resources