How to kill results from ps & grep

from: https://unix.stackexchange.com/questions/30759/whats-a-good-example-of-piping-commands-together

Problem

Let’s say the command conky stopped responding on my desktop, and I want to kill it manually. I know a little bit of Unix, so I know that what I need to do is execute the command kill <PID>. In order to retrieve the PID, I can use ps or top or whatever tool my Unix distribution has given me. But how can I do this in one command?

Answer

$ ps aux | grep conky | grep -v grep | awk '{print $2}' | xargs kill

DISCLAIMER: This command only works in certain cases. Don’t copy/paste it in your terminal and start using it, it could kill processes unsuspectingly. Rather learn how to build it.

How it works

1- ps aux

This command will output the list of running processes and some info about them. The interesting info is that it’ll output the PID of each process in its 2nd column. Here’s an extract from the output of the command on my box:

$ ps aux
 rahmu     1925  0.0  0.1 129328  6112 ?        S    11:55   0:06 tint2
 rahmu     1931  0.0  0.3 154992 12108 ?        S    11:55   0:00 volumeicon
 rahmu     1933  0.1  0.2 134716  9460 ?        S    11:55   0:24 parcellite
 rahmu     1940  0.0  0.0  30416  3008 ?        S    11:55   0:10 xcompmgr -cC -t-5 -l-5 -r4.2 -o.55 -D6
 rahmu     1941  0.0  0.2 160336  8928 ?        Ss   11:55   0:00 xfce4-power-manager
 rahmu     1943  0.0  0.0  32792  1964 ?        S    11:55   0:00 /usr/lib/xfconf/xfconfd
 rahmu     1945  0.0  0.0  17584  1292 ?        S    11:55   0:00 /usr/lib/gamin/gam_server
 rahmu     1946  0.0  0.5 203016 19552 ?        S    11:55   0:00 python /usr/bin/system-config-printer-applet
 rahmu     1947  0.0  0.3 171840 12872 ?        S    11:55   0:00 nm-applet --sm-disable
 rahmu     1948  0.2  0.0 276000  3564 ?        Sl   11:55   0:38 conky -q

2- grep conky

I’m only interested in one process, so I use grep to find the entry corresponding to my program conky.

$ ps aux | grep conky
 rahmu     1948  0.2  0.0 276000  3564 ?        Sl   11:55   0:39 conky -q
 rahmu     3233  0.0  0.0   7592   840 pts/1    S+   16:55   0:00 grep conky

3- grep -v grep

As you can see in step 2, the command ps outputs the grep conky process in its list (it’s a running process after all). In order to filter it, I can run grep -v grep. The option -v tells grep to match all the lines excluding the ones containing the pattern.

$ ps aux | grep conky | grep -v grep
 rahmu     1948  0.2  0.0 276000  3564 ?        Sl   11:55   0:39 conky -q

NB: I would love to know a way to do steps 2 and 3 in a single grep call.

4- awk '{print $2}'

Now that I have isolated my target process. I want to retrieve its PID. In other words I want to retrieve the 2nd word of the output. Lucky for me, most (all?) modern unices will provide some version of awk, a scripting language that does wonders with tabular data. Our task becomes as easy as print $2.

$ ps aux | grep conky | grep -v grep | awk '{print $2}'
 1948

5- xargs kill

I have the PID. All I need is to pass it to kill. To do this, I will use xargs.

xargs kill will read from the input (in our case from the pipe), form a command consisting of kill <items> (<items> are whatever it read from the input), and then execute the command created. In our case it will execute kill 1948. Mission accomplished.

Advertisements

How to install JDK8 or JDK9 on Ubuntu

from: https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04

  • sudo add-apt-repository ppa:webupd8team/java
  • sudo apt-get update
  • sudo apt-get install oracle-java8-installer
  • sudo update-alternatives –config java

 

From: https://github.com/franzwong/til/blob/master/java/silent-install-oracle-jdk8-ubuntu.md

Silent install Oracle JDK 8 on Ubuntu

Problem: During installation of Oracle JDK 8 on Ubuntu, we need to agree on license agreement manually.

Solution: Installing Java Automatically (With Silent Option)

Shell script:

sudo apt-get install -y python-software-properties debconf-utils
sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
sudo apt-get install -y oracle-java8-installer

Ansible:

---
- name: Add apt repository
  apt_repository:
      repo: 'ppa:webupd8team/java'
      state: present
      update_cache: yes

- name: Accept license
  debconf:
    name: 'oracle-java8-installer'
    question: 'shared/accepted-oracle-license-v1-1'
    value: 'true'
    vtype: 'select'

- name: Install java
  apt: name={{ item }} state=present
  with_items:
      - oracle-java8-installer
      - ca-certificates
      - oracle-java8-set-default

Change timezone in CentOS

from: https://www.vultr.com/docs/setup-timezone-and-ntp-on-centos-6

Input the following command in your terminal:

date

As you see, the Vultr CentOS 6 x64 OS uses the UTC time by default. You can modify it to any time zone as you wish, but using the local timezone of the server’s physical location is a best practice.

If our server was running in China, then we would use the “Asia/Shanghai” time zone:

rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/America/Torono /etc/localtime

You can navigate to the directory /usr/share/zoneinfo to find the appropriate time zone. There is an excellent resource on Wikipedia for timezone listings.

Input date again, you will find that the local system time has changed to CST (China Standard Time) GMT+0800.

Next, we will write the system time info into the hardware clock.

vi /etc/sysconfig/clock

Modify the content of this file as below.

ZONE="America/Toronto"
UTC=false
ARC=false

Save and quit.

:wq

Write the system time into the hardware clock.

hwclock --systohc --localtime

Input hwclock to see the result.