Process ID of nohup process continually updating - cant kill process - shell

I am trying to kill a nohup process in an EC2 instance but so far have been unsuccessful. I am trying to grab the process ID (PID) and then use it with the kill command in terminal, like so:
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
with columns, (I believe) they're:
UID PID PPID C STIME TTY TIME CMD
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
However, each time I try to kill the process, I get an error saying that the PID doesn't exist, seemingly because the PID changed. Here is a sequence I am running into in my command line:
// first try, grab the PID and kill
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16580
-bash: kill: (16580) - No such process
// ?? - check for correct PID again, and try to kill again
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16583 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16583
-bash: kill: (16583) - No such process
// try 3rd time, kill 1 PID up
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16584 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-myip ~]$ kill 16585
-bash: kill: (16585) - No such process
This is quite a struggle for me right now, since I need to kill/restart this nohup process. Any help is appreciated!
EDIT - I tried this approach to killing the process because it was posted as an answer in this thread (Prevent row names to be written to file when using write.csv) and was the 2nd highest rated answer.

Very very bad question ...
You are trying to kill you grep process...
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
The command is grep --color=auto nohup
I'm not sure you can kill nohup
nohup will run your command in a particular way. But after its launching, the nohup process dies.

If you want to grep the ps output :
ps -ef | grep '[n]ohup'
or
pgrep -fl nohup
because you are trying to kill not nohup pid but the grep itself...

Related

Why is exec command creating a child process?

I have a script from inside which I am calling the exec command. The sole purpose of this is to basically check if the script has been run as a specific user if not just run it as that user.
#!/bin/bash
typeset -ri KNOWNUSER_UID=92164
# Exec with known role account if not already
if [[ $(id -u) != "$KNOWNUSER_UID" ]]; then
exec sudo -u kuser "$0"
fi
sleep 100
I expected the above script to detect when running using root to show me just a single process with kuser. But this is what i see.
root 51823 20591 0 18:37 pts/0 00:00:00 sudo -u kuser /bin/supervisor
kuser 51825 51823 0 18:37 pts/0 00:00:00 /bin/bash /bin/supervisor

while running ps -ef | grep 'keyword' in cmd, getting one output values but while running in ansible i am getting two output values

While running ps -ef | grep 'keyword' in cmd i am getting one output values, but while running same command in ansible playbook i am getting two outputs, i dont know why.
Note: process is not running
while running in command, I am getting output like this
user 4917 130211 0 00:20 pts/0 00:00:00 grep --color=auto keyword
while running in ansible playbook i am getting outputs like this
\user 2480 2477 0 07:02 pts/1 00:00:00 grep keyword
user 2477 2476 81 07:02 pts/1 00:00:00 /bin/sh -c ps -ef | grep keyword
When you run ps -ef | grep 'keyword' the resulting list includes the grep command.
Suggesting to use command pgrep
pgrep -af 'keyword'
The output result is a list of lines:
pid 'command line that match RegExp pattern 'keyword'
If interested in pid only:
pgrep -f 'keyword'
If wants to store the pid in variable:
var1=$(pgrep -f 'keyword')
If wants to kill all the process:
pkill -9 -f 'keyword'

Finding running processes in shell script

I have shutdown all services on the server using putty.How do I find which all services are still running using the grep command? Also how do I kill these services using process id?
You can use below commands:
To list all the running processes:
ps -ef
To list down all processes of a particular user:
ps -u userId
To list a particular process:
ps -ef | grep processName
To list down a particular process run by a particular user:
ps -u userId | grep processName
Now to kill a process:
While you use ps command it list like below:
UID PID PPID C STIME TTY TIME CMD
user1 398 1 0 Mar 08 ? 1331:39 /usr/sbin/nscd
user2 1388 1 0 Mar 08 ? 11:52 /usr/lib/netsvc/yp/ypbind
Now to kill a process you need the process ID that is PID here.
Now to kill a process run by user user1:
kill -9 398 ( sure kill - Not safe)
kill -1 398 ( safe kill - let the process to complete its talk)
kill -15 398 ( safe kill like -1)
kill -8 398 ( Safe kill like -1)
Hope this will help you.

Why docker exec is killing nohup process on exit?

