not a valid identifier - bash script error while execution - bash

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.

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

ps(bash) command in ci pipeline

I have a code in ci pipeline:
after_script:
- echo "killing ssh-agent"
- PID=$(cat /tmp/agent-{$CI_JOB_ID})
- ps aux | grep ssh-agent
- if [ $(ps aux | grep ssh-agent | awk '{print $2}') -eq $PID ]; then kill -9 $(echo $PID); else echo 'there is no pid'; fi
- kill -9 $(echo $PID)
- echo "PID" $PID "is killed"
when I execute pipeline I get this output:
$ ps aux | grep ssh-agent
sh: line 137: ps: command not found
how can I return all service IDs and kill this one which I need

Bash outputting kill usage

I have a bash script as follows:
if [[ "$1" == "stop" ]]; then
echo "[$(date +'%d/%m/%Y %H:%M:%S:%s')]: Killing all active watchers" >> $LOG
kill -9 $(ps -ef | grep "processname1" | grep -v "grep" | grep -v "$$" | awk
'{print $2}' | xargs)
echo "[$(date +'%d/%m/%Y %H:%M:%S:%s')]: Killing all current processname2
processes" >> $LOG
kill -9 $(ps -ef | grep "processname2" | grep -v "grep" | awk '{print $2}' |
xargs)
exit 0
when i run 'x service stop', the following is outputted:
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill
-l [sigspec]
Killed
How do i stop the kill usage being displayed? It is successfully killing the process, however the fact that the usage is displayed is causing AWS CodeDeploy to fail.
Thanks!
Adam, please note that this is really just a comment with formatting. Don't take this as a real answer to your question. Please focus on the constructive comments to your question.
In my mis-spent youth, I wrote this bash function to do the ps -ef | grep .... madness:
# ps-grep
psg() {
local -a patterns=()
(( $# == 0 )) && set -- $USER
for arg do
patterns+=("-e" "[${arg:0:1}]${arg:1}")
done
ps -ef | grep "${patterns[#]}"
}
using the knowledge that the pattern [p]rocessname will not match the string [p]rocessname

script to kill a process in macos bash not working: illegal process id

I created this function to kill a process by partial name, put it in bash_profile, and executed it.
when I run it one command at a time manually, everything works.
but when I call the function, it fails with the output: "illegal process id"
function killServer() {
pid=$(ps -fe | grep '[p]rocessName' | awk '{print $2}')
if [[ -n $pid ]]; then
kill $pid
else
echo "Does not exist"
fi
}
output:
kill: illegal process id: i311821
running
ps -fe | grep '[p]rocessName'
gives:
1543721191 1947 1946 0 9:12AM ?? 0:46.76 ../../jdk/bin/java -server -da -XX:PermSize=256m Xrunjdwp:transport=dt_socket,address=8000,suspend=n,server=y -DMonitorDisabled -Xms2048m -Xmx2048m -Dwrapper.port=32000 -com.XXX.YYY.server.util.Main -b 0.0.0.0 -c default
what is the reason for that?
found the problem:
a space after $2 was missing. after the fix, it works:
ps -ef | grep "[X]XX" | grep -v grep | awk '{print $2 }' | xargs kill -9

shell script to search for a substring on AIX OS

I need to search the string "-Xms" from the below code:
cat | ps -eaf|grep $LOGNAME | while read LINE
do
if [[ grep LINE = "-Xms" ]]; then
pid=$(echo $LINE | awk '{print $2}')
#echo $pid
kill -9 $pid
fi
:
done
But this code does not work on AIX. It works fine on Linux.
Please help.
I made small changes in the script like making a variable for storing return of grep.
Try this
ps -aef|grep $LOGNAME | while read LINE
do
PAT=`grep "\-Xms" $LINE`
if [ "$PAT" != "" ]; then
pid=$(echo $LINE | awk '{print $2}')
#echo $pid
#kill -9 $pid
fi
done
i did not test though.
your complete loop can be replaced with :
ps -eaf | grep $LOGNAME| grep "-Xms"|awk '{print $2}'|xargs kill -9
coming to searching for substring:
cat | ps -eaf|grep $LOGNAME |grep "-Xms" while read LINE
do
pid=$(echo $LINE | awk '{print $2}')
kill -9 $pid
done

Resources