Making easy commands hard because we can learn something about Linux in the process!
Question:
How do we see how many days the Linux server has been up?
Easy Answer:
Hard Answer:
What did we learn here? (#1)
We learned there is a file in this /proc filesystem called uptime. What the hell is /proc and why is it keeping track of my uptime?
The /proc filesystem is like an x-ray into the several parts of the Linux system! Many utilities you use to get the current state of your Linux system actually read the information from proc!
If you cat /proc/uptime it will return two numbers separated by a space.
The first number is the total amount of seconds the system has been up. Hence the fact that I divided it by 86,400 in the command I issued. There are 86,400 seconds in each day!
The second number is how many seconds the core system was idle since it has been up. Obviously, the second number should always been smaller than the first.
What did we learn here? (#2)
We learned that the awk command is one of the most powerful command line tools on the face of the planet for a budding young Linux Ninja.
As any command line Linux user knows. Some commands you run will give you all kinds of information you are not interested in. (for instance, how many seconds the core system has been idle! ) Sometimes, you need to filter that (in a French accent) gar-baaage. Awk to the rescue!
When you feed data to awk, one of the great features it has is the ability to grab and print out a specific piece of text that you're interested in. By default, the delimiter is a space and the variables to use are named with a $ followed by the index of the piece of data you want to print. In our case, we want the first one as it's uptime in seconds. So, we will tell awk to print $1. Though, we don't want to stop there. We want to use awk to do some math! So we print the data we want and then tell awk to also divide it by the total seconds in a day!
The downside is it gives you a decimal with many numbers after the decimal point. Way more than you need!
Yeah, definitely need to trim that down a bit. Lets do this!
What did we learn here? (#3)
While the command line doesn't show you this. The printf command does not like you piping data to it! It prefers you provide the variables after you specify the format!
Example:
If you try to pipe Dave into printf, it ignores you like when you ask your wife when dinner will be on the table.
What did we learn here? (#4)
Well, then how was I able to pipe the seconds into printf?
Well, we learned that the xargs command is your friend!
xargs is what gave Superman his super powers! Due to the craziness of what xargs is, I will just quote from the man page.
In layman's terms. It lets me perform magic! I run xargs and then put my command after it. Since I piped in the variable, it includes that variable that was piped in as if it was an argument pasted to it! Yeah, MAGIC!!!
Example: I will echo the path to my home directory to xargs running the command ls.
That's some Linux Command Line Ninja stuff right there!
Now, lets put it all together and get how many days my pivpn server has been up!
Take Away
Yes, this was the hard way to get your Linux machine's uptime, but that wasn't the point. The point is learning the power that is at your finger tips while you are at the command line. There is a reason Microsoft has started to mimic and even implement Linux within their own environment. Have you ever try to do something like this at the DOS prompt? lol
I hope someone learns something here and it's not how to get your uptime the hard way. It's to open your mind to fun it can be just to figure things like this out. If your a puzzle guy or just love a challenge. The Linux command line is full of puzzles and challenges.
Enjoy!
Question:
How do we see how many days the Linux server has been up?
Easy Answer:
Code:
pi@pivpn:~ $ uptime
20:57:11 up 25 days, 23:07, 2 users, load average: 0.00, 0.02, 0.00
Code:
pi@pivpn:~ $ cat /proc/uptime | awk '{print $1 / 86400}' | xargs printf "%.2f days\n"
25.97 days
What did we learn here? (#1)
We learned there is a file in this /proc filesystem called uptime. What the hell is /proc and why is it keeping track of my uptime?
The /proc filesystem is like an x-ray into the several parts of the Linux system! Many utilities you use to get the current state of your Linux system actually read the information from proc!
If you cat /proc/uptime it will return two numbers separated by a space.
Code:
pi@pivpn:~ $ cat /proc/uptime
2244412.34 8841072.57
pi@pivpn:~ $
The second number is how many seconds the core system was idle since it has been up. Obviously, the second number should always been smaller than the first.
What did we learn here? (#2)
We learned that the awk command is one of the most powerful command line tools on the face of the planet for a budding young Linux Ninja.
As any command line Linux user knows. Some commands you run will give you all kinds of information you are not interested in. (for instance, how many seconds the core system has been idle! ) Sometimes, you need to filter that (in a French accent) gar-baaage. Awk to the rescue!
When you feed data to awk, one of the great features it has is the ability to grab and print out a specific piece of text that you're interested in. By default, the delimiter is a space and the variables to use are named with a $ followed by the index of the piece of data you want to print. In our case, we want the first one as it's uptime in seconds. So, we will tell awk to print $1. Though, we don't want to stop there. We want to use awk to do some math! So we print the data we want and then tell awk to also divide it by the total seconds in a day!
Code:
awk '{print $1 / 86400}'
The downside is it gives you a decimal with many numbers after the decimal point. Way more than you need!
Code:
pi@pivpn:~ $ cat /proc/uptime | awk '{print $1 / 86400}'
25.9842
pi@pivpn:~ $
What did we learn here? (#3)
While the command line doesn't show you this. The printf command does not like you piping data to it! It prefers you provide the variables after you specify the format!
Example:
Code:
pi@pivpn:~ $ printf "%s is teaching dumb Linux command line tricks\n" Dave
Dave is teaching dumb Linux command line tricks
pi@pivpn:~ $
If you try to pipe Dave into printf, it ignores you like when you ask your wife when dinner will be on the table.
Code:
pi@pivpn:~ $ echo Dave | printf "%s is teaching dumb Linux command line tricks\n"
is teaching dumb Linux command line tricks
pi@pivpn:~ $
What did we learn here? (#4)
Well, then how was I able to pipe the seconds into printf?
Well, we learned that the xargs command is your friend!
xargs is what gave Superman his super powers! Due to the craziness of what xargs is, I will just quote from the man page.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.
In layman's terms. It lets me perform magic! I run xargs and then put my command after it. Since I piped in the variable, it includes that variable that was piped in as if it was an argument pasted to it! Yeah, MAGIC!!!
Example: I will echo the path to my home directory to xargs running the command ls.
Code:
pi@pivpn:~ $ echo "/home/pi" | xargs ls
Desktop Documents Downloads Go Music Pictures Projects Public Templates Videos oldconffiles python_games
pi@pivpn:~ $
That's some Linux Command Line Ninja stuff right there!
Now, lets put it all together and get how many days my pivpn server has been up!
Code:
pi@pivpn:~ $ cat /proc/uptime | awk '{print $1 / 86400}' | xargs printf "%.2f days\n"
25.99 days
pi@pivpn:~ $
Take Away
Yes, this was the hard way to get your Linux machine's uptime, but that wasn't the point. The point is learning the power that is at your finger tips while you are at the command line. There is a reason Microsoft has started to mimic and even implement Linux within their own environment. Have you ever try to do something like this at the DOS prompt? lol
I hope someone learns something here and it's not how to get your uptime the hard way. It's to open your mind to fun it can be just to figure things like this out. If your a puzzle guy or just love a challenge. The Linux command line is full of puzzles and challenges.
Enjoy!