Skip to content

Tag: Linux

tmux save scrollback to a file

Before each command, run the prefix (default Ctrl+B).

:capture-pane -pane -S -3000
:save-buffer buffer.txt

This will write all scrollback from that pane to a file ~/buffer.txt.

endless tail in a file

tail -n 50 -f your.log
  • –folow or -f FILE to output appended data as the file grows
  • –lines or -n K output the last K lines of the file

Alternatively,  you can also use watch.

Bash Brace Expansion

photo by whiskeyandtears at https://www.flickr.com/photos/whiskeyandtears/2140154564

[bash]$ echo {0..9}
0 1 2 3 4 5 6 7 8 9[/bash]

[bash]$ echo b{a,e,i,o,u}
ba be bi bo bu
[/bash]

[bash]$ echo x{0..9}y
x0y x1y x2y x3y x4y x5y x6y x7y x8y x9y[/bash]

[bash]$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z[/bash]

[bash]$ echo {1..3} {A..C}
1 2 3 A B C[/bash]

[bash]$ echo {1..3}{A..C}
1A 1B 1C 2A 2B 2C 3A 3B 3C[/bash]

[bash]echo {a,b{1,2,3},c}
a b1 b2 b3 c[/bash]

[bash]$ mkdir -p {project1,project2}/{src,tst,bin,lib}/
$ find .
.
./project1
./project1/tst
./project1/bin
./project1/lib
./project1/src
./project2
./project2/tst
./project2/bin
./project2/lib
./project2/src
[/bash]

[bash]$ echo {{A..Z},{a..z}}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z[/bash]

[bash]$ for i in {a..f} 1 2 {3..5} ; do echo $i;done
a
b
c
d
e
f
1
2
3
4
5[/bash]

The examples below requires Bash version 4.0 or greater.

[bash]$echo {001..9}
001 002 003 004 005 006 007 008 009
[/bash]

[bash]$ echo {1..10..2}
1 3 5 7 9
[/bash]

find a process running a deleted library

sudo find /proc -maxdepth 2 -name maps -exec grep -HE ‘\(deleted\)’ {} \; | cut -d/ -f3 | sort -u | xargs –no-run-if-empty ps

Looking for libssl in specific:

sudo find /proc -maxdepth 2 -name maps -exec grep -HE ‘/libssl\.so.* \(deleted\)’ {} \; | cut -d/ -f3 | sort -u | xargs –no-run-if-empty ps

Killing all process using a deleted version of libssl:

sudo find /proc -maxdepth 2 -name maps -exec grep -HE ‘/libssl\.so.* \(deleted\)’ {} \;| cut -d/ -f3 | sort -u | xargs –no-run-if-empty sudo kill

if host pings execute command

[bash]#!/bin/sh
HOST="silveiraneto.net"
if ping -c 1 $HOST > /dev/null
then
echo your command
fi
[/bash]

  • replace silveiraneto.net for your desired hostname or ip
  • ping sends only one package (-c 1) and ignores the text output
  • a ping is successful returns 0 and the if proceeds
  • replace the echo line with your command

printing only odd or even lines using sed

$ cat AtoF
A
B
C
D
E
F

Print only odd lines

sed -n 'p;n' AtoF
A
C
E

-n suppress automatic printing of pattern space, p print the current pattern space, n read/append the next line of input into the pattern space.

Alternatively:

$ sed -n 1~2p AtoF
A
C
E

-n suppress automatic printing of pattern space, 1~2 from the 1st line match every line every 2 steps, p print the current pattern space. sed -n 0~p has the same effect . print only the even lines.

Print only even lines

$ sed -n 'n;p' AtoF 
B
D
F

or

$ sed -n 2~2p AtoF
B
D
F

0~2p would also work. I prefer

DIY recycle your old tv

When I came here to this room I found this old big tv laying around. Old but perfectly functional.

First I put a tv cable on it with hundreds of channels but after one month I realized that mostly of them are crap and I was watching everything on my laptop (mostly shows from Netflix and BitTorrent). Obviously I wanted to connect my laptop video output on it.  As an old television it doesn’t have any HDMI port. It have a RCA video and audio ports so after some research I bought a VGA-to-RCA converter. Besides watch movies on it there is a lot of cool things I could do with it to decorated the room or create a environment while we are jamming.

In this article I’ll show how to create a fireplace.

You will need

Getting a high definition fireplace video

You can search in Youtube for high definition fireplace videos.

There is a lot of ways to extract the videos from Youtube and it’s not the point explain it here but I can say one the best ways today is using JDownloader. Using it you can extract sound and video in any available resolution.

Choose a fireplace video and download the highest resolution available (usually 720p). Here I stored it at /home/silveira/Videos/fireplace_hd.flv.

