I can use
pgrep -f 'keyword1 | keyword2'
to run a pgrep and return all processes that match either keyword.
How can I use & to do this instead? I just want processes that contain both keywords
The following patterns failed:
pgrep -f 'keyword1 & keyword2'
pgrep -f 'keyword2 && keyword2'
MAN pgrep(1)
OPTIONS
-f The pattern is normally only matched against the process
name. When -f is set, the full command line is used.
.
Side question:
Is there a built in Python library for running these commands? I couldnt seem to find one and everyone suggested using subprocess.Popen(), which is how I'm running the 'pgrep' command, however I'd prefer a pure Python solution if it's available
I'm not sure you can do that with pgrep you can however use awk:
ps ax -o pid,cmd | awk '{pid = $1; $1=""}/[k]eyword1/ && /keyword2/ {print pid}'
The reason i use [k]eyword1 is to avoid matching the awk process.
If PCRE is supported with pgrep something like this would work:
pgrep -f '(?=.*keyword1)(?=.*keyword2)'
You can use or with a wildcard reversing the pattern to get either keyword in any order.
pgrep -f 'keyword1.*keyword2|keyword2.*keyword1'
The typical way to do a grep 'and' is to grep multiple times. Since pgrep returns pids you have to filter the list of processes directly and then extract the PID:
ps ax -o pid,cmd | grep 'keyword1' | grep 'keyword2' | awk '{print $2}'
Related
This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 1 year ago.
The following python script:
def run_build(path):
cmd = path + '/build.sh'
p = subprocess.call(cmd)
The following bash script exec two another scripts:
#!/bin/bash
cd "${0%/*}"
echo $(./create_env.sh)
echo $(./set_webhook.sh)
echo $(docker-compose up -d --build)
create_env.sh:
#!/bin/bash
PORT=$(comm -23 <(seq 7000 8000 | sort) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort -u) | head -n 1)
MONGODB_PORT=$(comm -23 <(seq 27017 27100 | sort) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort -u) | head -n 1)
destdir=$PWD/.env
echo >> "$destdir"
echo "APP_PORT=$PORT" >> "$destdir"
echo "MONGODB_PORT=$MONGODB_PORT" >> "$destdir"
The output is:
Path: /home/navka/Environments/teststartupservicebot/build.sh
./create_env.sh: line 2: head: command not found
./create_env.sh: line 2: comm: command not found
./create_env.sh: line 2: seq: command not found
...
Where is my problem? Thanks!
I would say your first step would be to place:
echo $PATH
as the first line following the #!/bin/bash shebang line in create_env.sh, to ensure the path is set up.
Make sure it contains the directory for those executables (probably /usr/bin), which you can probably find out by executing (for example) which comm or where comm from a command line.
If it doesn't contain the relevant directory, that explains why it cannot find the executables. In that case, you will need to discover why they're not there.
Perhaps the simplest fix would be to just add something like:
PATH="${PATH}:/usr/bin"
to your environment setup script. This will ensure the path does have the relevant entry.
And, as an aside, if those lines in build.sh are meant to be cumulative (so, for example, set_workbook requires the environment changes made by create_env, you should be aware that these are currently run in sub-shells, meaning changes from one will not persist after the sub-shell exits.
That's not necessarily the case as you persist them to a file, which may be read by the subsequent steps.
If you do need the changes in the environment for subsequent steps (as opposed to a file), you will need to source them in the context of the current shell, such as with:
. ./create_env.sh
As I said, this may not be necessary but you may want to look into it, just in case.
I have Raspbian as the linux distro running on my RPI. I've setup a small socket server using twisted and it receives certain commands from an iOS app. These commands are strings. I started a process when I received "st" and now I want to kill it when i get "sp". This is the way I tried:
Imported OS
Used os.system("...") //to start process
os.system("...") // to kill process
Lets say the service is named xyz.
This is the exact way I tried to kill it:
os.system('ps axf | grep xyz | grep -v grep | awk '{print "kill " $1 }' | sh')
But I got a syntax error. That line runs perfectly when I try it in terminal separately. Is this a wrong way to do this in a python script? How do I fix it?
You will need to escape the quotes in your string:
os.system('ps axf | grep xyz | grep -v grep | awk \'{print "kill " $1 }\' | sh')
Or use a triple quote:
os.system('''ps axf | grep xyz | grep -v grep | awk '{print "kill " $1 }' | sh''')
Alternatively, open the process with Popen(...).pid and then use os.kill()
my_pid = Popen('/home/rolf/test1.sh',).pid
os.kill(int(my_pid), signal.SIGKILL)
Remember to include a shebang in your script (#!/bin/sh)
Edit:
On second thoughts, perhaps
os.kill(int(my_pid), signal.SIGTERM)
is probably a better way to end the process, it at least gives the process the chance to close down gracefully.
I want to kill python interpeter - The intention is that all the python files that are running in this moment will stop (without any informantion about this files).
obviously the processes should be closed.
Any idea as delete files in python or destroy the interpeter is ok :D (I am working with virtual machine).
I need it from the terminal because i write c code and i use linux commands...
Hope for help
pkill -9 python
should kill any running python process.
There's a rather crude way of doing this, but be careful because first, this relies on python interpreter process identifying themselves as python, and second, it has the concomitant effect of also killing any other processes identified by that name.
In short, you can kill all python interpreters by typing this into your shell (make sure you read the caveats above!):
ps aux | grep python | grep -v "grep python" | awk '{print $2}' | xargs kill -9
To break this down, this is how it works. The first bit, ps aux | grep python | grep -v "grep python", gets the list of all processes calling themselves python, with the grep -v making sure that the grep command you just ran isn't also included in the output. Next, we use awk to get the second column of the output, which has the process ID's. Finally, these processes are all (rather unceremoniously) killed by supplying each of them with kill -9.
pkill with script path
pkill -9 -f path/to/my_script.py
is a short and selective method that is more likely to only kill the interpreter running a given script.
See also: https://unix.stackexchange.com/questions/31107/linux-kill-process-based-on-arguments
You can try the killall command:
killall python
pgrep -f <your process name> | xargs kill -9
This will kill the your process service.
In my case it is
pgrep -f python | xargs kill -9
pgrep -f youAppFile.py | xargs kill -9
pgrep returns the PID of the specific file will only kill the specific application.
If you want to show the name of processes and kill them by the command of the kill, I recommended using this script to kill all python3 running process and set your ram memory free :
ps auxww | grep 'python3' | awk '{print $2}' | xargs kill -9
to kill python script while using ubuntu 20.04.2 intead of Ctrl + C just push together
Ctrl + D
I have seen the pkill command as the top answer. While that is all great, I still try to tread carefully (since, I might be risking my machine whilst killing processes) and follow the below approach:
First list all the python processes using:
$ ps -ef | grep python
Just to have a look at what root user processes were running beforehand and to cross-check later, if they are still running (after I'm done! :D)
then using pgrep as :
$ pgrep -u <username> python -d ' ' #this gets me all the python processes running for user username
# eg output:
11265 11457 11722 11723 11724 11725
And finally, I kill these processes by using the kill command after cross-checking with the output of ps -ef| ...
kill -9 PID1 PID2 PID3 ...
# example
kill -9 11265 11457 11722 11723 11724 11725
Also, we can cross check the root PIDs by using :
pgrep -u root python -d ' '
and verifying with the output from ps -ef| ...
I used nohup python *.py & to run my tornado web service, and 8 processes started.
.
However, it is really annoying killing my tornado processes. I have to ues kill -3 pid 8 times to finally turn down my service. So I want to know how can I kill the 8 processes at one time in my bash? Thanks.
I tried killall python, but it is dangerous when there is other python process running.
#Viktor suggest me to use pkill -P <parent> and it works in my Ubuntu. But in Centos, 'pkill' doesn't work. So how can I 'pkill' the processes in Centos? Thanks.
This depends on your environment a bit.
But you may want to look into pkill -P <parent>, which kills everything with the same parent pid.
Maybe :
ps aux | grep -e 'python spam.py' | grep -v grep | awk '{print $2}' | xargs -i kill {}
Killing the parent process, that is the one with smallest PID should do the job, like kill -15 18054 in your ps example. Also, you could do some grep magic, like:
for i in `ps waux | grep "python spam.py" | awk '{ print $2 }'`; do kill -15 ${i}; done
I have a very long string ssh_cmd, I get it from
cmd = """kill -9 `ps -ef|grep "udp_receiver"|grep -v "grep"|awk '{print $2}'`"""
HostName="133.33.22.1"
ssh_cmd = """ssh -t inria_spoofing#{0} 'sudo nohup bash -c "{1} > /nohup.out 2>&1 &"'""".format(HostName, cmd)
the resulted ssh_cmd is:
ssh -t kitty#133.33.22.1 'sudo nohup bash -c "kill -9 `ps -ef|grep "udp_receiver"|grep -v "grep"|awk '{print $2}'` > /nohup.out 2>&1 &"'
however, I'm afraid when I run
child = pexpect.spawn(ssh_cmd)
there is problem,
so how to organize the string?
thanks!
To answer the question, here's the proper ssh_cmd: ssh -t kitty#133.33.22.1 "sudo nohup bash -c \"kill -9 \\\`ps -ef | grep 'udp_receiver' | grep -v 'grep' | awk '{print \\\$2}'\\\` > /nohup.out 2>&1 &\""
Basically, you need to escape double quotes, backticks and backslashes in a command each time you embed this command in another one. I did not use single quotes except at the lower level because you cannot use escaped single quotes inside single quotes.
You do need to escape the $ too when it is just a character inside a string quoted with double quotes, even if the string does also contain single quotes.