if condition getting executed even though the condition is not satisfied - shell

Whats wrong in my script?
im trying to start the oracle listener & a service which uses a java process.
But if condition getting executed even though the condition is not satisfied...(i.e)
when value of this is (ps -ef | grep -v root | grep LISTENER | wc -l) is 1 (listener is running) , if condition alone executing.
below is the code
#!/bin/bash
mydate=date --rfc-3339=seconds
logFile=/home/user/Pictures/ostart.log
service=oatxpress
service1=listener
if [[ $(ps -ef | grep -v root | grep LISTENER | wc -l) -eq 0 ]] && [[ $(ps -ef | grep -v grep | grep java | wc -l) -eq 0 ]];
echo "$(ps -ef | grep -v root | grep LISTENER | wc -l)" >>$logFile
echo "$mydate : $service1 and $service is not running!!!" >>$logFile
then
lsnrctl start
service oatxpress start
echo "$mydate : $service1 and $service is started via script!!!" >>$logFile
elif [ $(ps -ef | grep -v root | grep LISTENER | wc -l) -eq 0 ];
echo "$mydate : $service1 is not running!!!" >>$logFile
then
lsnrctl start
echo "$mydate : $service1 is started via script!!!" >>$logFile
elif [ $(ps -ef | grep -v root | grep java | wc -l) -eq 0 ];
echo "$mydate : $service is not running!!!" >>$logFile
then
service oatxpress start
echo "$mydate : $service is started via script!!!" >>$logFile
else
echo "$mydate : $service & $service1 are running!!!" >>$logFile
fi
}

Your format is not what you intend.
You are writing if cmd1; cmd2; cmd3; then..., when you intend to write if cmd1; then cmd2; cmd3 In other words, you are using [[..]] && [[...]] to check some process counts but ignore those values, then you do two echo statements; if the 2nd echo succeeds then the script enters the first clause and invokes lsnctrl start; services .... Just move your then to be before the echos. That is:
if [[ ...
then ...
echo ...
echo ...
elif ...
then
echo ...
else
echo ...
fi

Related

Needed shell script which will check for not responding apps and kill them

Need to execute on MacOS.
Most of the solution is giving status running or stopped but for Not Responding state not having any solution.
tried solutions like this
pgrep "$1" 2>&1 > /dev/null
echo $?
if [ $? -eq 0 ]
then
{
echo " "$1" PROCESS RUNNING "
ps -ef | grep $1 | grep -v grep | awk '{print $2}'| xargs kill -9
}
else
{
echo " NO $1 PROCESS RUNNING"
};fi

how to stop script from running when drives are full

I have a script that will run and fill up a drive without remorse. I want to set a highwater mark of 90%.
I created a cron that will check if the script is installed and if so if running.
#!/bin/sh
COUNTER=$(df -Ph | grep -vE '^tmpfs|cdrom' | sed s/%//g | sed 1d | awk '{ if($5 > 90) print $1;}' | wc -l)
if [[ -e /var/stats/automation/stats-collection.pl ]]; then
_running=$(ps faux | grep stats-collection.pl | grep -v grep | awk '{print $2}')
if [[ $_running -eq 1 ]]; then
echo "Stats running"
if [[ $COUNTER -gr 0 ]]; then
echo "Drive found above highwater mark. Ending Platform stats."
kill $_running
fi
else
echo "Platform stats not running."
else
echo "Platform stats not found"
fi
How would I combine the benefits of a continuously running cron job to run and stop the offending script from running. Would a while loop be the answer?

Bash Script issue, command not found, PATH seems to be correct

I have a issue with my Script, i am just trying to fingure out if my screen session is running or not (line 19).
The rest of the script is working.
#!/bin/bash
echo $PATH // /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
echo "0"
content=$(wget http://interwebs.com/index.php?page=count -q -O -)
z=$(($content / 5))
z=$(($z + 1))
echo $z // 4
lockfile=/var/tmp/mylock
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# do stuff here
x=1
count=0
while [ $x -le $z ]
do
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
if [ "$req" = "" ]; then
# run bash script
screen -amds mystatus$count /usr/bin/wget --spider interwebs.com/index.php?page=cronwhatsoever$(( $count +1))-$(( $count +5))
else
echo "Cron running"
fi
x=$(( $x + 1 ))
count=$(( $count +5))
done
# clean up after yourself, and release your trap
rm -f "$lockfile"
trap - INT TERM EXIT
else
echo "Lock Exists: $lockfile owned by $(cat $lockfile)"
fi
sleep 15
It returns line 19: =: command not found. Actually running:
ps -ef | grep -i bukkit | grep -v grep
Works without issues if i run it directly in my Terminal, so any idea how to solve this issue?
I guess it something PATH related but grep is located in /bin/grep.
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
should be
req="$(ps -ef | grep -i mystatus$count | grep -v grep)"
Don't use $ on the left-hand side of an assignment, and you must not have spaces around the =

not a valid identifier - bash script error while execution

I'm getting following error while I try to capture process ids in my shell script.....
$bash ./restartjbossserver.sh
./restartjbossserver.sh: line 10: `i=$(ps -ef | grep "jboss" | grep -v "grep" | awk '{print $2}')': not a valid identifier
And this is my script....
for i=$(ps -ef | grep "jboss" | grep -v "grep" | awk '{print $2}')
do
echo $i
if [ $i != NULL ]
then
echo "Killing JBos Process.."
kill -9 $i
echo "Killed Joss Process..."
fi
done
sleep 10s
echo "Deleting JBoss Cache..."
rm -rf /home/cbsmsblapp/opt/EAP-6.3.0/jboss-eap-6.3/domain/tmp/*
echo " Deleted JBoss Cache..."
sleep 10s
nohup /home/cbsmsblapp/opt/EAP-6.3.0/jboss-eap-6.3/bin/domain.sh & >nohup.out
The syntax for iterating over a list is
for i in $( ...
not
for i=$( ...
Have a look at the pkill and pgrep commands. You could just pkill jboss.

If or while loop inside case command positional parameters

Being relatively new to anything other than bash scripting, I have created a script to
check if a process is running
output PID's to the shell
if not prompt user input and start etc/etc.
I've moved onto positional parameters and can't see where I'm going wrong:
if [ "$1" == "" ]; then
proc_finder
elif [ $1 != "" ];then
case $1 in
-p | --process )
shift
z=$(ps aux |grep $1 |grep -v grep > /dev/null)
if [ ! -z "$z" ]; then
echo "YES"
else
echo "NO"
fi
;;
* )
echo "Usage -p (process)"
esac
fi
This always seems to return yes even when putting in -p test for example. I know im doing something fundamentally wrong, looking at the verbose output the grep -v grep is being done last hence I believe it always returnes an exit state of 0.
Shouldn't that be if [ $? -eq 0 ]?
EDIT 1
You can try this:
z=`ps aux | grep $1 | grep -v grep > /dev/null`
if [ ! -z "$z" ]; then
echo "YES"
else
echo "NO"
fi
If $z is not empty (-z: test for zero-length string) this implies the process was found with the ps command.
EDIT 2
The ps ... grep ... grep is being redirect to /dev/null. That means z will contain nothing. remove the redirection and z should have some output.
z=`ps aux | grep $1 | grep -v grep`
EDIT 3
Alternatively, you can just do this:
ps aux | grep $1 | grep -v grep > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "YES"
else
echo "NO"
fi
In this case, you are not saving the grep output. That's good if you don't really need it.

Resources