not able to execute commands in parallel in shell script through terminal - node.js

I am trying to execute below command in parallel using &
ENV=prod npm run SettingsSuite -- --runid Prod_E2E_$BUILD_NUMBER --APPNAME Prod_E2E --squad ops --track coreServices; echo $? >> "$log_file" &
ENV=prod npm run InventorySuite -- --runid Prod_E2E_$BUILD_NUMBER --APPNAME Prod_E2E --squad ops --track coreServices; echo $? >> "$log_file" & wait
but everytime it runs in sequential. my requirement is I need to run multiple commands in parallel and store exit code of each command in a log file.

Because you have a semicolon in there, only the echo commands are run in the background. You need to use some grouping parentheses:
(ENV=prod npm run SettingsSuite -- --runid Prod_E2E_$BUILD_NUMBER --APPNAME Prod_E2E --squad ops --track coreServices; echo $? >> "$log_file") &
(ENV=prod npm run InventorySuite -- --runid Prod_E2E_$BUILD_NUMBER --APPNAME Prod_E2E --squad ops --track coreServices; echo $? >> "$log_file") &
wait

Related

Run script command on parallel

i’ve bash script which I need to run on it two command in parallel
For example I’m executing a command of npm install which takes some time (20 -50 secs)
and I run it on two different folders in sequence first npm install on books folder and the second
is for orders folder, is there a way to run both in parallel in shell script ?
For example assume the script is like following:
#!/usr/bin/env bash
dir=$(pwd)
cd $tmpDir/books/
npm install
grunt
npm prune production
cd $tmpDir/orders/
npm install
grunt
npm prune production
You could use & to run the process in the background, for example:
#!/bin/sh
cd $HOME/project/books/
npm install &
cd $HOME/project/orders/
npm install &
# if want to wait for the processes to finish
wait
To run and wait for nested/multiple processes you could use a subshell () for example:
#!/bin/sh
(sleep 10 && echo 10 && sleep 1 && echo 1) &
cd $HOME/project/books/
(npm install && grunt && npm prune production ) &
cd $HOME/project/orders/
(npm install && grunt && npm prune production ) &
# waiting ...
wait
In this case, notice the that the commands are within () and using && that means that only the right side will be evaluated if the left size succeeds (exit 0) so for the example:
(sleep 10 && echo 10 && sleep 1 && echo 1) &
It creates a subshell putting things between ()
runs sleep 10 and if succeeds && then runs echo 10, if succeeds && then run sleep 1 and if succeeds && then runs echo 1
run all this in the background by ending the command with &

Travis-ci doesn't quit after running bash script over ssh to start activator

We are trying to have travis continually deploy to our own server when our build is successful.
env:
global:
- ACTIVATOR_VERSION=1.3.7
- ACTIVATOR_ZIP_FILE=typesafe-activator-${ACTIVATOR_VERSION}-minimal.zip
- ACTIVATOR_ZIP_URL=http://downloads.typesafe.com/typesafe-activator/${ACTIVATOR_VERSION}/${ACTIVATOR_ZIP_FILE}
- ACTIVATOR_BIN=${TRAVIS_BUILD_DIR}/activator-${ACTIVATOR_VERSION}-minimal/activator
- "DEPLOY_USERNAME=#######"
- "DEPLOY_PASSWORD=########"
- "DEPLOY_HOST=########"
language: java
jdk:
- oraclejdk8
addons:
ssh_known_hosts:
- ########
apt:
packages:
- sshpass
install:
- wget $ACTIVATOR_ZIP_URL
- unzip -q $ACTIVATOR_ZIP_FILE
script:
- $ACTIVATOR_BIN test
after_success:
- sshpass -p $DEPLOY_PASSWORD ssh $DEPLOY_USERNAME#$DEPLOY_HOST -o stricthostkeychecking=no 'bash deploy.sh'
After our travis finishes without errors it runs an ssh script on our server to pull from our git, stop our running activator and start a new one.
The script:
#!/bin/bash
#Get the path of the local repository directory
set -o verbose
DIR="/home/ftpuser/eaglescience/"
TARGET="origin/develop"
SLEEP=1m
#echo "Go into directory " ${DIR}
cd ${DIR}
PID="`cat target/universal/stage/RUNNING_PID`"
#echo "Get the code from " ${TARGET}
git fetch --all
#echo "force checkout"
git checkout --force "${TARGET}"
#echo "Compiling activator"
activator clean stage
#echo "Running activator"
kill -15 ${PID}
target/universal/stage/bin/eaglescience -Dapplication.secret=############### &
#echo "Running..."
sleep ${SLEEP}
exit 0
The problem here is that Travis-ci does not exit the bash script after it runs (even with the exit 0). This means that Travis-CI will keep waiting for a response until it times out and erros our build
The response we got after a while is the following:
[success] Total time: 33 s, completed Mar 9, 2016 11:19:20 AM
#echo "Running activator"
kill -15 ${PID}
target/universal/stage/bin/eaglescience -Dapplication.secret=########### &
#echo "Running..."
sleep ${SLEEP}
[warn] - application - system properties: application.secret is deprecated, use play.crypto.secret instead
[info] - play.api.Play - Application started (Prod)
[info] - play.core.server.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
exit 0
No output has been received in the last 10 minutes, this potentially indicates a stalled build or something wrong with the build itself.
The build has been terminated
We have tried a lot of different things, we have tried to run the ssh bash command silent. But then travis-ci terminates the connection almost instantly and the command wil not run. We also tried to add && exit 0 but then the server still keeps waiting on response.
Try using nohup the shell file with output to /dev/null 2>&1 & eg : nohup filename.sh > /dev/null 2>&1 &

