launch multiple processes in bash with for loop - bash

On ubuntu, I'm trying to launch multiple instances of filebeat on the same host using a for loop but I encounter a weird behavior
The command I'm trying to launch
for i in `seq 1 2` ; do /etc/filebeat-test/filebeat-$i/filebeat & done
And the error message I get and then when I exit the command:
Exiting: error loading config file: stat filebeat.yml: no such file or directory
^C
[1]- Termine 1 /etc/filebeat-test/filebeat-$i/filebeat
[2]+ Termine 1 /etc/filebeat-test/filebeat-$i/filebeat
It looks like $i is not interpreted..
The weird thing is that I had this command working a couple of times, but 2 seconds later it won't work. I also tried to launch it from a file and to replace $i with "$i" with the same result.
Any idea what is going on?
Thanks for your help

Does this work for you:
# set -x # uncomment this to see what actually happens while the lopp cycles
for i in `seq 1 2` ; do (/etc/filebeat-test/filebeat-$i/filebeat &); done

Related

Bash script for retrieving server stats

#!/bin/bash
printf "Date\tMemory"
end=$((SECONDS+3600))
while[$SECONDS -lt $end];do
Memory=$(free -m | awk 'NR==2 {printf "%.2f%%\t",$3*200/$2')
Date=$(date"+%Y-%m-%d %H:%M:%S")
echo "$Date $Memory"
sleep 15
done
Q1: I have written above code and saved in perfMemstats.sh file and executed it using below command:
bash perfMemstats.sh >> perfStats.dat
Problem :
If I execute this .sh file its executed only for 10 mins. I want to run this file in background so that I can monitor other stats. Please let me know what mistake I'm doing. I even tried giving '&' it running in background but only for 10 min.
If I run this command without saving it in an output file (bash perfMemstats.sh) it is executing for 1 hour.

Bash script not exiting once a process is running

How should I modify my bash script logic so it exits the while loop and exits the script itself once a process named custom_app is running on my local Ubuntu 18.04? I've tried using break and exit inside an if statement with no luck.
Once custom app is running from say...1st attempt then I quit the app, run_custom_app.sh lingers in the background and resumes retrying 2nd, 3rd, 4th, 5th time. It should be doing nothing at this point since app already ran successfully and user intentionally quit.
Below is run_custom_app.sh used to run my custom app triggered from a website button click.
Script logic
Check if custom_app process is running already. If so, don't run the commands in the while code block. Do nothing. Exit run_custom_app.sh.
While custom_app process is NOT running, retry up to 5 times.
Once custom_app process is running, stop while loop and exit run_custom_app.sh as well.
In cases where 5 run retries have been attempted but custom_app process is still not running, display a message to the user.
#!/bin/sh
RETRYCOUNT=0
PROCESS_RUNNING=`ps cax | grep custom_app`
# Try to connect until process is running. Retry up to 5 times. Wait 10 secs between each retry.
while [ ! "$PROCESS_RUNNING" ] && [ "$RETRYCOUNT" -le 5 ]; do
RETRYCOUNT="`expr $RETRYCOUNT + 1`"
commands
sleep 10
PROCESS_RUNNING=`ps cax | grep custom_app`
if [ "$PROCESS_RUNNING" ]; then
break
fi
done
# Display an error message if not connected after 5 connection attempts
if [ ! "$PROCESS_RUNNING" ]; then
echo "Failed to connect, please try again in about 2 minutes" # I need to modify this later so it opens a Terminal window displaying the echo statement, not yet sure how.
fi
I have tested this code on VirtualBox as a replacement for your custom_app and the previous post was using an until loop and pgrep instead of ps. As suggested by DavidC.Rankin pidof is more correct but if you want to use ps then I suggest to use ps -C custom_app -o pid=
#!/bin/sh
retrycount=0
until my_app_pid=$(ps -C VirtualBox -o pid=); do ##: save the output of ps in a variable so we can check/test it for later.
echo commands ##: Just echoed the command here not sure which commands you are using/running.
if [ "$retrycount" -eq 4 ]; then ##: We started at 0 so the fifth count is 4
break ##: exit the loop
fi
sleep 10
retrycount=$((retrycount+1)) ##: increment by one using shell syntax without expr
done
if [ -n "$my_app_pid" ]; then ##: if $my_app_pid is not empty
echo "app is running"
else
echo "Failed to connect, please try again in about 2 minutes" >&2 ##: print the message to stderr
exit 1 ##: exit with a failure which is not 0
fi
The my_app_pid=$(ps -C VirtualBox -o pid=) variable assignment has a useful exit status so we can use it.
Basically the until loop is just the opposite of the while loop.

