CodeDeploy fails in ApplcationStop coz not able to Kill process - bash

I have been using the same script and it used to work before. Due to another error previously,
I deleted the application folder from the console. (The application was killed successfully in the previous deploy but there was an error in AfterInstall)
stop_bot.sh:
#!bin/bash
ps -ef | grep testMain.py | grep -v grep | awk '{print $2}' | xargs kill -9
Error:
LifecycleEvent - ApplicationStop
Script - scripts/stop_bot.sh
[stderr]kill: not enough arguments

The error means that grep testMain.py does not return any values. You have to double check your programs and ensure that grep testMain.py (or whatever it should be) actually provides a list of processes to be killed.

Related

Bash shell script command executes out of order when run through Bitrise

In the bitrise workflow's script step, I added following snippet:
adb shell ps | grep screenrecord | awk ‘{print $2}’ | xargs adb shell kill
Purpose is to kill the process called screenrecord that was started in a previous step and it works fine when I test it on my system. But when this workflow is triggered through bitrise, it fails with following logs:
What is the cause of this issue and how to fix it?
Most likely this is because awk is not outputting the process id. One possible workaround to try is the following:
adb shell ps | grep screenrecord | sed -E 's/[ ]+/ /g' | cut -d' ' -f2 | xargs adb shell kill
where the awk command has been substituted with sed (to remove the multiple spaces) and a cut one (to get the process id).

How to get and kill the process Mac terminal

Find specific client with name and memory
Get the client pId
Kill pId
Can I do kill that process just within the terminal.
What I tried
ps aux | grep leagueClientUxHelper | awk '{print $2}'
but I can't get the large memory process.
Same process names in activated status:
You can kill a process with the id by using the kill command.
ps aux | grep TextEdit | awk '{print $2}' | xargs kill
That will pipe it directly into the kill command. You will get an error because the ps aux will show two PIDs but it won't cause any problems.
Kill only works with PIDs
Are you trying to drill down to a certain memory length or compared to another instance like league1 memory is > leage2 memory?

Strip process pid to log_file_name (weird behaviour when running from cron)

I am curious if anyone have similar issue when running scripts from cron. This line of script should copy opentsdb_daemon.log file to opentsdb_daemon_with_pid.log. Currently openTSDB is running on only one PID.
!/bin/sh
cp -f /opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon.log "/opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon_pid_$(ps -ef | grep [o]pentsdb | awk '{print $2}').log
It runs ok. File opentsdb_daemon_pid_76079.log is created but when running fron cron it's creating something like this: opentsdb_daemon_pid_63453?63454?76079.log
I have tried to run it from diffrent cron users - with same effect. I would be extremely grateful for any advice.
The command ps -ef | grep [o]pentsdb | awk '{print $2}' should return multiple PID at the time cron run it, what you get is all the PID separated by "?"
The "?" is because \n is not correctly diplayed in the filename
I assume that it is because when cron executes the command, the command appears in the process list, so :
grep [o]pentsdb is also grep by grep [o]pentsdb ;)
You can determine it by the two consecutive PID 63453 and 63454 which are the process lines "cron execute the command xxx" and the child of this process which is the "command xxx"
Maybe a solution could be to add something like :
$(ps -ef | grep [o]pentsdb |grep -Ev "grep|cron" | awk '{print $2}')

Kill command won't work correctly in bash script

I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.

Grep output of command and use it in "if" statement, bash

Okay so here's another one about the StarMade server.
Previously I had this script for detecting a crash, it would simply search through the logs:
#!/bin/bash
cd "$(dirname "$0")"
if ( grep "[SERVER] SERVER SHUTDOWN" log.txt.0); then
sleep 7; kill -9 $(ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}')
fi
It would find "[SERVER] SERVER SHUTDOWN" and kill the process after that, however this is not a waterproof method, because with different errors it could be possible that the message doesn't appear, rendering this script useless.
So I have this tool that can send commands to the server, but returns an EOF exception when the server is in a crashed state. I basically want to grab the output of this command, and use it in the if-statement above, instead of the current grep command, in such a way that it would execute the commands below when the grep finds "java.io.EOFException".
I could make it write the output to a file and then grep it from there, but I wonder, isn't there a better/more efficient method to do this?
EDIT: okay, so after a bit of searching I put together the following:
if ( java -jar /home/starmade/StarMade/StarNet.jar xxxxx xxxxx /chat) 2>&1 > /dev/null |grep java.io.EOFException);
Would this be a valid if-statement? I need it to match "java.io.EOFException" in the output of the first command, and if it matches, to execute something with "then" (got that part working).
Not sure to solve your problem, but this line:
ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}'
could be change to
ps -aef | awk '/[S]tarMade.jar/ {print $2}'
The [S] prevents awk from finding itself.
Or just like this to get the pid
pidof StarMade.jar

Resources