I am using Jenkins in centos 9 Server. My intention is that Jenkins will kill the existing Java jar process and will restart the new build Jar. previously I have used the same command for centos 8 but after installing centos 9 I am facing this problem.
process=`ps -ef | grep -v awk | awk -e '/java.*iqa/ { print $2 }'`
kill -9 ${process}
Below the error message, I am getting. How It can be solved?
++ ps -ef
++ grep -v awk
++ awk -e '/java.*iqa/ { print $2 }'
+ process=57623
+ kill -9 57623
/tmp/jenkins15731163701833690666.sh: line 4: kill: (57623) - Operation not permitted
Build step 'Execute shell' marked build as failure
Related
After a restart of VM I was not able to run any docker command. I follow some question on stack overflow and run the following command ps axf | grep docker | grep -v grep | awk '{print "kill -9 " $1}' | sudo sh
Now There is no image or container.
Result of commands
docker ps and ps -a
docker images list
docker ls
docker ls -a
All the command return empty list. Everything has been clean up.
Is there any way to find our backups or restore the deleted containers?
I am trying to ssh into server, and into a docker container to run the service. however I am not able to store containerId into a variable to pass it to enter the container.
#!/bin/bash
ssh test_server << EOF
ls
sudo docker ps | grep 'tests_service_image' | colrm 13 # This command works
containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13) # This doesn't
sudo docker exec -i "$containerId" bash # Throws error since containerId is empty
./run.sh
EOF
exit
The problem is that you are doing variable/function expansions on your own side. You need to escape those so that those expansions happen on the server side.
#!/bin/sh
ssh test_server << EOF
containerId=\$(sudo docker ps | grep 'tests_service_image' | colrm 13)
sudo docker exec -i "\$containerId" bash
./run.sh
EOF
exit
Edit:
Pass it directly to docker exec command like so
sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash
Original Answer:
This is written assuming that the script execution is done post sshing into the server. but modified the answer to above based on the specific query
container ID is stored in variable containerId, you are getting the error Error: No such container: because you are passing a different variable $container instead of $containerId to docker exec command.
When I try to run Docker:
sudo dockerd
I get this exception:
failed to start daemon: error while opening volume store metadata
database: timeout
What I've tried but didn't help:
ps axf | grep docker | grep -v grep | awk '{print "kill -9 " $1}' | sudo sh
The problem was that I had two instances of Docker running. I solved that by killing them and then started docker again using systemctl start docker
I have a job like this:
parameterized ${GIT_URL} and ${REMOTE_IP}.
clone code by git url and package my project as jar
scp jar file to remote ip, and then start it as server.`
I am using Publish Over SSH Plugin.
The problem is, I have to add every server to my job configuration.
So is it possible to execute shell with parameterized remote ip like this?
#!/bin/sh
scp ${APP_NAME}.jar root#${REMOTE_IP}:/root/${APP_NAME}.jar
ssh root#${REMOTE_IP}
cd /root
ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}' | xargs kill
nohup java -jar ${APP_NAME}.jar &
Yes. Use "$REMOTE_IP" to resolve it to the parameter value.
#!/bin/sh
scp ${APP_NAME}.jar root#"$REMOTE_IP":/root/${APP_NAME}.jar
ssh root#"$REMOTE_IP"
cd /root
ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}' | xargs kill
nohup java -jar ${APP_NAME}.jar &
I solved this in another way.
#!/bin/sh
scp ${APP_NAME}.jar root#${REMOTE_IP}:/root/${APP_NAME}.jar
ssh root#${REMOTE_IP} "sh -s" -- < /opt/jenkins/my.sh ${REMOTE_IP} ${APP_NAME}
So my.sh is a local shell file which define how to start jar as server with parameterized ip
I've been trying to execute bash on running docker container which has specific name as follows. --(1)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs -I'{}' docker exec -it '{}'
but it didn't work and it shows a message like
"docker exec" requires at least 2 argument(s)
when I tried using command as follows --(2)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs docker exec -it
it shows another error messages like
the input device is not a TTY
But when I tried using $() (sub shell) then it can be accomplished but I cannot understand why it does not work with the two codes (1)(2) above (using xargs)
Could any body explain why those happen?
I really appreciate any help you can provide in advance =)
EDIT 1:
I know how to accomplish my goal in other way like
docker exec -it $(docker ps | grep perf | awk '{print $1 " bash"}' )
But I'm just curious about why those codes are not working =)
First question
"docker exec" requires at least 2 argument(s)
In last pipe command, standard input of xargs is, for example, 42a9903486f2 bash. And you used xargs with -I (replace string) option.
So, docker recognizes that 42a9903486f2 bash is a first argument, without 2nd argument.
Below example perhaps is the what you expected.
docker ps | grep somename | awk '{print $1 " bash"}' | xargs bash -c 'docker exec -it $0 $1'
Second question
the input device is not a TTY
xargs excutes command on new child process. So you need to reopen stdin to child process for interactive communication. (MacOS: -o option)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs -o docker exec -it
This worked for me:
sudo docker ps -q | xargs -I'{}' docker exec -t {} du -hs /tmp/
The exec command you run is something like this:
docker exec -it 'a1b2c3d4 bash'
And that is only one argument, not two. You need to remove the quotes around the argument to docker exec.
... | xargs -I'{}' docker exec -it {}
Then you will exec properly with two arguments.
docker exec -it a1b2c3d4 bash
------ ---
first arg ^ ^ second arg