How to add a variable inside the command in shell script? - bash

I took the process ID and stored it in a variable. And I navigate to another directory. Then I should pass that process ID to get results in another command. how to achieve this? I tried the below with no luck... Please help me
pid=$(ps -ef | grep j[a]va | grep wmip | awk '{ print $2 }' | sed -n 1p)
echo $pid
#query=$(echo "cd /wmip/webMethods7/jvm/linux150/bin/ ; ./jstat -gcutil $pid")
#echo $query
result=$(cd /wmip/webMethods7/jvm/linux150/bin/ ; ./jstat -gcutil $pid)
echo $result

You should use the full path of ./jstat

Could this work? 'pid' could be a reserved word, change it
jpid=$(pgrep -f 'java .*wmip')
result=$(/wmip/webMethods7/jvm/linux150/bin/jstat -gcutil $jpid)

Related

grep a variable in if condition

So currently i need to grep a "." from a variable and if it is successful than do some work. So currently i am using this code
if echo "$arg3" | grep -iv '\.' ;
then
FS="'$arg4.$arg3'"
fi
Here i will pass the value in $arg3 like test and look for the "." in the variable and if it is not there than add a substring $arg4 and store it to the another variable.
SO my problemis, it is working but when i run my script it will print the $arg3 value as well.
Thanks in regards,
Vivek
You can redirect the grep output to null as below:
if echo "$arg3" | grep -iv '\.' > /dev/null 2>&1

ps working on command line but not in script

I have a simple script that checks the process id of some process using ps. When I run it directly on command line, it works fine but does not when I run it in a script. What am I doing wrong?
This works fine:
ps auwx | grep elasticsearch | grep -v grep | grep user | awk '{print $2}' | tail -1
In script, it does not:
#!/bin/bash
#Setting ES Heap to 50GB
ES_HEAP_SIZE="50g"
#Finding dump file to be deleted
FILE_ID=$(ps auwx | grep elasticsearch | grep -v grep | grep user | awk '{print $2}' | tail -1)
FILE_NAME="java_pid$FILE_ID.hprof"
echo "Elasticsearch pid: $FILE_ID"
echo "Dump file name if it exists: $FILE_NAME. Checking now."
if [ -s $FILE_NAME ]
then
rm $FILE_NAME
kill -9 $FILE_ID
#Starting elasticsearch daemon
/data/elasticsearch-1.4.4/bin/elasticsearch -d
else
echo "All good. Dump file $FILE_NAME does not exist."
fi
Personal pet peeve: why do you have six executions in your pipeline when two will handle everything you need?
ps auwx | awk '/elasticsearch/ && /user/ { x=$2 } END{ print x; }'
As an aside, you wanted PID? Because it looks like you're reading PPID.
Hope that helps.

Bash Script can run php script manually but cannot work in Cron

I have a bash script like this:
#!/bin/bash
log_file=/home/michael/bash/test.log
checkalive=checkalive.php
#declare
needRestart=0
#Check checkalive.php
is_checkalive=`ps aux | grep -v grep| grep -v "$0" | grep $checkalive| wc -l | awk '{print $1}'`
if [ $is_checkalive != "0" ] ;
then
checkaliveId=$(ps -ef | grep $checkalive | grep -v 'grep' | awk '{ printf $2 }')
echo "Service $checkalive is running. $checkaliveId"
else
echo "$checkalive OFF"
needRestart=1
fi
#NEED needRestart
if [ $needRestart == "1" ];
then
#START SERVICE
echo "Restarting services..."
/usr/bin/php5.6 /home/michael/bash/$checkalive >/dev/null 2>&1 &
echo "$checkalive..."
echo `date '+%Y-%m-%d %H:%M:%S'` " Start /home/michael/bash/$checkalive" >> $log_file
fi
I can run it manually but when I try to run it in Cron, it doesn't work for some reasons. Apparently the command:
/usr/bin/php5.6 /home/michael/bash/$checkalive >/dev/null 2>&1 &
does not work.
All of file permissions are already set to executable. Any advice?
Thank you
You have run into one of cron's most common mistakes, trying to use it like an arbitrary shell script. Cron is not a shell script and you can't do everything you can do in one, like dereferencing variables or setting arbitrary new variables.
I suggest you replace your values into the cron line and avoid usage of variables
/usr/bin/php5.6 /home/michael/bash/checkalive.php >/dev/null 2>&1 &
Also, consider removing the trailing & as it is not necessary.

pgrep inside bash script not working

I'm trying to save the pgrep -f "instance" output inside a variable in a bash script. For some reason it doesn't work:
here is the code:
function check_running() {
local component_identifier=$1
local process_check=`get_component_param_value $component_identifier $process_check_col | tr -d '\n'`
if [ "$process_check" != "n/a" ]; then
log "process to check: ****""$process_check""****"
pid=`pgrep -f $process_check`
log "pid: " $pid
fi
}
I have tried with different ways, in single and double quotes.
Also, neither this works:
pid=$(pgrep -f "$process_check")
Please note that the process_check variable returns correctly and is definitely working.
I believe the problem is that this field is at the end of the line and may contain a \n char, that is why I've added a tr in the process_check var.
Any idea?
this is the output of the logs:
process to check: ****"instance"****
pid:
I found a way to answer this question:
echo `ps -ef | grep "$process_check" | grep -wv 'grep\|vi\|vim'` | awk '{print $2}'
I hope other people will find this useful
Well, in my case it didn't like the arguments I was trying to pass.
pgrep -f "example" # working
pgrep -f "-f -N example" # not working

Shell Script to find PID of ssh and kill the PID if present

I am trying to write a script to find a reverse SSH PID and kill it if present. I am stuck on "awk" as it gives error. below is the script:
a=('ps -aef | grep "ssh -fN" | grep -v grep | awk '{ print $2 }'')
if [ -n "$a" ]
then
echo "String \"$a\" is not null."
kill -9 "$a"
fi
I commented out if, then, kill and fi lines to debug the script. I get following error:
String "ps -aef | grep "ssh -fN" | grep -v grep | awk {" is not null.
I believe parenthesis for awk is creating the problem and I am unable to get a workaround for this. On Command line, this works perfectly and returns the correct PID.
ps -aef | grep "ssh -fN" | grep -v grep | (awk '{ print $2 }'
Once the PID is passed on to variable "a", I need to issue kill command. OS is Centos 6.4
P.S: I am not fluent on scripting but trying to achieve an objective. Help will be highly appreciated!
There are multiple problems with your script.
You need command substitution to store the output of ps pipeline into an array.
You need to check for the number of elements in the array.
Refer to the array instead of the variable.
The following might work for you:
pids=( $(ps -ef | grep '[s]sh -fN' | awk '{print $2}') )
if [ "${#pids[#]}" -gt 0 ]; then
kill -9 "${pids[#]}";
fi
First, if you have grep and then awk, you can get rid of the greps:
ps -aef | grep "ssh -fN" | grep -v grep | awk '{ print $2 }'
ps -aef |awk ' { if ( ($0 ~ /ssh -FN/) && (! $0 ~ /grep/) ) { print $2 } }'
However, instead of using ps, use pgrep.
pgrep -f "ssh -[fN][fN]" # Will match against either 'ssh -fN' or 'ssh -Nf'
There is even a pkill that will do the entire command for you:
pkill -f "ssh -[fN][fN]"
That will find all of the processes that match that particular string and kill them (if they exist).

Resources