Why Python Prints Nothing When Piped to tee

Recently I wrote a Slack bot in Python. Being lazy, I just print the log messages directly and save them to a file with the tee command. That way I can see the bot’s activity in the console and keep the same information in a file. But when I combined Python’s print() with tee, I found that the log file often had no content, or messages that should have appeared showed up much later. After some research, I learned that Python’s print function checks where its output is going. If the output goes to a shell, it is written immediately. If it doesn’t, the output is stored in a buffer and only written out once the buffer is full. So when you use Python with tee, you run into this buffering problem, and the expected output doesn’t show up on the screen or in the file in time. ...

June 16, 2018 · 2 min

bash: warning: setlocale: LC_ALL: cannot change locale

Seeing the error “-bash: warning: setlocale: LC_ALL: cannot change locale” when you log in to a remote server over SSH? You run into it because when SSH connects to a remote host, it automatically sends a copy of your local LC_* variables (the locale) to the remote host. If the server doesn’t support the locale you specified, you’ll get the error “-bash: warning: setlocale: LC_ALL: cannot change locale”! ...

February 22, 2017 · 1 min

When GitLab Deleted Its Database by Mistake, How Do We Keep the rm Command Under Control?

We have all heard the story of someone accidentally deleting the root directory as a joke, but don’t think it is just a joke! I have personally seen countless friends’ computers suffer this kind of disaster. Today, the engineers at GitLab (a service similar to GitHub) also demonstrated the immense power of the rm -rf command… They wiped out the database on the production server… I always thought this kind of disaster would never happen to me, but now even a service like GitLab, which has tens of thousands of users, has had it happen through carelessness. That made me look into how to prevent it, so let’s talk about how to keep the rm command under control! ...

February 1, 2017 · 4 min

Generate Large File on Linux/macOS

Sometimes you need to generate a file of a specific size for testing network speed, disk read/write performance, or program performance. The first thing that comes to mind is probably dd, right? But when you generate files with dd, don’t you find it slow? Also, did you know that besides dd, you can also use truncate and fallocate? This post briefly explains the methods you can use to generate a large file, how to use them, and the differences between them. Note that the contents of the files generated here are meaningless. ...

January 27, 2017 · 3 min

How to Measure a Command's Execution Time in Terminal?

Have you ever wanted to measure how long the commands you run in Terminal take? Recently, because of a work requirement, I put together a file-sorting tool out of various commands. Performance requirements were strict and it had to finish within a specified time, so I looked into the handy time command, which can measure how long a command takes to run. ...

January 27, 2017 · 1 min

Linux Grep Tips - Finding Lines That Contain a String

On Linux, we sometimes need to quickly search a file for the “lines” that contain a keyword. For example, while reading a log, you might want to find the errors that happened at a certain time. Opening the file and scanning it line by line is not very efficient. That’s when you need grep! ...

January 20, 2017 · 2 min

How Can I Check That My Crontab Is Working Correctly?

Have you ever had this problem? When you first learn crontab, you are never quite sure whether your settings are correct. After each change, you wonder whether the job will actually run the way you expect. Here I share two ways to confirm that your crontab is set up correctly! ...

January 20, 2017 · 2 min

How to Hook SSH Login and Send Notifications (Email or Others)

When you first log in to Google or Facebook, they send you a notification email to improve security. I think many IT people wonder, “How can I do the same thing for SSH?” If every SSH login sends a notification to the system manager, that helps security. ...

July 30, 2016 · 2 min

Compiling and Installing a PHP Extension Manually (Linux)

If you want to install an extension for PHP, what should you do? Many people will answer Pecl. That is indeed a very convenient way to install extensions. But what if your server does not have Pecl? And what if, for some reason, you cannot install Pecl? Then you have no choice but to compile and install the extension manually~ This post shows you how to manually compile a PHP extension on Linux. ...

January 26, 2012 · 2 min

How to Manually Install Python on Linux Without Breaking yum

I previously wrote a program that automatically backs up important data on Linux and uploads it to Google Storage. It was written in Python 2.7, but the Python built into Linux is 2.4, and even if you upgrade with yum, the newest version it gives you is 2.4. So if you want a newer version of Python, you have to compile it yourself. Below, I’ll use Python 3.2 as the example. ...

August 19, 2011 · 2 min