1

I often need to check if a process is running, so I use one of these:

ps aux | grep myprocess
ps -Fe | grep myprocess
top
pgrep myprocess (only shows the PID)
pkill myprocess (if I want to kill it)

All of the above commands work well but, is there a shorter command to do this? Any answer is appreciated, but the chosen one needs to be...

  • A built in solution, as I work with many different devices.
  • Enables to use a pattern.
  • Gives you similar information than ps aux.
  • Shorter than what we already know.

Thanks

  • is above commands are hard to understand? Why you need a shorter one? – Avinash Raj Jun 19 '14 at 9:53
  • No, I understand them. Because I use them over 20 times a day. – Katu Jun 19 '14 at 10:01
  • How much shorter than pgrep myprocess do you need to get? What Doesn't that do that you need? – Oli Jun 19 '14 at 10:02
  • It only gives me the PID by default. Is not much information. A command of that length with the info that ps aux gives you would be ideal :) – Katu Jun 19 '14 at 10:07
4

I think what you're asking for doesn't exist so why not write a little bash function or script that does exactly what you want?

function p {
    ps aux | awk -v s="$@" 'NR>1 && $11~s'
}

Stick that in your ~/.bash_functions (or wherever is called by ~/.bashrc) and call source ~/.bashrc to reload it and you should be able to run:

$ p firefox
oli       5992 11.2  4.2 2856240 1044104 ?     Sl   Jun17 313:56 /usr/lib/firefox/firefox

The expression will take a regex which makes it doubly handy. And p on its own will give you a full listing.

  • @AvinashRaj Not with the $@ argument IIRC. – Oli Jun 19 '14 at 10:26
3

Using alias & grep to filter out its own terminal process :

alias qp="ps aux | grep -E -v \"tty.*grep|pts.*grep\" | grep -m1"

If you want last/newest process:

alias qp="ps ax | tac | grep -E -v \"tty.*grep|pts.*grep\" | grep -m1"
0

It give the process id if running and not if not running

pidof process

  • Thanks but I would like to be able to use a pattern and also get more info than the pid – Katu Jun 20 '14 at 13:02
0

The other answers are very good and basically wrap up your ad hoc solution of ps | grep into a shell alias or function. There is nothing wrong with that approach. Note, however, that this means ps will be extracting information for all running processes and then you have awk or grep filtering out the lines of interest.

I would propose that it's slightly more efficient to use pgrep to get a list of PIDs (as you show in your question), and use ps to get the output format you want for only the PIDs matched by pgrep.

Simple process match, POSIX-style full listing

pg() {
    pids=`pgrep -d, "$1"`
    [ "$pids" ] && ps -f -p "$pids"
}

pg myprocess

This shows a full ps listing for the matched process names. This function definition can be added to ~/.bashrc to always be defined in your interactive shell. This can also be modified in a few different ways to change the output format or to match full command lines instead of just the process name.

Simple process match, BSD-style user listing

pg() {
    pids=`pgrep -d, "$1"`
    [ "$pids" ] && ps up "$pids"
}

Full command-line match, BSD-style job control listing

pg() {
    pids=`pgrep -f -d, "$1"`
    [ "$pids" ] && ps jp "$pids"
}

Full command-line match, POSIX-style long listing

pg() {
    pids=`pgrep -f -d, "$1"`
    [ "$pids" ] && ps -l -p "$pids"
}

Note the -f option on pgrep in the last two examples to match on the full command line. You can alter these examples to suit your needs, the important part being that the p or -p option is given with the list of PIDs found by pgrep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.