Schlagwort: Shell
Copy to Clipboard from Command Line
If you’ve ever been debugging with a fellow developer, you’ll hear “OK, execute that and let me know what it says”. In this case, you can either manually copy the output and instant message the text over to them, or you can write the output to file with >>
, open the file, manually copy the contents, and paste it into IM. I experience this for a few hours recently and it was way to much work! Shouldn’t there be a way to quickly place an execution’s output directly into the clipboard just to save some time? You can with pbcopy
!
Copy stdout to Clipboard
You’ll use a single pipe to transfer the stdout result into the clipboard:
# command | pbcopy hg diff | pbcopy
The git diff
information is copied to the clipboard in this example; now you can show your colleague what you’ve changed.
Copy File Contents to Clipboard
In the case of copying file contents into the clipboard, pbcopy
goes first:
# pbcopy < file.ext pbcopy < circle.yml
The complete file contents are instantly copied to the clipboard for easy sharing.
Pasting to File
So what if you want to paste the clipboard contents into a new or existing file? Use pbpaste
:
#pbpaste > file.txt pbpaste > commands.txt
The clipboard contents will be placed into the given file.
pbcopy
will be a big timesaver for me moving forward. Manually copy and pasting information is with the mouse or trackpad is inconvenient and time-consuming. These types of command line techniques can make us more proficient, skilled developers!
The post Copy to Clipboard from Command Line appeared first on David Walsh Blog.
Was ist in einem LXC-Container gemountet?
Gestern wollte/musste ich mal wissen, was in einem LXC-Container (auf unserem Kommune-Server) gemountet ist. – Da das im ersten Anlauf nicht ganz so trivial heraus zu bekommen war, hier mal meine kleine Gedankenstütze.
long story short
root@host:~/# ls -lha /proc/$(lxc-info -n container -p | awk '{print $2}')/root/home
Erklärbär
Szenario
- LXC-Version: 0.7.5 (ja ich weiß, is alt … ;))
- Host =
hn
- Container =
container
- Mount = in der fstab (
host:/var/lib/lxc/container/fstab
) ist dashost:/home
mit der Zeile"/home home none bind 0 0"
eingebunden (d.h. also, dass beim Starten des LXC-Containers des /home vom Host beim Start des Containers in den Container gemountet wird; dabei isthome
als relativer Pfad zum/
angegeben)
Das Problem
Wenn man nun auf dem Host wissen will, wie das /home
im Container aussieht (ls -lha /var/lib/lxc/container/rootfs/home
) wird man feststellen, dass es ganz anders aussieht, als erwartet. – Hintergrund ist, dass der obige Mount in einem temporären Filesystem (also nicht wie ein normaler/echter Mount) eingehängt wird.
Hier mal die Ausgabe, die mit einem Standard-LXC-Template (Ubuntu) erzeugt wird:
root@host:~/# ls -lha /var/lib/lxc/container/rootfs/home
total 4.0K
drwxr-xr-x 3 root root 19 Aug 18 2012 .
drwxr-xr-x 22 root root 4.0K Feb 7 14:15 ..
drwxr-xr-x 2 ubuntu ubuntu 54 Aug 20 2012 ubuntu
Die Lösung
Nach etwas Recherche stieß ich auf den Blog-Post „LXC 1.0: Advanced container usage“ des LXC-Entwicklers Stéphane Graber, in dem der Trick (und einige Hintergründe) erklärt werden.
Jeder Container hat eine eigene Prozessnummer (pid
). Diese bekommt man mit lxc-info -n container -p
heraus.
Der im temporären Dateisystem eingehängte Mount befindet sich unter host:/proc
und dort wieder unter der jeweiligen PID.
Also angenommen unser Container hat die PID 1234
, dann findet man dessen root-Filesystem (inkl. aller Mounts) unter host:/proc/1234/root/
.
Wir benötigen also zuerst die PID des Containers (lxc-info -n container -p
) und danach können wir uns das Filesystem anzeigen lassen (ls -lha /proc/PID/root/
). – Beide Befehle kann man nun kombinieren.
mit LXC 1.x
In oben genannten Blog-Post wird für das Eruieren der PID lxc-info -n container -p -H
(angepasst!) angegeben. Dabei stehen die Schalter -n container
für den Containernamen, -p
für die Ausgabe der PID und -H
(wahrscheinlich; siehe unten) für die numerische Ausgabe der PID.
Die Kombination der beiden Befehle zum Anzeigen des Root-Dateisystems für den Container sieht dann so aus: ls -lha /proc/$(lxc-info -n container -p -H)/root/
(angepasst!).
mit LXC 1.x (in meinem Fall 0.7.5)
Da ich ATM allerdings noch nicht die Version 1.x verwende, funktioniert der Tipp (aus dem Blog-Post für die Version 1.x leider nicht (so ganz).
Denn in 0.7.5 gibt es den Schalter -H
bei der Ausgabe der PID mit lxc-info -n container -p
nicht, so dass man nicht nur die PID, sondern den Text pid: 1234
zurück bekommt. :-/
Allerdings ist das nicht so dramatisch, denn man kann sich ja mit awk
behelfen (und somit den Schalter ersetzen/emulieren). 🙂
lxc-info -n container -p | awk '{print $2}'
liefert nur die zweite ‚Spalte‘ der lxc-info-Ausgabe, also die nummerische PID.
Ergo hier nun meine Lösung zum Anzeigen des Root-Dateisystems des Containers:
root@host:~/# ls -lha /proc/$(lxc-info -n container -p | awk '{print $2}')/root/home
total 4.0K
drwxr-xr-x 3 root root 19 Aug 18 2012 .
drwxr-xr-x 22 root root 4.0K Feb 7 14:15 ..
drwxr-xr-x 2 user1 user1 […] user1
drwxr-xr-x 2 user2 user2 […] user2
drwxr-xr-x 2 user3 user3 […] user3
… wieder was gelernt … 😉 – Allerdings wohl nicht für lange, denn das Update auf die Version 1.x steht ja vor der Tür …
- 0
- 0
- 0
- 0