I have running docker ubuntu container with just a bash script inside. I want to start my application inside that container with docker exec like that:
docker exec -it 0b3fc9dd35f2 ./main.sh
Inside main script I want to run another application with nohup as this is a long running application:
#!/bin/bash
nohup ./java.sh &
#with this strange sleep the script is working
#sleep 1
echo `date` finish main >> /status.log
The java.sh script is as follow (for simplicity it is a dummy script):
#!/bin/bash
sleep 10
echo `date` finish java >> /status.log
The problem is that java.sh is killed immediately after docker exec returns. The question is why?
The only solution I found out is to add some dummy sleep 1 into the first script after nohup is started. Than second process is running fine. Do you have any ideas why it is like that?
[EDIT]
Second solution is to add some echo or trap command to java.sh script just before sleep. Than it works fine. Unfortunately I cannot use this workaround as instead of this script I have java process.
This is not an answer, but I still don't have the required reputation to comment.
I don't know why the nohup doesn't work. But I did a workaround that worked, using your ideas:
docker exec -ti running_container bash -c 'nohup ./main.sh &> output & sleep 1'
Okay, let's join two answers above :D
First rcmgleite say exactly right: use
-d
options to run process as 'detached' background.
And second (the most important!) if you run detached process, you don't needed nohup!
deploy_app.sh
#!/bin/bash
cd /opt/git/app
git pull
python3 setup.py install
python3 -u webui.py >> nohup.out
Execute this inside a container
docker exec -itd container_name bash -c "/opt/scripts/deploy_app.sh"
Check it
$ docker attach container_name
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 11768 1940 pts/0 Ss Aug31 0:00 /bin/bash
root 887 0.4 0.0 11632 1396 pts/1 Ss+ 02:47 0:00 /bin/bash /opt/scripts/deploy_app
root 932 31.6 0.4 235288 32332 pts/1 Sl+ 02:47 0:00 python3 -u webui.py
I know this is a late response but I will add it here for documentation reasons.
When using nohup on bash and running it with 'exec' on a docker container, you should use
$ docker exec -d 0b3fc9dd35f2 /bin/bash -c "./main.sh"
The -d option means:
-d, --detach Detached mode: run command in the
background
for more information about docker exec, see:
https://docs.docker.com/engine/reference/commandline/exec/
This should do the trick.

Trying to kill process by shell scripting

I want to kill process through shell scripting it is giving me error. Here is what i tried so far:
When i try to kill memcache it give me error like "kill: No such process" , i used below command:
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
or if try like below:
kill -9 $(pidof memcache)
i get error like below:": arguments must be process or job IDs"
When i run directly on command prompt process is running:
ring#ubuntu:~/parasol$ ps aux | grep memcache
memcache 873 0.0 0.0 323220 1188 ? Sl 22:56 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
ring 1714 0.0 0.0 9384 920 pts/0 S+ 23:45 0:00 grep --color=auto memcache
I reff to https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-name
AND
Shell script to capture Process ID and kill it if exist
My Ubuntu Version:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"
Acutally
ps -ef | grep memcache
will give two lines..
so you can go this way
ps -ef | grep memcache | grep -v "grep" | awk '{print $2}' | xargs kill;
This can get you exact one PID
if I has to do I would break it in 2 lines in start
#!/bin/bash
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
echo $PID
#to check PID is right
kill -9 $PID
save it in scrip files say test.sh
then on terminal
chmod +x test.sh
then
./test.sh
You can use these commands:
pkill memcached
or
pgrep memcached | xargs kill -9
or
killall memcached.
pgrep, pkill - look up or signal processes based on name and other attributes.
Your first command might be killing itself before having chance to kill the target processes.
For a reliable way, run pkill /usr/bin/memcached or pkill -9 /usr/bin/memcached although the latter is a bad practice.
For the first command, you see that there are two processes that matches the pattern you grep for. The actual memcached process and your grep process. That is probably the reason for the error of the first command line.
Try narrowing the search down, for example by grepping for e.g. "^memcache.*/usr/bin/memcached".
The problem with the second error is that you're calling pidof with the username instead of the process name, so the command is essentially kill -9 without any process id. Try instead e.g. pidof memcached to get the process id of the correct process.
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
#...
First, you could use cut instead of awk in this case. No need to use a tank to kill a fly ;)
Then, why is it necessary to brutally kill memcache? You have a daemon to stop it :
/etc/init.d/memcached stop
service memcached stop

Resources