There are times when you need to find a running process on your machine.
Do you like to know how to do it? Ok, open a new terminal.
Find a running process
You already know how to list processes on your linux machine. In case you don’t know anything about that just run the following command on your console and see what is going to happen.
ps aux
But the above command is not useful when you are looking for a specific process and not the entire list. So the grep command comes in handy in this case. You can send the output of the ps aux command to the grep command and search for a process like shown in the following example.
ps aux | grep ipython
The ipython shell is not running yet on my machine so the above command gives only the following output on my console.
oltjano 8698 0.0 0.0 13600 944 pts/4 S+ 13:49 0:00 grep --color=auto ipython
And if i run ipython and do a ps aux | grep ipython again I get a different output which tells me that ipython is currently running.
Running ipython with the following command.
ipython
Then run ps aux | grep ipython.
ps aux | grep ipython
The following output is displayed on my console when running the above command.
oltjano 8709 6.0 0.4 149684 18592 pts/0 Sl+ 13:53 0:00 /usr/bin/python /usr/local/bin/ipython oltjano 8712 0.0 0.0 13600 944 pts/4 S+ 13:54 0:00 grep --color=auto ipython
So as you can see from the above output I am running the ipython located in /usr/local/bin with the process id of8709. There is also a simple trick I want to share with you guys. In order to stop picking the actual grep itself use the following syntax.
ps aux | grep '[i]python'
[i] is the trick used in the above command. Running the above command when ipython is a actually running gives only the following output which is different from the one produced by ps aux | grep ipython.
oltjano 8709 6.0 0.4 149684 18592 pts/0 Sl+ 13:53 0:00 /usr/bin/python /usr/local/bin/ipython
As you guys can see from the above output we do not pick the actual grep itself anymore. Less output, less stuff to watch, better results!
Now would you like to know how do I kill a running process by its id?
Kill a running process by pid
You can easily kill a running process if you know its pid by using the kill command like shown in the following example. Nothing hard here, but it is useful to know how to do it.
kill 8709
After running the above command you will not see the running process anymore which in my case is ipython. It is going to be terminated. The kill 8709 command is good, but I would like to show you guys a more useful command. In fact some commands that are grouped together using piping.
The following command extracts the process id of a process and kills it automatically for you.
kill $(ps aux | grep '[i]python' | awk '{print $2}')
The following is an explanation of the parts taking place in the above command.
First the ps command is run. It lists all the running processes. The output of the ps command is piped with the | to the grep command. As we already know the grep command filters the output based on the search string. In this case I am looking for ipython.
The output of the grep is then passed as input to the awk command which gives the second filed of each line. The second filed in this case is the pid.
oltjano 8709 6.0 0.4 149684 18592 pts/0 Sl+ 13:53 0:00 /usr/bin/python /usr/local/bin/ipython
In the above output we can see that the first field is oltjano and the second field is 8709 which is pid of the running process. Then we use the kill command to terminate the process or the processes if there is more than one pid.
Cool! We did not have to manually type the pid of the process to kill it.
It is time for some other commands. Now you know how to pickup a current specific running process and how to kill it by using the process id, the pid.
The tar command
The tar command is used to store and extract from a tape or disk archive. You probably have seen some files ending in .tar, you need to use the tar utility in order to get its contents.
For example the following command can be used to extract the contents of example.tar in a verbose way.
tar xvf example.tar
Let me explain the options being used in the above command to you guys. The v option used in here stands for verbose output which is very helpful if you want to know what is happening while you are extracting the files from archive. The f option stands for and the x option stands for extract which in other words means to get the files from the archive.
You can also view the files of an existing archive without extracting it files.
tar tvf example.tar
What about creating a new tar archive. In order to create a new tar archive you need to use different options than the ones you used when you did an extract.
First you need a directory with files in it. This is the directory that you are going to use to create a tar archive.
tar cvf example.tar example/
The c option stands for creating an archive. The v option is for verbose output and the f option is for file. Easy, isn’t it? Just remember that when you want to create a new archive you have to use the c option.
Reduce size of the files with gzip
The gzip utility is very useful when you want to reduce the size of files especially if you plan to share them over the network.
The following is a short description of the gzip tool take from the manual page.
Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times.(The default extension is -gz for VMS, z for MSDOS, OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if a file name is “-“, the standard input is compressed to the standard output. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.
Now lets stop
gzip test.txt
You can easily uncompress the file by using the same utility, gzip. To uncompress a *.gz file use the following syntax.
gzip -d test.txt.gz
As you can see we have the -d option present in the above command when decompressing a *.gz file. This option stands for decompress or uncompress.
Enough for today, shutdown your computer
I like to do everything from the command line when possible so why not shutdown the system from the console? What is the command? Do you guys want to do the same thing, or maybe use this command in the near future?
In case you want to power off immediately run the following command.
shutdown -h now
Want to shutdown after some time? Then use the following syntax.
shutdown -h + 13
The above command will make the system shutdown after 13 minutes.