Skip to content

Instantly share code, notes, and snippets.

@th-schwarz
th-schwarz / SpringBeanColletor.java
Created July 14, 2024 10:18
Collect spring-managed beans before the ApplicationStarted event is started. Can be used to process things before the application starts.
/**
* SpringBeanCollector is an abstract class that implements the BeanPostProcessor interface. It provides
* functionality for collecting and processing desired Spring managed beans.
*
* <p>During the bean post-processing, SpringBeanCollector checks if the bean matches the desired types.
* Once all the desired beans are initialized, it triggers the processing logic by calling the
* process() method. The processBean() method is called internally to check and initialize the
* beans.
*/
@th-schwarz
th-schwarz / apache2AllDomains
Last active April 23, 2022 17:06
Fetch all domains on port 443 with aliases from apache2
apache2ctl -S | sed "0,/^\*:443/d" | sed -e "/ServerRoot/Q" | sed '1d' | sed 's/.*port 443 namevhost //' | sed 's/.*alias //' | awk '{print $1}' | sort
@th-schwarz
th-schwarz / A-ProcessBuilderWrapper.md
Last active July 15, 2024 16:53
A complete wrapper to the ProcessBuilder

There are many topics around the ProcessBuilder. The focus here is how to handle the output of executed commands correctly!

The following two things are important:

  • Consuming STDOUT and STDERR is necessary to avoid freezing!
  • If the process writes a large amount of output, it must be consumed. This can be done by calling ProcessBuilder#redirectErrorStream, which redirects STDERR to STDOUT!

I've written a wrapper object to the ProcessBuilder to handle and configure it easier. Have look at the code below.

Here is a simple example to demonstrate the usage:

@th-schwarz
th-schwarz / check-mailcow-update.sh
Last active September 29, 2022 05:41
Script to check, if there is an update for mailcow-dockerized. In this case, an email will be sent. It's intended to use in a cron jobs.
#!/bin/bash
set -o nounset
set -o errexit
cd /opt/mailcow-dockerized && ./update.sh --check > /dev/null
exit_code=$?
[ $exit_code -eq 0 ] && echo -e "Subject:MAILCOW: Update available \n\n Update your mailcow instance please!\n" | sendmail root
exit 0
@th-schwarz
th-schwarz / EasiestCallOfProcessBuilder.java
Last active October 27, 2021 11:17
The easiest call of the ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("ls", "-al");
pb.directory(Paths.get("/tmp").toFile());
pb.redirectErrorStream(true);
Process process = pb.start();
try (var infoStream = process.getInputStream()) {
infoStream.transferTo(System.out);
}
int exitCode = process.waitFor();
if(exitCode == 0)
System.out.println("Command successful processed.");
@th-schwarz
th-schwarz / munin-plugin-vpn-user-count.sh
Created October 26, 2021 09:27
munin plugin to count openvpn users
#!/bin/bash
if [ "$1" = "config" ]; then
echo 'graph_title OpenVPN - count of users'
echo 'graph_args --base 1000 -l 0'
echo 'graph_vlabel count of users'
echo 'graph_category network'
echo 'graph_scale no'
echo 'run.label users'
exit 0
fi
@th-schwarz
th-schwarz / backup-mysql.sh
Last active October 21, 2021 08:32
Dumps every mysql database to a single file, all dump files will be zipped and redirected to the stdout.
## Dumps every database to a single file, all dump files will be zipped and redirected to the stdout.
## Usage: backup-db.sh > databases.zip
##
#! /bin/bash
set -o nounset
set -o errexit
trap 'rm -rf "$BACKUP_DIR"' EXIT
BACKUP_DIR=$(mktemp -d)
MYSQL=/usr/bin/mysql