Archive

Archive for the ‘bash’ Category

limit the CPU usage of Firefox / Dropbox / etc.

June 24, 2016 Leave a comment

Problem
I have an older dual core laptop where Firefox sometimes uses 120%-130% CPU and slows down the machine completely. Restarting Firefox solves the problem for a few minutes but then again, it eats up the CPU. What to do?

Solution
I don’t have many tabs open but I still have this problem. I also uninstalled the Flash plugin but it didn’t solve the problem.

However, I found a nice tool called cpulimit:

Cpulimit is a tool which limits the CPU usage of a process (expressed in percentage, not in CPU time). It is useful to control batch jobs, when you don’t want them to eat too many CPU cycles. The goal is prevent a process from running for more than a specified time ratio. It does not change the nice value or other scheduling priority settings, but the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly. The control of the used CPU amount is done sending SIGSTOP and SIGCONT POSIX signals to processes. All the children processes and threads of the specified process will share the same percentage of CPU.” (from the README of the project)

The following setting worked for me:

$ cpulimit -l 80 firefox

Firefox uses several threads but as mentioned in the documentation, they will will share the same percentage of CPU.

The CPU usage may jump higher than the specified value, but cpulimit will push it back in a few seconds.

My old laptop has become useable again :)

Update (with Dropbox)
I noticed that Dropbox also loves my CPU. Here is how I could limit this greedy beast. Originally, I started “$HOME/.dropbox-dist/dropboxd” automatically at each startup. Create the file “$HOME/bin/cpulimit_dropboxd.sh” with the following content:

#!/usr/bin/env bash

cpulimit -l 50 $HOME/.dropbox-dist/dropboxd

Make it runnable (chmod u+x cpulimit_dropboxd.sh) and call this script (cpulimit_dropboxd.sh) when your system comes up. Here I give 50% CPU for Dropbox but you can play with that value.

Categories: bash, firefox Tags: ,

email notification from a script

June 15, 2016 Leave a comment

Problem
I have a Digital Ocean VPS box where several scripts are running. Some of them run for a day. I would like to get an email notification when a particular script starts / ends, or when something happens.

In short: how to send an email from the command line?

Solution
First, do the necessary configuration to be able to send emails from the command line (more details here).

Sending email without a body:

mailx -s "subject" < /dev/null "to@email.com" 2>/dev/null

Sending email with a body:

echo 'this is the body of the email' | mailx -s "subject" "to@email.com" 2>/dev/null

I also made a Python wrapper for it that you can find here.

Categories: bash, python Tags: ,

untruncated output of “ps”

Problem
You must have noticed that when you use the Unix command “ps“, the output’s length in truncated to fit the screen’s width. How to get the full output?

Note: Instead of “ps” I usually use “ps aux“.

Solution
Use the “ww” options too:

ps auxww

Are you interested in one particular PID?

ps ww PROCESS_PID

(This is another thing that bugged me for years but a quick Google search enlightened me.)

Categories: bash Tags: , ,

compile and try a Go project on GitHub

Problem
I found an interesting Go project on GitHub (https://github.com/ichinaski/pxl) that I wanted to try. How to compile it?

(This project “pxl” can display images in the terminal).

Solution

GOBIN=$(pwd) GOPATH=/tmp/gobuild go get github.com/ichinaski/pxl

Under Manjaro I had to install the package “gc”, which contains the official Go compiler.

Categories: bash Tags: , ,

redis: list all the keys

Problem
You want to list all the keys in your Redis database. Or, you want to list just some of them.

Solution
See the documentation here. Example:

$ redis-cli
127.0.0.1:6379> KEYS jabba*
1) "jabba:cnt1"
2) "jabba:cnt2"
127.0.0.1:6379> KEYS *
1) "jabba:cnt1"
2) "jabba:cnt2"
3) "others:counter"
Categories: bash Tags: ,

how to configure a Digital Ocean (or Linode, etc.) Ubuntu box

March 21, 2016 Leave a comment

I wrote a doc about it on GitHub: https://github.com/jabbalaci/DigitalOceanNotes . Following this guide I can set up a virtual private server (VPS) in 30-40 minutes.

tmux: a screen alternative

March 18, 2016 Leave a comment

A few days ago I wrote about the awesome tool screen. Then, some of my readers namely “i90rr _” and Rodnee suggested that I should try tmux (thanks for the tip). Actually, I tried it years ago, but since I didn’t have to work with SSH too much, it was not integrated in my daily routine. However, a few days ago I bought a Digital Ocean VPS to do some Python webapp development and now I do need ssh :) And without screen / tmux it would be a pain.

So, after screen, let’s see tmux.

tmux: an advanced and friendly terminal multiplexer

The scenario is the same:

ssh
From machine A, which is your local machine, you want to SSH to a remote server.

Log in with SSH:

ssh -p PORT USER@1.2.3.4

Specify the port if the SSH daemon is not on port 22. In the case of screen I used screen twice, but now I think it’s not necessary. First just use “ssh”. To keep the connection alive between machines A and B, it’s a good idea to use this alias:

alias ssh='ssh -o ServerAliveInterval=60'

More info about it here.

Once you are logged in to machine B, make sure that you have a recent version of tmux installed. More info here. And now just start tmux and use it :)

My config file

A big advantage of tmux over screen is its sane configation system. You can find my ~/.tmux.conf file here, I won’t copy it here.

Screenshot

My tmux looks like this:

tmux_screenshot
In the top right corner I also display my hostname and IP address, but it’s blurred out on the screenshot.

Detach

A very special feature of screen / tmux is the ability to detach. It means that you have several tabs open within tmux (tmux calls them “windows”), and pressing “Ctrl-b d” you can close tmux. However, the programs in its tabs are still running, they are not terminated! You can close SSH, even restart your local machine, it doesn’t matter. When you log in again to machine B with SSH, you can attach to this tmux session and voilá, there you have your tabs and every program is running.

For instance, you launch a program on the server that runs for hours. Just detach, and attach a few hours later to see the result of this program. Or, you log in to a remote server from home, launch some programs, detach, shutdown your local machine, go to your workplace, log in again with SSH and attach to tmux, and you continue where you left off at home.

I don’t want to write a tmux tutorial because others have already done it much better. So let’s see some links:

Links

Appendix

I use two simple scripts to make re-attaching easier. The first one is called “tm”:

#!/usr/bin/env bash

# tm

tmux list-sessions
echo "# tmux attach -t 0"

The second one is called “tm0”:

tmux attach -t 0

If you don’t give your tmux session a name (I don’t) then your session has ID 0. When I log in again to the remote server first I run “tm” to see the running sessions. Normally I only have one session, so its ID is 0. With “tm0” I can attach to it and continue my work in tmux. Easy peasy :)

Summary: to detach, press “Ctrl-b d”, and close SSH. To continue your work, log in with SSH, check sessions with “tm”, and attach with “tm0”. Easy peasy :)

Categories: bash Tags: , ,
Follow

Get every new post delivered to your Inbox.

Join 90 other followers