Restart failed program every 5 min

Problem

  • Your program fails regularly
  • You need to check if it is running and if not, restart it

Solution

Create a script:

nano restart.sh

Put in the code and replace htop with your program name:

#!/bin/sh

ps cax | grep htop > /dev/null
if [ $? -eq 0 ]; then
echo “Process running.”
else
echo “Process is down.”
fi

Make the script executable:

chmod +x restart.sh

Create a cron job:

crontab -e

Create a new line (replace /home/user/):

*/5 * * * * /home/user/restart.sh

Fin.

 

System wide proxy settings for Xubuntu (16.10)

Problem

  • You have to set http etc. proxy settings for all application
  • XUbuntu GUI is not helpful ūüėČ

Solution

Step 1: Edit environment

sudo nano /etc/environment

Append these lines (fill in your proxy):

http_proxy=http://yourproxy:8080/
https_proxy=http://yourproxy:8080/
HTTP_PROXY=http://yourproxy:8080/
HTTPS_PROXY=http://yourproxy:8080/

To load the new variables:

source /etc/environment

Step 2: Edit apt

Go to /etc/apt/apt.conf.d/ and create a file named 95proxies.

sudo nano /etc/apt/apt.conf.d/95proxies

Fill in these lines:

Acquire::http::proxy “http://yourproxy:8080/”;
Acquire::ftp::proxy “ftp://yourproxy:8080/”;
Acquire::https::proxy “http://yourproxy:8080/”;

Fin.

Raspberry PI – Static WIFI IP address

Problem

  • ¬†Raspberry PI is configured using Wifi and DHCP by default (Jessie)

Solution

The solution is simple. Open the file /etc/dhcpcd.conf as root (sudo nano /etc/dhcpcd.conf)

At the end of the file, add the following lines:

interface wlan0
static ip_address=192.168.xxx.xxx/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

  • static ip_address: your desired IP address
  • static routers: your gateway
  • static domain_name _servers: your DNS server

Save the file and reboot:

sudo reboot

Fin.

Icinga2 – Change layout of email notifications

Problem

  • You want to change the layout of the email notifications in Icinga2
  • You want to know, which variables Icinga2 provides

Solution

In Icinga2, emails are sent by a command line script (default is one for host and one for service notifications). In a default installation these scripts can be found under/etc/icinga2/scripts

The scripts are easy structured. At first, a variable template is set with a string which defines the mail body. After this template is “printed” and sent to mail. The -s parameter defines the subject of the email notification. (How to setup mail¬†is described in an another¬†post).

The default template looks like this:

#!/bin/sh
template=`cat <<TEMPLATE
***** Icinga *****

Notification Type: $NOTIFICATIONTYPE

Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $HOSTSTATE

Date/Time: $LONGDATETIME

Additional Info: $HOSTOUTPUT

Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
TEMPLATE
`

/usr/bin/printf “%b” “$template” | mail -s “$NOTIFICATIONTYPE – $HOSTDISPLAYNAME is $HOSTSTATE” $USEREMAIL

This means, you can change the mail body and the subject of the email.

Variables

Unfortunately, a listing of the available variables in Icinga2 are difficult to find. I found them in the migration chapter of the Icinga2 docs.

If you need more information in your notification, pick them out of the list. To use a new variable for the template or subject, you have to register them in the environment.

The environment is set in the commands.conf under /etc/icinga2/conf.d/

object NotificationCommand “mail-service-notification” {
import “plugin-notification-command”

command = [ SysconfDir + “/icinga2/scripts/mail-service-notification.sh” ]

env = {
NOTIFICATIONTYPE = “$notification.type$”
SERVICEDESC = “$service.name$”
HOSTALIAS = “$host.display_name$”
HOSTADDRESS = “$address$”
SERVICESTATE = “$service.state$”
LONGDATETIME = “$icinga.long_date_time$”
SERVICEOUTPUT = “$service.output$”
NOTIFICATIONAUTHORNAME = “$notification.author$”
NOTIFICATIONCOMMENT = “$notification.comment$”
HOSTDISPLAYNAME = “$host.display_name$”
SERVICEDISPLAYNAME = “$service.display_name$”
USEREMAIL = “$user.email$”
}
}