Ubuntu run script as non-root user

I've an init script (/etc/init.d) that should run a my executable jar file as a serviceat boot. I need that this script is runned by a specified user.
With su & sudo is possibile but it split the process and I don't like this.
There is another way to run this script as limited user?
This is the relevant part of my init script:
#!/bin/bash
APP_NAME="myapp"
APP_HOME=/home/user1/jetty
JAVA_HOME=/opt/local/java/latest
echo "Service $APP_NAME - [$1]"
echo "JAVA_HOME -> $JAVA_HOME"
echo "APP_HOME -> $APP_HOME"
echo "APP_NAME -> $APP_NAME"
function start {
if pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
then
echo "Service [$APP_NAME] is already running. Ignoring startup request."
exit 1
fi
echo "Starting application..."
cd $APP_HOME
nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME.jar\
< /dev/null > $APP_HOME/logs/app.log 2>&1 &
}
On Ubuntu you should use the program start-stop-daemon for this. It has options for launching daemons as different users, managing pid files, changing the working directory, and pretty much anything else that is usually needed by init scripts.

Linux change to another /dev/ttyX and run program there

Is it possible to run a script on /dev/tty1 and spawn a program to run on /dev/tty2 and wait for it to complete and run the other commands in the script? For instance:
echo "Hello, this is from terminal 1"
chvt 2
sh myprogram.sh (I want it to run on tty2, but it runs on tty1)
chvt 1
myprogram.sh:
echo "Hello' this is from terminal 2, please type your input:"
read A
exit A
Yes, you can do this using openvt.
openvt -c 2 sh myprogram.sh

Adding a service startup script for Amazon linux AMI

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:
chkconfig --add axisservice
But when I launch a new instance from my image, the axis2server is not getting started.
I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?
#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###
case "$1" in
start)
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
;;
stop)
echo -n "Stopping axisservice"
echo "."
;;
*)
echo "Usage: /sbin/service axisservice {start|stop}"
exit 1
esac
exit 0
I went through https://help.ubuntu.com/community/CloudInit as well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.
$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx
This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.
Thanks,
H.
I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.
So at first, I'd adjust the start part of your script (current):
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."
Edited:
echo -n "Starting axisservice"
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."
So what did I do?
removed & so script waits for your shell script (axis2server.sh) to complete
checked the return status ($?) of your shell script
Further debugging:
Add set -x to your scripts to enable tracing and log both stderr and stdout.
Questions:
Are you are aware that stop (in your service script) doesn't do anything?
touch ~/temp.txt is that supposed to create /root/temp.txt? (I'm guessing root runs this script.)
If none of my suggestions work, can you share axis2server.sh and paste stderr and stdout?

Resources