I want to get the PID of a process namely "cron" by command line.
I tried the following script.
ps ax|grep 'cron'
but I am getting a part of a table,
1427 ? Ss 0:00 /usr/sbin/cron -f
24160 pts/5 S+ 0:00 grep --color=auto cron
How I extract the pid from this ?
The pgrep utility will return the process IDs for the currently running processes matching its argument:
$ pgrep cron
228
It may also be used to "grep for" things on the command line:
$ pgrep -f uerfale
69749
69752
$ pgrep -l -f uerfale
69749 slogin uerfale
69752 slogin: /home/kk/.ssh/sockets/uerfale-9022-kk.sock [mux] m
To kill a process by name, use pkill. It works in the same way as pgrep but will send a signal to the matched processes instead of outputting a process ID.
Just use pidof, rather to use other commands and apply post-processing actions on them.
$ pidof cron
22434
To make the command return only one PID pertaining to to the process, use the -s flag
-s
Single shot - this instructs the program to only return one pid.
Like this, for example:
ps -ef|grep 'cron'|grep -v grep|awk '{print $2}'
You can try this;
ps -o pid,sess,cmd afx | egrep "( |/)cron( -f)?$"
or
pstree -pas <cronPID>
Related
When I run a simple bash script, say myscript.sh
#!/bin/bash
sleep 30
from the terminal, and then do pgrep myscript.sh I don't get any result. Why?
You're probably doing this:
pgrep myscript.sh
This won't show the process you're running because it is /bin/bash that is running your script.
You should be doing:
pgrep -fl myscript.sh
To list your process.
As per man pgrep:
-f Match the pattern anywhere in the full argument string of the process instead of just the executable name.
Your just running your bash script, u need to use -f flag, please check man page
Check pgrep is installed in your machine. Just do man pgrep, if you get command not found that install the utility.
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match.
Example usage:
pgrep name | xargs kill
If you use pgrep name | kill, the ouput of pgrep name is feed to stdin of kill. Because kill does not read arguments from stdin, so this will not work.
Using xargs, it will build arguments for kill from stdin. Example:
$ pgrep bash | xargs echo
5514 22298 23079
I need to use command that shows all processes related to terminal. Ps -a looks good except that there is no username printed. This command prints:
PID TTY TIME CMD
26969 pts/34 0:00 man
27636 pts/2 0:00 awk
25215 pts/35 0:00 bash
I would like it to be similar to this:
PID TTY TIME CMD USER
26969 pts/34 0:00 man name
27636 pts/2 0:00 awk name
25215 pts/35 0:00 bash name
Columns order does not matter
Use:
ps a -o pid,tty,etime,cmd,user
From ps manual:
SIMPLE PROCESS SELECTION
a ... An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes
when used together with the x option.
STANDARD FORMAT SPECIFIERS
Here are the different keywords that may be used to control the output format (e.g. with option -o) or to sort the selected processes
with the GNU-style --sort
option.
For example: ps -eo pid,user,args --sort user
I have found that ps -af works the way I want
I'd like to kill a process/script with a simple command using. At the moment I do the following
ps -ef | grep myscriptname
kill 123456
But is there a way to maybe combine the 2 command together so I don't need to look and manually write the pid, something like this kill grep myscriptname?
You want pkill:
pkill myscriptname
On some systems there is a similar tool called killall, but be careful because on Solaris it really does kill everything!
Note that there is also pgrep which you can use to replace your ps | grep pipeline:
pgrep myscriptname
It prints the PID for you, and nothing else.
Another alternative is using the pidof command:
kill $(pidof processname)
you can try this simple trick
pkill -f "my_sript_filename"
I use kill $(pgrep <program name>), it works.
Another alternative, pgrep with xargs
ps aux | pgrep gitlab | xargs kill
An alternative is piping to the xargs command:
ps -ef | grep myscriptname | xargs kill
http://man7.org/linux/man-pages/man1/xargs.1.html
I made an alias for this function in order to kill processes in bash:
On my .bashrc file
kill_process(){
# $1 being a parameter for the process name
kill $(ps ax | grep "$1" | awk '{print $1}')
}
alias kill_process=kill_process
So, suppose I want to kill the meteor process:
Let's see all meteor processes:
ps aux | grep 'meteor' | awk '{print $2}'
21565
21602
21575
21546
Calling the kill_process function with the alias
kill_process meteor
bash: kill: (21612) - No such process
So, the kill_process function effectively terminates the meteor processes, but it's kill command looks for an inexistent pid. Notice the pid 21612 wasn't listed by ps aux | grep. Any ideas to improve the kill_process function to avoid this?
I think in your case the killall command would do what you want:
killall NAME
The standard way of killing processes by name is using killall, as Swoogan suggests in his answer.
As to your kill_process function, the grep expression that filters ps will match the very own grep process (you can see this running the pipeline without awk), but by the time kill is invoked, that process is no longer running. That's the message you see.
Each time you run the command, grep runs again with a new PID: that's the reason you can't find it on the list when you test it.
You could:
Run ps first, pipe it into a file or variable, then grep
Filter grep's PID out of the list
(Simpler) supress kill output:
kill $(...) 2>/dev/null
This command ps -ef | grep php returns a list of processes
I want to kill in one command or with a shell script all those processes
Thanks
The easiest way to kill all commands with a given name is to use killall:
killall php
Note, this only sends an interrupt signal. This should be enough if the processes are behaving. If they're not dying from that, you can forcibly kill them using
killall -9 php
The normal way to do this is to use xargs as in ps -ef | grep php | xargs kill, but there are several ways to do this.
ps -ef lists all processes and then you use grep to pick a few lines that mention "php". This means that also commands that have "php" as part of their command line will match, and be killed. If you really want to match the command (and not the arguments as well), it is probably better to use pgrep php.
You can use a shell backtick to provide the output of a command as arguments to another command, as in
kill `pgrep php`
If you want to kill processes only, there is a command pkill that matches a pattern to the command. This can not be used if you want to do something else with the processes though. This means that if you want to kill all processes where the command contain "php", you can do this using pkill php.
Hope this helps.
You can find its pid (it's on the first column ps prints) and use the kill command to forcibly kill it:
kill -9 <pid you found>
Use xargs:
ps -ef | grep php | grep -v grep | awk '{print $2}' | xargs kill -9
grep -v grep is exclude the command itself and awk gives the list of PIDs which are then passed kill command.
Use pkill php. More on this topic in this similar question: How can I kill a process by name instead of PID?