In the “env = {” statement, a mapping between the Icinga2 variables and the command variables is build up. If you want to add the needed variable, just add a new line.

Fin

Migrate MySQL to another server

Problem

  • You want to migrate a full MySQL¬†database to another server

Setting

  • Debian/Ubuntu/Raspbian
  • Installed MySQL instance on both servers (sudo apt-get install¬†mysql-server)
  • Root password

Solution

Backup

The basic call to backup a MySQL looks like this:

mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

In our case:

mysqldump -u root -pSTRONGROOTPASSWORD ‚Äď‚Äďall-databases > backup.sql

(no space between -p and the password!)

Copy

scp backup.sql user@host:/home/user

Import

The basic call to import looks like this:

mysql -u root -p[root_password] [database_name] < backup.sql

In our case:

mysql -u root -pSTRONGROOTPASSWORD < backup.sql

Set debian system maintainer password

For the default installation of a MySQL¬†on Debian, Ubuntu or Raspian, a system maintainer password is set by the package. It’s a different one for every instance. By importing the old database, the password for the new instance were overwritten. This is bad, because the MySQL¬†will not start after reboot and other stupid things happen. So you have to restore the correct password. This is stored in¬†/etc/mysql/debian.cnf. To get it do the following:

sudo cat /etc/mysql/debian.cnf | grep password
password = asdfadsfsdafadsgfadf
password = asdfadsfsdafadsgfadf

Setting the password:

mysql -u root -pSTRONGROOTPASSWORD

SET PASSWORD FOR ‘debian-sys-maint’@’localhost’ = PASSWORD(‘asdfadsfsdafadsgfadf’);

flush privileges;

quit;

Fin

 

How to pass a password to scp or ssh in a script

Problem

  • You want to use scp or ssh in a script but you can pass the password in the call

Solution

If you want to use ssh or scp in a script they ask for a password which you can not supply while running automatically. For this specific problem i use sshpass. This that you can easily provide the password.

Install (for Ubuntu/Debian)

sudo apt-get install sshpass

SSH

sshpass -p ‘strongPassword’ ssh user@192.168.2.77 command_to_run

SCP

sshpass -p ‘strongPassword’ scp *.tar.gz user@192.168.2.77:/home/user

Fin

Raspberry PI – Configure Static IP with /etc/network/interfaces or dhcpcd

[Update] Changed order of the solutions due to comments. Added warning to solution 2.[/Update]

[A simple solution for Wifi can be found here.]

Problem

  • In the default config the Raspberry PI 3 is configured using Wifi and DHCP (Jessie)
  • If you want to use wired ethernet with a static IP config, you have to reconfigure

Solution 1 – dhcpcd

Open dhcpcd.conf

sudo nano /etc/dhcpcd.conf

A valid config would look like this:

interface eth0
static ip_address=192.168.2.77/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

Restart networking and the work is done:

sudo service networking restart

Solution 2 – /etc/networking/interfaces (get rid of the dhcpcd service)

Warning: This solution is only useful, if you do not depend on dhcpcd, e.g. in a wired static IP environment.

Open the interfaces file with

sudo nano /etc/network/interfaces

A correct config would look like this:

auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.2.77
netmask 255.255.255.0
gateway 192.168.2.1
dns-nameservers 192.168.2.1

Now you have to stop and disable dhcpcd, because it will make your config useless:

sudo service dhcpcd stop

sudo systemctl disable dhcpcd

Restart networking and the work is done:

sudo service networking restart

Fin

M4 – stdio.h:477

Problem

While compiling M4 you get this error

./stdio.h:477:1: error: ‘gets’ undeclared here (not in a function)
_GL_WARN_ON_USE (gets, “gets is a security hole – use fgets instead”);

Solution

sed -i -e’/gets is a security/d’ /m4-1.4.16/lib/stdio.in.h