Browsed by
Category: Linux

Automation via shell aliases

Automation via shell aliases

terminal-appWhen working on software development projects there are many repetitive tasks to do, may it be the deployment of a binary, starting of different servers in docker containers or standalone, the exchange of config files for different environments, the migration of a database or something simple as the navigation to deep paths on the command line to do some editings or server starts there. All this tasks can be annoying and over time they add up to a significant amount of time which is not very productive. The logical counter measure for a developer should be to automate as much as possible of this recurring tasks. For example you can bundle up the start of different docker containers via docker compose or delegate some tasks to a CI server but in the end some tasks will remain which you have to trigger manually -at least on your development machine- which is where your shell can become handy with a feature called ‘alias’. An alias is an automation feature which at least every popular Linux/Unix/macOS shell provides. With an alias you can define a new command that executes a series of shell commands completely automatic.
I have a rule of thumb defined where I try to automate every manual task by an alias which I have to do at a minimum of three times a week (to be honest I also do an alias if I have to do a task two times every week just because it is such a handy feature).
To define a new alias all you have to do is to add it to the config file of your shell. In the case of the very popular bash shell this file is called .bashrc (on macOS you have to use the .bash_profile file when you use bash) while for zsh it is called .zshrc but in both cases the file sits in the users home folder. All you have to do is to open your rc file with an text editor, scroll to the end and add a new line starting with the keyword ‘alias’ followed by the alias name you chose and the command that should be executed. A sample command could look like this:

alias cdwildfly="cd ~/programming/java/servers/wildfly/"

Read More Read More

Find and kill a process on a specific port (lsof)

Find and kill a process on a specific port (lsof)

terminal-app
You might all have experienced the situation when you want to start a server either from within your IDE or via the terminal to only get the error that the “port is already in use” and the startup is aborted. This is mostly caused by aborting the server or a crash of the IDE which started it and not terminating it properly. When using macOS (or any other BSD or a Linux) there is a simple solution for this.

For such purposes macOS comes with the “lsof” command which stands for “list open files”. Its purpose is to show who is using a specific file or in our case who is using a specific port. After identifying the process it is easy to terminate it using the “kill” command.

Read More Read More

Backup von /home mit tar aus der Shell

Backup von /home mit tar aus der Shell

Vor einigen Wochen habe ich es geschafft mein Ubuntu so zu verbasteln das ich den XServer und somit auch keinen Desktop mehr starten konnte. Nun war mein letztes Backup allerdings leider nicht mehr ganz taufrisch und die Arbeit einiger Tage drohte verloren zu gehen. Da aber noch ein Login auf der Shell möglich war war es kein Problem die Home-Partition zu sichern und in aller Ruhe das System neu aufzusetzen.

Ein tar Backup von der Shell ist eigentlich nur ein Einzeiler. Man wechselt dazu in das zu sichernde Verzeichnis und tippt folgendes in die Shell ein:

tar -cvpzf backup.tar.gz

Tar erstellt ein Archiv in dem alle Berechtigungen so wie sind erhalten bleiben können. Die Optionen dahinter stehen für folgendes:
C: erstellt ein neues Archiv (create)
V: Der Fortschritt wird ausgegeben (verbose)
P: Berechtigungen werden beibehalten (preserve)
Z: Mit gzip komprimieren
F: Legt den Namen des Backups fest (filename)

Die entstehende Datei kann man sich bequem auf eine USB Platte kopieren und fertig ist das Backup.