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
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
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
how can i check if a process is running, if it is running then echo "process is running" and keep them from using that process til its finished. i have this piece of code but i cant get it to not allow them to use that process after it echos:
#!/bin/bash
SERVICE=EXAMPLE
if ps ax | grep -v grep | grep -v grep | grep $SERVICE > /dev/null
then
echo -e ""
echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
fi
It seems you want to write a loop, not a single if statement.
And you probably want to sleep a bit between checking the condition.
#!/bin/bash
SERVICE=EXAMPLE
while ps ax | grep -v grep | grep "$SERVICE" > /dev/null
do
echo
echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
sleep 60
fi
The condition can be simpler if you have pgrep:
while pgrep "$SERVICE" >/dev/null
(Or the simpler while pgrep -q "$SERVICE" if your implementation of pgrep supports it.)
When there is no matching process (already finished or not started yet),
then the script will not produce any output.
If you want to get some output in that case,
then you can rework like this:
while true
do
if pgrep "$SERVICE" > /dev/null; then
echo
echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
sleep 60
else
echo "Process '$SERVICE' not running"
break
fi
fi
To print the message only once and just wait until the process is no longer running:
is_running() {
pgrep "$SERVICE" > /dev/null
}
if is_running; then
echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
while true; do
sleep 60
is_running || break
done
else
echo "Process '$SERVICE' not running"
fi
Another solution
#!/bin/bash
tput civis # hide cursor
until ! ps -ef | grep "$SERVICE" | grep -v "grep" > /dev/null; do
while ps -ef | grep "$SERVICE" | grep -v "grep" > /dev/null; do
echo -en "\r${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
done
printf "\r%80s" "" # clear line
echo -en "\rProcess '$SERVICE' is completed!\n"
done
tput cnorm # show cursor again
This solution is useful also if you have multiple instance of the service together.
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
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.