This is an old revision of the document!
Service Builder
Here are my shell script services that I use on most servers. These predate systemd by a few years, hence not being systemd units. These all use cronjobs to keep them running similar to:
* * * * * /usr/bin/flock --nonblock /tmp/apache-restart.lock /bin/bash /usr/local/bin/apache-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/mariadb-restart.lock /bin/bash /usr/local/bin/mariadb-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/fail2ban-restart.lock /bin/bash /usr/local/bin/fail2ban-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/qemuagent-restart.lock /bin/bash /usr/local/bin/qemuagent-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/amavis-restart.lock /bin/bash /usr/local/bin/amavis-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/clamav-restart.lock /bin/bash /usr/local/bin/clamav-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/dovecot-restart.lock /bin/bash /usr/local/bin/dovecot-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/postfix-restart.lock /bin/bash /usr/local/bin/postfix-restart.sh > /dev/null 2>&1 * * * * * /usr/bin/flock --nonblock /tmp/spamass-restart.lock /bin/bash /usr/local/bin/spamass-restart.sh > /dev/null 2>&1 0 4 * * * /usr/bin/flock --nonblock /tmp/mariadb-dump.lock /bin/bash /usr/local/bin/mariadb-dump.sh > /dev/null 2>&1
## Apache
cat > /usr/local/bin/apache-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart apache2.service"
SERVICE="apache2.service"
LOGFILE="/home/logs/apache.log"
#check for the word dead in the service output from systemctl
if
systemctl status apache2.service | grep 'failed\|dead'
then
echo "Jonathan, apache failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[apache2-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/apache-restart.sh
#### Mariadb Restart
cat > /usr/local/bin/mariadb-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart mariadb.service"
STATUS="/bin/systemctl status mariadb.service"
SERVICE="mariadb.service"
LOGFILE="/home/logs/mysql.log"
#check for the word dead in the service output from systemctl
if
$STATUS | grep "dead\|failed"
then
echo "Jonathan, mariadb failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[mariadb-restart]-$(hostname)-$(date)" friend@haacksnetworking.org < $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/mariadb-restart.sh
#### Fail2Ban Restart
cat > /usr/local/bin/fail2ban-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart fail2ban.service"
SERVICE="fail2ban.service"
LOGFILE="/home/logs/fail2ban.log"
#check for the word dead in the service output from systemctl
if
systemctl status fail2ban.service | grep 'failed\|dead'
then
echo "Jonathan, fail2ban failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[fail2ban-restart]-$(hostname)-$(date)" friend@haacksnetworking.org < $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/fail2ban-restart.sh
#### Qemu Agent Restart
cat > /usr/local/bin/qemuagent-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart qemu-guest-agent.service"
SERVICE="qemu-guest-agent.service"
LOGFILE="/home/logs/qemu-guest-agent.log"
#check for the word dead in the service output from systemctl
if
systemctl status qemu-guest-agent.service | grep 'failed\|dead'
then
echo "Jonathan, qemu-guest-agent failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[qemu-guest-agent-restart]-$(hostname)-$(date)" friend@haacksnetworking.org < $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/qemuagent-restart.sh
#### Amavis Restart
cat > /usr/local/bin/amavis-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart amavis.service"
SERVICE="amavis.service"
LOGFILE="/home/logs/amavis.log"
#check for the word dead in the service output from systemctl
if
systemctl status $SERVICE | grep 'failed\|dead'
then
echo "Jonathan, amavis failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[amavis-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/amavis-restart.sh
#### ClamAV Restart
cat > /usr/local/bin/clamav-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart clamav-freshclam.service"
SERVICE="clamav-freshclam.service"
LOGFILE="/home/logs/clamav.log"
#check for the word dead in the service output from systemctl
if
systemctl status $SERVICE | grep 'failed\|dead'
then
echo "Jonathan, clamav failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[clamav-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/clamav-restart.sh
#### Dovecot Restart
cat > /usr/local/bin/dovecot-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart dovecot.service"
SERVICE="dovecot.service"
LOGFILE="/home/logs/dovecot.log"
#check for the word dead in the service output from systemctl
if
# systemctl status $SERVICE | grep 'failed\|dead'
systemctl status $SERVICE | grep 'inactive'
then
echo "Jonathan, dovecot failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[dovecot-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/dovecot-restart.sh
#### Postfix Restart
cat > /usr/local/bin/postfix-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart postfix.service"
SERVICE="postfix.service"
LOGFILE="/home/logs/postfix.log"
#check for the word dead in the service output from systemctl
if
systemctl is-active $SERVICE | grep 'inactive'
then
echo "Jonathan, postfix failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[postfix-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/postfix-restart.sh
#### Spam Assassin Restart
cat > /usr/local/bin/spamass-restart.sh << 'EOF'
#!/bin/sh
#functions
RESTART="/bin/systemctl restart spamassassin.service"
SERVICE="spamass-milter.service"
LOGFILE="/home/logs/spamass.log"
#check for the word dead in the service output from systemctl
if
systemctl status $SERVICE | grep 'failed\|dead'
then
echo "Jonathan, spamassassin failed at $(date), so I restarted it for you." >> $LOGFILE
$RESTART >> $LOGFILE
mail -s "[spamass-restart]-$(hostname -f)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
else
exit
fi
EOF
chmod 750 /usr/local/bin/spamass-restart.sh
#### MariaDB Dump
cat > /usr/local/bin/mariadb-dump.sh << 'EOF'
DATE=`date +"%Y%m%d-%H:%M:%S"`
START1="$(date +%s)"
LOGFILE="/home/logs/mysql-dump.log"
/usr/bin/mysqldump \
--defaults-extra-file=/etc/mysql/mysqlpassword.cnf \
--add-drop-database \
--all-databases \
--allow-keywords \
--comments \
--complete-insert \
--lock-all-tables \
--skip-dump-date \
--events \
--flush-logs \
--flush-privileges \
--hex-blob \
--opt \
--routines \
> /root/name-databases-all.sql
END1="$(date +%s)"
DURATION1=$[ ${END1} - ${START1} ]
MINUTES=$[ ${DURATION1} / 60 ]
echo "$(date) Jonathan, the db dump took exactly ${DURATION1} seconds and approximately ${MINUTES} minutes to complete." | tee -a $LOGFILE
mail -s "[mariadb-dump]-$(hostname)-$(date)" friend@haacksnetworking.org < $LOGFILE
rm $LOGFILE
EOF
chmod 750 /usr/local/bin/mariadb-dump.sh
— oemb1905 2026/09/05 23:25