0

I'm trying to kill some process in ubuntu 18.04 for which I am using pkill command. But I am to able to suppress Killed message for some reason.
Here is process which are running.

# ps -a
    PID TTY          TIME CMD
   2346 pts/0    00:00:00 gunicorn
   2353 pts/0    00:00:00 sh
   2360 pts/0    00:00:00 gunicorn
   2363 pts/0    00:00:00 gunicorn
   2366 pts/0    00:00:00 ps

My attempts to kill the process and supressing logs

# 1st attempt
# pkill -9 gunicorn 2>&1 /dev/null
pkill: only one pattern can be provided
Try `pkill --help' for more information.

#2nd attempt (This killed process but got output `Killed` and have to press `enter` to get into command line)
# pkill -9 gunicorn > /dev/null
root@my-ubuntu:/# Killed

#3rd attempt(behavior similar to previous attempt)
# pkill -9 gunicorn 2> /dev/null
root@my-ubuntu:/# Killed

root@my-ubuntu:/#

What is it that I am missing?

Pranjal Doshi
  • 862
  • 11
  • 29
  • redirection may be `>/dev/null 2>&1`, which means `1>/dev/null 2>&1` , as written `2>&1 /dev/null`, `/dev/null` is taken as an argument (also note that redirection order is important). Otherwise `>& /dev/null` – Nahuel Fouilleul Jul 08 '21 at 07:29
  • @NahuelFouilleul I tried `pkill -9 gunicorn >& /dev/null` as well. But not able to eliminate `Killed` message. – Pranjal Doshi Jul 08 '21 at 07:33
  • related : https://stackoverflow.com/questions/81520/how-to-suppress-terminated-message-after-killing-in-bash and https://stackoverflow.com/questions/5719030/bash-silently-kill-background-function-process/5722850 in a subshell `( exec >&/dev/null ; pkill ... )` or current shell `exec >&/dev/null ; pkill ...; exec >&/dev/tty` – Nahuel Fouilleul Jul 08 '21 at 07:39
  • btw, it is better idea to configure gunicorn to create pid file (add option `-p file.pid`) and then kill it by pid from that file – mvp Jul 08 '21 at 07:58

2 Answers2

2

I think you want this syntax:

pkill -9 gunicorn &>/dev/null

the &> is a somewhat newer addition in Bash ( think 4.0 ??) that is a shorthand way of redirecting both stdout and stderr.

Also, are you running pkill from the same terminal session that gunicorn was started on? I don't think pkill prints a message like "Killed" which makes me wonder if that is coming from some other process....

You might be able to suppress it by running set +m in the terminal (to disable job monitoring). To reenable, run set -m.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • `gunicorn` was wokring as background process. And I'm getting `Killed` even with `# pkill -9 gunicorn &>/dev/null` and as mentioned I've multiple instance of `gunicorn` running. That might be the reason. – Pranjal Doshi Jul 08 '21 at 08:09
  • Yep, that is what is happening. Either gunicorn or (more likely) the shell is producing that output (reason I doubt it's gunicorn is because you are using `pkill -9` which should not allow the process the opportunity to write any messages) – Z4-tier Jul 08 '21 at 08:21
  • Yeah. one thing I observed is when I am killing process using `kill -9 $(ps ax | grep "gunicorn" | awk '{print $1}') 2> /dev/null` I am not getting killed message when rediercting stderr. – Pranjal Doshi Jul 08 '21 at 08:29
  • I added an update to this answer that I think will fix it. Bash *does* produce a "Killed" message for background processes, but `set +m` will suppress that notification (notice the "plus" before the `m` and not a `-`) – Z4-tier Jul 08 '21 at 08:38
0

I found the only way to prevent output from pkill was to use the advice here: https://www.cyberciti.biz/faq/how-to-redirect-output-and-errors-to-devnull/

command 1>&- 2>&-

This closes stdout/stderr for the command.

CodingFrog
  • 1,225
  • 2
  • 9
  • 17