Screen layout

I’m using the TV as a second display in the right. The width of my laptop monitor is 1920.

Creating a shortcut

First, right click in the upper panel of Gnome and select Add to Panel.  Select the Custom Application Launcher option.

Let’s fill it like this:

On the command put:

/usr/bin/mplayer /home/silveira/Videos/fireplace_hd.flv -nosound -loop 0 -geometry +1900+0 -fs

Explaning:

  • /usr/bin/mplayer the mplayer full path (probably you have the same path but you can try “whereis mplayer” in a terminal to find it).
  • /home/silveira/Videos/fireplace_hd.flv as I said, where I put the video.
  • -nosound no sound. 🙂 Usually I have some music playing so I don’t want any fire sound. But if you want  the fire crackling sound omit this parameter.
  • -loop 0 the video I took have 10 minutes. If I put -loop 6 it would loop for 1 hour. Zero means infinite times.
  • -geometry +1900+0 you can specify the size of the screen and position but here I’m justing saying I want it on horizontal position 1900 (bigger than my first screen) so it goes to the tv.
  • -fs fullscreen

Now drink the wine and cheese in front of your low carbon footprint fireplace.  😉

Bonus

Now you can use the same idea and make an virtual aquarium, ant farm, an interactive art piece or anything!

Miojo Script

O pre-requisito é o notify-send, um utilitário de linha de comando do libnotify. No Ubuntu:

sudo aptitude install libnotify-bin

E aqui o script em si:

sleep 5m; notify-send "aviso" "tirar o miojo do fogo"

Pronto, depois de cinco minutos isso vai aparecer:

Congelando e Ressuscitando Processos

batman mr freeze

Nem só de morte vive o kill.

Suponha que você tem um processo chamado program e quer congelar seu funcionamento. Para congela-lo sem mata-lo você pode mandar um sinal SIGSTOP com:

kill -s stop `pidof program`

Para ressuscitar o mesmo processo:

kill -s cont `pidof program`

Twitter Bot @rudaufc versão 1

robô

Este aqui é um bot bem simples para Twitter.

Diariamente, as nove da manhã ele posta qual vai ser o cardápio do RU (Restaurante Universitário) da UFC naquele dia.

Assim, quando vai batendo a hora da fome, os alunos podem entrar no perfil @rudaufc e olhar qual vai ser o prato do dia, ou quem está seguindo ele no Twitter pode ter a agradável surpresa de ver todo dia o que vai ser servido hoje.

Aqui está o código fonte do arquivo rudaufc.sh:

#!/bin/sh
# Twitter bot @rudaufc
login="rudaufc"
senha="suasenhaaqui"

segunda="Picadinho com legumes ou bife na chapa. Salada de macarrão com cenoura. Arroz. Feijão com abóbora e batata doce."
terca="Franco guisado ou coxas de frango ao forno . Salada de acelga, cenoura e passas. Arroz. Feijão com abóbora e batata doce."
quarta="# Feijoada à moda RU ou bisteca . Salada de repolho branco, cenoura e abacaxi. Arroz. Feijão com abóbora e batata doce"
quinta="Frango à passarinho ou frango chinês. Salada de Alface, Tomate e Cebola. Arroz. Feijão com abóbora e batata doce."
sexta="# Isca ao molho ou maravilha de carne. Salada de acelga com cenoura. Arroz. Feijão com abóbora e batata doce."

dia=`(date +%w)`
log=`(date +%Y-%m-%d-%s)`"-$$.log"
dir="/home/silveiraneto/rudaufc"
msg=""
case "$dia" in
#	"0") msg=$domingo ;;
	"1") msg=$segunda ;;
	"2") msg=$terca ;;
	"3") msg=$quarta ;;
	"4") msg=$quinta ;;
	"5") msg=$sexta ;;
#	"6") msg=$sabado ;;
esac

curl -u $login:$senha -d status="$msg" http://twitter.com/statuses/update.xml > $dir/$log

A mágica toda está na capacidade do Curl de acessar facilmente a API do Twitter para enviar mensagens.

Para que o script execute diariamente as nove da manhã ele está alocado em um servidor com a crontab configurada da seguinte maneira:

0 5 * * *  . /caminho_para_onde_ele_esta/rudaufc.sh

ps: leve em conta que o servidor está em um fuso horário diferente do Brasil.

Nessa versão o prato de cada dia está hardcoded no script, o que não é o ideal e faz com que semanalmente eu tenha que atualizar o script inserindo os pratos da semana manualmente. Eu espero que a próxima versão seja capaz de descobrir esses pratos e se atualizar sem nenhuma interferência.