Ten very handy Linux commands to make your DevOps life easy

In this era of cloud computing, containers, serverless and being a cloud-native citizen there are still servers who need attention. If you are a full-stack developer, my humble opinion is part of your toolkit is know you way around Linux. This blog post is a collection of some very handy, ten to be precise, Linux command to make your DevOps life easy.

Although most servers are Linux-centric, you can apply those commands also to your developer box if you run MacOS.

Win-Win.

NOTE: At Cloud 66 we provision your servers with Ubuntu 16.04

1. Access the server

If your are running MacOS, open your terminal and run the command:

$ ssh [username]@[ip-address-server] -i [path_to_private_key.pem]

Or if you are using cx our toolbelt:

$ cx ssh -s [stack_name] [server_type]

NOTE: Don’t forget to open up your firewall is your are not using cx ssh

2. Superpowers

Having elevated privileges all the time is dangerous! You can easily f$%k-up your system. That is why you should use sudo every time you run a potentially dangerous command.

For example; Killing a running process can be dangerous. Use this following line with cautious.

$ sudo kill -9 [process_id]

When your run the sudo command Linux will run your command in another process and your environment variables are not preserved. To make sure you preserve the environment variables, use sudo -E

With great sudo power comes great responsibility.

3. No life without a package manager

Before you install new packages make sure the package manager is up-to-date, run the update command first.

$ sudo apt-get update

To install a package, like htop, run the following command:

$ sudo apt-get install htop

Upgrading your installed packages.

$ sudo apt-get install upgrade

Read the manual of apt-get to known all the very nice features of APT package manager.

Your server needs attention

4. Where did my diskspace go?

The aim is to make sure you are not overshooting the 80% threshold. If you exceed the threshold it’s time to scale or clean-up the mess, because running out of resources you have the change your application show some fickle behavior.

To check in a human readable format:

$ sudo df -h

But in most cases, you want to check which part of your system is consuming lots of disk space. Use the following command:

$ sudo du -h -d 1 /var/

You need to use du recursively to pin-point the directory which causing the most disk usage. After running du -h -d 1 /var/ maybe a subdirectory like /var/deploy using the most space. For inspection, run:

$ sudo du -h -d 1 /var/deploy

Or change the depth of recursing to 2, run:

$ sudo du -h -d 2 /var/

5. Who eat all my resources?

A really nice tool which you can help to see how your process are consuming memory, cpu and which cores are under pressure, use htop.

Just run:

$ sudo htop

Hit the F5 key to show all process in a tree view, to debug the subprocesses.

6. PID to the rescue

$ sudo ps aux

  • a = show processes for all users
  • u = display the process’s user/owner
  • x = also show processes not attached to a terminal

7. Show me the contents

$ tail -f /var/log/somelog.log

-f option will follow the file and when the file grows if will append to the console output.

To output a file or concatenate some files for outputting to the console, use cat

$ cat /var/log/somelog.log

8. Filter crap with grep

In Linux, you can combine two things to make life easier. A piece of pipe | and some grep. A | in Linux will send the output of the left-hand command to the right-hand command. You can create pipelines of commands to achieve your goal.

To show only the request with a HTTP 404 status codes:

$ sudo tail -f /var/log/somelog.log | grep 404

Getting the PID (process id) of a Ruby process:

$ sudo ps aux | grep ruby

grep is a very powerful tool and can filter/match lots of things to help you. Read the manual of grep to level-up your Linux skills.

9. Containers are using resources too.

$ sudo docker stats $(docker ps | awk '{if(NR>1) print $NF}')

Yes! This command shows the CPU load, memory usage and network utilization.

You can see some serious pipelining with awk. awk is a programming language designed for text processing and typically used for data extraction and reporting tool. It is a standard feature of most Unix-like operating systems.

10. Star Wars and some fortune

$ telnet towel.blinkenlights.nl

or you want some random fortune?

$ sudo apt-get install fortune

$ fortune

Happy coding and love Linux commands.

Originally published at blog.cloud66.com on February 7, 2017.

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: www.cloud66.com