how can I compare numbers in my script:
#!/bin/sh
service=myservice
if [ $(ps | grep -v grep | grep $service | wc -l) > 0 ]
then
echo "$service is running!!!"
else
echo "$service is NOT running!!!"
fi
the above is not working
Using git bash:
$ if ((9 > 2 )); then
> echo 'hello'
> fi
hello
thanks to #anubhava, I made this script
#!/bin/sh
service=myService
if pgrep "$service" > /dev/null
then
echo "$service is running!!!"
else
echo "$service is NOT running!!!"
fi
Related
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
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
Hi I've been searching on the forum but I cant seem to get this right. I am trying to create a script that asks the user which process they are searching for then returns with a 1 if the process is running.
This works:
#!/bin/bash
SERVICE='httpd'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi
I want to add this to the script:
echo -e "please enter process name: \c"
read word
for something like:
#!/bin/sh
echo -e "please enter process name: \c"
read input_variable
if ps ax | grep -v grep | grep $varname > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi
Use pgrep to search for processes:
read process_name
if pgrep "${process_name}" >/dev/null 2>&1 ; then
"echo ${process_name} found"
else
"echo ${process_name} not found"
fi
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 =
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.