Check if task exist - AWS ECS - aws-cli

How do you check from cli or using a shell script if the task exists?
The documentation is confusing. Is using the --task-definition option as family.
The command
TASK=$(aws ecs describe-task-definition --task-definition ${TASK_NAME}
it errors saying
An error occurred (ClientException) when calling the DescribeTaskDefinition operation: Unable to describe task definition.
Any ideas?

Does this help?
#!/bin/bash
ecs_arn=$1
aws_result=$(aws ecs describe-task-definition --task-definition "$ecs_arn" --query 'taskDefinition.[taskDefinitionArn,status]' --output json 2>/dev/null)
# is it present?
if [ $(echo "$aws_result" | grep -i "$ecs_arn" | wc -l) -ne 1 ]; then
echo "task-definition $ecs_arn doesn't exist."
elif [ $(echo "$aws_result" | grep -i active | wc -l) -ne 1 ]; then
echo "task-definition $ecs_arn exist but not active."
else
echo "task-definition $ecs_arn exist and it's active."
fi
# end
Save it as bash script and execute it like:
./script.sh "arn:aws:ecs:REGION-HERE:ACCOUNT-NAME:............."
You can extract all definition ARNs with:
aws ecs list-task-definitions
I'm redirecting std errors to /dev/null because the api returns error like yours when the task doesn't exist.
You can modify the validations as you like in your case.
Documentation used: https://docs.aws.amazon.com/cli/latest/reference/ecs/index.html

Related

getting first column of a matching line in bash

I am getting an output from a shell command, and want to incorporate the first column of a matching line into a second command. Here is what I have, and it works:
kubectl get pods -n system | while read string; do
if [[ "$string" == my-pod-* && "$string" == *Running* ]]; then
# echo $string
read -ra ADDR <<< "$string"
echo
echo "--- Reading logs for ${ADDR[0]} ---"
# desired output: kubectl -n system logs my-pod-123 --tail=5 -f
kubectl -n system logs ${ADDR[0]} --tail=5 -f
fi
done
the output from first command would look something like this:
name status namespace running
my-pod-123 Running system 4h31m #<<I want this one
another-pod-5 Running system 5h15m
my-pod-023 Terminating system 8h05m
given that the output will contain only one match, is there a shorter way to do this without looping like this? Thanks in advance for helping me improve my Bash skills as this seems very clumsy.
You may use awk like this:
name=$(kubectl get pods -n system | awk '/^my-pod.*Running/{print $1}')
[[ -n $name ]] && kubectl -n system logs "$name" --tail=5 -f
awk command will match pattern my-pod.*Running at the start of a line and if it is found then it will print first column. We store that in variable name.
If $name is not empty then we call kubectl -n system logs using that value.
How about grep?
wanted=$(kubectl get pods -n system | grep 'my-pod-.*Running')
Can do error checking at the same time:
if ! wanted=$(kubectl get pods -n system | grep 'my-pod-.*Running'); then
echo "Error: no running my-pods" >&2
fi

sh command giving "[[: not found " error when I am running through Jenkins stage

In this Jenkin stage, I am trying to get the pod status and if the status is "TRUE" it will move to the next stage else wait for it.
node('master'){
withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/config"]){
POSTGRES_CMD= """while [[ \$(kubectl get pods -l app=postgres-${NAME_SPACE} -o 'jsonpath={..status.conditions[?(#.type=="Ready")].status}' -n ${NAME_SPACE}) != "True" ]]; do echo "waiting for pod" && sleep 1;done"""
echo "${POSTGRES_CMD}"
READY_POD = sh (
script: "${POSTGRES_CMD}",
returnStdout: true
).trim()
echo "${READY_POD}"
Error:- /var/lib/jenkins/workspace/k8s#2#tmp/durable-95fdd/script.sh: [[: not found
POSIX sh supports single bracket conditionals: []. You should be fine using $() for command substitution, in some environment you might need to use back ticks `
while [ "`echo \"true\"`" == "true" ]; do
sleep 1
done
In any case, kubectl supports running that wait loop for you:
kubectl wait pods --for=condition=Ready -l app=postgres-${NAME_SPACE} -n ${NAMESPACE} --timeout=60s
That’s a bash feature. Most small container images use busybox or similar which is sh but not bash.

How to write output to a file in if else statement in shell script

I'm trying to write output to a file, the correct output is printed in the successful file and the same is printed in the unsuccessful log.
In the unsuccessful log it should print only unsuccessful logs but it is printing successful logs also.
for TASKARN in `aws ecs list-tasks --cluster APPS --desired-status RUNNING --region us-east-1 --service-name coreservice-service | jq .taskArns[] | sed 's/.$//; s/^.//' | cut -d":" -f 6 | cut -d"/" -f 3`
do
echo $TASKARN
cd /tmp/
val=`find core-$TASKARN.log -maxdepth 0 -daystart -mtime -1`
if [ $val == core-$TASKARN.log ]
then
echo " core-$TASKARN.log is present " >> /opt/successfull.log
else
echo " core-$TASKARN.log is not present " >> /opt/unsuccessfull.log
fi
done
In successful log
core-26f374a6a.log is present
In unsuccessful log
core-26f374a6a.log is not present
Apart from the shellcheck suggestions, you might consider the below points too..
Replace TASKARN with taskarn. Full-uppercase variables are reserved as environment variables. You may accidentally override an environment variable if you set a full uppercase variable.
Consider removing cd /tmp/. Instead do :
val=$(find /tmp/ "core-${taskarn}.log" -maxdepth 0 -daystart -mtime -1)
# When you embed a variable inside string consider curly braces. See reference.
Reference

Check if service exists in bash (CentOS and Ubuntu)

What is the best way in bash to check if a service is installed? It should work across both Red Hat (CentOS) and Ubuntu?
Thinking:
service="mysqld"
if [ -f "/etc/init.d/$service" ]; then
# mysqld service exists
fi
Could also use the service command and check the return code.
service mysqld status
if [ $? = 0 ]; then
# mysqld service exists
fi
What is the best solution?
To get the status of one service without "pinging" all other services, you can use the command:
systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"
By the way, this is what is used in bash (auto-)completion (see in file /usr/share/bash-completion/bash_completion, look for _services):
COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )
Or a more elaborate solution:
service_exists() {
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
if service_exists systemd-networkd; then
...
fi
Hope to help.
Rustam Mamat gets the credit for this:
If you list all your services, you can grep the results to see what's in there. E.g.:
# Restart apache2 service, if it exists.
if service --status-all | grep -Fq 'apache2'; then
sudo service apache2 restart
fi
On a SystemD system :
serviceName="Name of your service"
if systemctl --all --type service | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
On a Upstart system :
serviceName="Name of your service"
if initctl list | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
On a SysV (System V) system :
serviceName="Name of your service"
if service --status-all | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
In systemd (especially in Debian), it doesn't seems to work properly using the various answers from here. For some services like pure-ftpd if it's in disabled mode, it will not show up in service list when you trigger this command:
systemctl --all --type service
and when you start again the pure-ftpd with systemctl start pure-ftpd the list will appear again. So listing the service using systemctl --all --type service will not work for all services. Take a look at this for more information.
So, this is the best code so far (improvement from #jehon's answer) to check if a service is exist (even it has status inactive, dead or whatever status it is):
#!/bin/bash
is_service_exists() {
local x=$1
if systemctl status "${x}" 2> /dev/null | grep -Fq "Active:"; then
return 0
else
return 1
fi
}
if is_service_exists 'pure-ftpd'; then
echo "Service found!"
else
echo "Service not found!"
fi
Explanation:
If systemctl status found a service, it must have a text 'Active:' we filter using grep and it would return 0. If there is no 'Active:' text it would return 1.
If systemctl status does not find the 'Active:' text, it will print out a standard error. So, I put redirection 2> /dev/null to redirect the standard error. For example, if you are looking for the non existence service, you would get this error message if you don't put that error redirection:
Unit pure-ftpdd.service could not be found.
We don't want to have the above standard error message if you are doing scripting
EDIT:
Another method is to list out unit files which able to detect disabled service as pointed by #Anthony Rutledge for Debian system:
systemctl list-unit-files --type service | grep -F "pure-ftpd"
But using this method will not always work especially for older system because some unit files might not be detected using this command as explained in here. Also, using this method is slower if you have large unit-files that need to be filtered (as commented by #ygoe about heavy load on a small computer).
To build off of Joel B's answer, here it is as a function (with a bit of flexibility added in. Note the complete lack of parameter checking; this will break if you don't pass in 2 parameters):
#!/bin/sh
serviceCommand() {
if sudo service --status-all | grep -Fq ${1}; then
sudo service ${1} ${2}
fi
}
serviceCommand apache2 status
After reading some systemd man pages ...
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
... and systemd.services(5)....
... and a nice little article ...
https://www.linux.com/learn/understanding-and-using-systemd
I believe this could be an answer.
systemctl list-unit-files --type service
Pipe to awk {'print $1'} to just get a listing of the service units.
Pipe to awk again to get the service names exclusively. Change the field separator to the period with -F.
awk -F. {'print $1'}
In summary:
systemctl list-unit-files --type service | awk {'print $1'} | awk -F. {'print $1'}
With variation and augmentation of the base solution, you can determine the state of your system's services by combining a for loop with systemctl is-active $service.
#!/bin/sh
service=mysql
status=$(/etc/init.d/mysql status)
print "$status"
#echo $status > /var/log/mysql_status_log
var=$(service --status-all | grep -w "$Service")
if [ "output" != "" ]; then
#executes if service exists
else
#executes if service does not exist
fi
$Service is the name of the service you want to know if exists.
var will contain something like
[+] apache2
if the service does exist
if systemctl cat xxx >/dev/null 2>&1; then
echo yes
fi
Try this, as ps command can be used in both Ubuntu&RHEL, this should be work in both platform.
#!/bin/bash
ps cax | grep mysqld > /dev/null
if [ $? -eq 0 ]; then
echo "mysqld service exists"
else
echo "mysqld service not exists"
fi

CoreOS - get docker container name by PID?

I have a list of PID's and I need to get their docker container name. Going the other direction is easy ... get PID of docker container by image name:
$ docker inspect --format '{{.State.Pid}}' {SOME DOCKER NAME}
Any idea how to get the name by PID?
Something like this?
$ docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.ID}}' | grep "^${PID},"
[EDIT]
Disclaimer This is for "normal" linux. I don't know anything useful about CoreOS, so this may or may not work there.
Because #Mitar's comment suggestion deserves to be a full answer:
To get container ID you can use:
cat /proc/<process-pid>/cgroup
Then to convert the container ID to docker container name:
docker inspect --format '{{.Name}}' "${containerId}" | sed 's/^\///'
... as a one-liner as well
PID=20168; sudo docker ps --no-trunc | grep $(cat /proc/$PID/cgroup | grep -oE '[0-9a-f]{64}' | head -1) | sed 's/^.* //'
I use the following script to get the container name for any host PID of a process inside a container:
#!/bin/bash -e
# Prints the name of the container inside which the process with a PID on the host is.
function getName {
local pid="$1"
if [[ -z "$pid" ]]; then
echo "Missing host PID argument."
exit 1
fi
if [ "$pid" -eq "1" ]; then
echo "Unable to resolve host PID to a container name."
exit 2
fi
# ps returns values potentially padded with spaces, so we pass them as they are without quoting.
local parentPid="$(ps -o ppid= -p $pid)"
local containerId="$(ps -o args= -f -p $parentPid | grep docker-containerd-shim | cut -d ' ' -f 2)"
if [[ -n "$containerId" ]]; then
local containerName="$(docker inspect --format '{{.Name}}' "$containerId" | sed 's/^\///')"
if [[ -n "$containerName" ]]; then
echo "$containerName"
else
echo "$containerId"
fi
else
getName "$parentPid"
fi
}
getName "$1"

Resources