getting scripts to run exclusively

I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do what I need, but it doesn't seem to be working the way I expect. I have the following script:
#!/bin/bash
inst=$1
lock=/nobackup/julvr/locks/_tst.lk
exec 200>$lock
flock -x -w30 200 || { echo "$inst: failed flock" && exit 1; }
echo "$inst:got lock"
for i in {1..2}; do
echo "$inst: $i"
sleep 1
done
echo "$inst:done script";
And then I run
> flocktest.sh test1 & flocktest.sh test2
[1] 25213
test1:got lock
test1: 1
test2:got lock
test2: 1
test1: 2
test2: 2
test1:done script
test2:done script
[1]+ Done flocktest.sh test1
It seems both instances of flocktest are running in parallel... When does flock release its lock? How to make it keep the lock until the script is complete?
(An aside, if I do flock -x -w 20 200, then it complains flock: 20: fcntl: Bad file descriptor..., which seems odd, as the man page seems to imply I can add a --w timeout parameter before the lockfile...)
flock seems to me very complicated.
Perhaps you can try this way.
cat script_unique.sh
while test -n "$run_sh"
do
sleep 2
done
export run_sh="run_sh"
sleep 2
echo "$run_sh"
sleep 4
echo "$0 $1"
run_sh=""
Ok, I found my bug -- I was using a really old version of flock which had a different interface than the one described in the man page. I updated to a new version of flock, and it worked.

Bash exec in infinite while loop doesn't work

#!/bin/bash
while :
do
echo twerkin
exec free -h > /ver/www/raspberry/load.txt
exec /opt/vc/bin/vcgencmd measure_temp > /var/www/raspberry/heat.txt
done
This is what I made, I am going to read it on my website, but thats not the problem. The problem is that it gives me this error:
pi#raspberrypi ~ $ sh showinfo.sh
showinfo.sh: 7: showinfo.sh: Syntax error: "done" unexpected (expecting "do")
I'm running this on my raspberry pi with Raspbian (Debian Wheezy)
You don't seem to understand what the exec keyword does. It replaces your current script (basically, thewhile loop) with the free command.
To run a command, simply specify it.
#!/bin/bash
while : ; do
free -h
/opt/vc/bin/vcgencmd measure_temp
done
(I omitted the redirections because you were overwriting the old results on each iteration. What do you actually want?)
try to change few things in your script
#!/bin/bash
while : ;
do
echo twerkin
#### commands to execute are not need to be prefixed with exec
#### + concatenate output with >>
free -h >> /ver/www/raspberry/load.txt
#### same here
/opt/vc/bin/vcgencmd measure_temp >> /var/www/raspberry/heat.txt
#### you probably would want to add a delay here, because in other way
#### the script will be executed probably way too fast
# sleep 0.01
done

Bash command substitution freeze

Currently I encountered the following situation when bash script freeze on
PID=`cat test.pid`
After the analysis it was found that even this commands freeze
TEST=$(echo 1)
TEST=`echo 1`
Using set -x in bash script, I can see the following output
+ echo 1
1
++ echo 1
for script
#!/bin/bash
set -x
echo 1
TEST=$(echo 1)
set +x
This script is called from Qt process and everything have worked lately.
When I call this script manually from bash it also works, but when I do it from process it fails.
Currently I'm looking for possible reasons of such freezes, and I've no more ideas.
When I printed environments they matched, but I also can't prinenv inside `` as it freezes also.

Resources