Monit losing bash variable? - bash

Trying to get monit to monitor a custom daemon we wrote, and it's just not working with the bash stop/start script. If I run the stop/start script by hand from the command line it's working 100% perfectly, every single time. If it get's executed through monit, the variables is empty. Extract from the script where I am having problems:
GETPID=$(ps aux | grep unicorn | grep master | cut -d" " -f7)
echo "getPID : $GETPID"
echo $GETPID > $PIDFILE
The $GETPID variable is blank when this gets executed with monit. By hand it works perfectly.
Anyone have any ideas?

In general it is not a very good idea to parse the output of ps or ls.
You can write a simple pgrep using find on the proc filesystem:
# find /proc/ -maxdepth 2 -type l -name exe -lname '/bin/bash' -printf '%h\n' 2>/dev/null | sed 's/.*\///'
3580
3595
9504
9869
10054
10156
10193
# pgrep bash
3580
3595
9504
9869
10054
10156
10193

Thanks for the help. Problem was path to unicorn in rvm install.

Related

Different output when running command in bash script vs terminal

When I run the following code in a bash script I receive an output of 2
#!/bin/bash
HIPPO=$(ps -a | grep hippo | wc -l)
echo "$HIPPO"
However when I run the command ps -a | grep hippo | wc -l straight from a command prompt I get an output of 0
Reading the documentation on ps particularly the -a flag, I'm not understanding why the output is different.
How is called your script? If you named it with hippo, it will count in your ps call.
https://superuser.com/questions/935374/difference-between-and-in-shell-script
When you do the command substitution, the command gets runs once according to the above. So I am assuming, the echo is picking a zombie process that ran that command.

How do I make a stop after a run?

If I do a:
npm run script
Can I stop it with a stop?
npm stop script
Why I tried it and it does not work.
I know that with the combination of "Ctrl + c" I kill it, but I want to do it by command.
Try something like that:
ps -ef | grep script | awk '{print $2}' | head -n1 | xargs kill -9
This command should find first process named script on the list of all unix processes created by all users and kill it by with using its PID.

bash: test if a process is running with a certain option

I would to know if it's possible to detect if amarok is playing music or not, in bash.
I have tested
#! /bin/bash
if [ "$(pidof amarok --play)" ]
then
echo amarok is playing music!
else
echo amarok is not playing!
fi
But I have this error:
pidof: invalid options on command line!
pidof thinks that --play is its option, and that is of course an error.
It would be better to check if --play is inside the commandline that was used to start amarok:
grep -- --play /proc/$(pidof amarok)/cmdline
Another option is to grep output of ps aux:
ps aux | grep [a]marok.*--play
In the both cases we suppose that there only one amarok running in the system.
Update 1.
To check if any music is playing at the moment,
you can so:
grep RUNNING /proc/asound/card*/pcm*/sub*/status

Using bash and watch to monitor qemu-kvm

I'm trying to monitor some qemu-kvm processes using a bash script and watch to show several details like memory/cpu use, resources, ports,etc. Everything is going great until I try to get the IMG file that qemu-kvm is using.
Initially I thought I could get that information from the running process cmd:
ps -p PID -o cmd | tail -1
qemu-kvm -name TEST -drive file=/img/test.qcow2,if=ide,media=disk,cache=none -daemonize
So after plenty of tests I have wrote this little script just to synthesise the problem:
#!/bin/bash
lsof -p PID | grep '/img/' | awk {'print $9'}
The output of this script looks totally fine when I execute it from the command line:
bash testscript
/img/test.qcow2
The problem comes when I try to run the script along with watch:
watch -n1 "bash test-script" or watch -n1 ./test-script
The output is just empty...
Why don't I get any results? I'd be really glad if somebody can help me understanding this.
EDIT: I have found an alternative solution. I am now getting the information from parsing the procfs with some arrays to find the IMG info:
OIFS=$IFS;
IFS='-';
Array=($(cat /proc/PID/cmdline))
for ((i=0; i<${#Array[#]}; ++i))
do
if [[ "${Array[$i]}" == *drive* ]]; then
image=${Array[$i]}
echo $image
fi
done
IFS=$OIFS;
This works fine combined with watch, but I would still like to know what's the problem with the other method. Is lsof limited somehow??
I tried the same process as you; the only difference in my case was the need for sudo. With that it worked. Same issue?
#!/bin/sh
sudo lsof | grep .B.disk.img
watch -n1 sh test

Bash: How can I make a sync call until finding a match pattern in log file

I have a start server script
startserver.sh
It will run as background task startserver.sh &
the script need to run for sometime until it can really run in running state.
The running state could write into log file server.log when it is ready.
So I need to know when the server is really run by executing a bash cmd. If not, I need to wait until the Running state is shown in the server.log.
Can i achieve this in bash?
try something like this
mkfifo my_fifo
tail -f server.log >my_fifo &
tail_pid=$!
perl -ne '/pattern/&&exit' <my_fifo
kill $tail_pid
rm my_fifo
EDIT : perl command can be replaced with
grep -l 'pattern' <my_fifo >/dev/null
or if grep support this option
grep -q 'pattern' <my_fifo

Resources