I am trying to create a script that kills an Xorg process.

Here is an example of me running the commands in a terminal:

  • First, I find the process id of the Xorg program (in this case, 2494):


lucas@lucas-ThinkPad-W520:~$ ps ax  | grep Xorg 
 2494 ?        Ss     6:53 Xorg :8 -config /etc/bumblebee/xorg.conf.nvidia -configdir /etc/bumblebee/xorg.conf.d -sharevts -nolisten tcp -noreset -verbose 3 -isolateDevice PCI:01:00:0 -modulepath /usr/lib/nvidia-331/xorg,/usr/lib/xorg/modules
 4247 pts/1    S+     0:00 grep --color=auto Xorg
  • Then I want to kill Xorg's process:


lucas@lucas-ThinkPad-W520:~$ sudo kill -15 2494

How can I wrap this functionality into a script? Basically, get the 2494 and insert it as an argument into the sudo kill -15 command.

  • A simple solution is pkill -15 Xorg if you've got pgrep/pkill. Mar 2 '14 at 13:13

Note that there already is a command that does what you want to do : killall -15 Xorg.

You can also do kill -15 $(pidof Xorg).

For your script, you can use ps aux | grep Xorg | grep -v grep | awk '{print $2}' as suggested by @Adionditsak or ps ax | grep Xorg | grep -v grep | awk '{print $1}' (without option 'u' in ps).

  • grep -v grep is good to include. Thanks for input. And yeah ps aux | grep Xorg | grep -v grep | awk '{print $2}' is pretty much what is happening in pidof aswell. Just like the piped version. Mar 2 '14 at 13:01
  • As always, there is more than one way to do it. :-) I tend to prefer to use short commands though, as parsing the output of commands is usually a good way to introduce strange bugs in scripts.
    – lgeorget
    Mar 2 '14 at 13:02
  • This is helpful. What is grep -v grep? (I couldn't find an answer in the man or google). Also, will Adionditsak's answer kill more than one process, since $xorg_process is equal to the pid's of more that one process?
    – modulitos
    Mar 2 '14 at 13:28
  • 2
    It's much easier to do ps aux | grep [X]org | awk '{print $2}' and not have to have the extra grep. And as already pointed out in other answers, there are much better utilities such as pkill and killall that do the job by matching the command name (and not the command arguments).
    – Drav Sloan
    Mar 2 '14 at 13:32
  • 1
    From man grep: ` -v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) ` It's useful not to include grep itself in the results of ps. Yes, Adionditsak's solution may kill more than one process.
    – lgeorget
    Mar 2 '14 at 13:32

Get the PID of the process in row 2 (awk '{print $2}'), and set in a variable. Then kill it. Something like this?

xorg_process=$(ps aux | grep 'Xorg' | awk '{print $2}')
kill -15 $xorg_process
  • 1
    There is no need for awk here, just use -o with ps to output the command name. Eg 'ps -eo comm. Also using -e` is more portable that the BSD syntax for printing all processes (and shorter).
    – Graeme
    Mar 2 '14 at 13:23
  • This is the old style method. I miss also a grep -v grep after 'Xorg'
    – user55518
    Mar 2 '14 at 17:19

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.