Bash Script To Restart Linux Server Services

Here’s the command line code to create the file in nano:

sudo nano /opt/launch-crashed-services.sh

Here is the bash script.

#!/bin/bash

service mysql status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo service mysql restart > /dev/null
fi

service nginx status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo service nginx restart > /dev/null
fi

service php7.2-fpm status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo service php7.2-fpm restart > /dev/null
fi

Continue reading “Bash Script To Restart Linux Server Services” »

Share

osTicket Automated Install Script

osTicket is one of the leading open source ticketing systems. Here’s an easy way to spin up an instance on Ubuntu.

Note:
This script is purely intended for use in short-lived demo systems. The passwords are obvious (password !?) and there is no hardening applied to any of this system. DO NOT USE THIS SCRIPT IN PRODUCTION.

Create Shell Script
Spin up an Ubuntu instance then create a new shell script and give it executable permissions:

touch osTicket.sh
chmod +x osTicket.sh

Script Content
Paste this into the script: Continue reading “osTicket Automated Install Script” »

Share

Proxmox VM auto start VM after found in shutdown state

I was having this strange issue, where a running busy VM stopeed all of a sudden due to high CPU or Memory overload issue. So manually had to start everythime. In order to avoid this, created a small script to start the VM in case if it’s down.

#!/bin/bash

# Set environment
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

su -

if [[ $(qm status 101) = *"status: stopped"* ]];
then
echo `qm start 101`
fi
Share

Email Alert for Host down using fping

A simplified bash script for host status alert:

#!/bin/bash

email=h.t.emdad@gmail.com
NBR_DOWN=0
LOGFILE=/tmp/pinglog.txt

echo "Server Down Status" > $LOGFILE
for i in $(cat ping.txt); do
fping $i >/dev/null
if [ $? -ne 0 ]; then
echo "$i is down" >> $LOGFILE
NBR_DOWN=$((NBR_DOWN+1))
fi
done

if [ $NBR_DOWN -gt 0 ]; then
mailx -s "Server Down Alert" $email < $LOGFILE
fi

The contents on ping.txt is like (entries are with new line entry:

core-router
host1
bc
host2
host3
host4
cpanel

If you do not have ‘mailx’ installed, install it using

yum install mailx.
Share

WordPress Backup Script

#!/bin/bash

# This script creates a compressed backup archive of the given directory and the given MySQL table. More details on implementation here: https://theme.fm
# Feel free to use this script wherever you want, however you want. We produce open source, GPLv2 licensed stuff.
# Author: Konstantin Kovshenin exclusively for Theme.fm in June, 2011
# Modification by Hasan T. Emdad tweenpath.net on August, 2020

# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.

NOW=$(date +"%Y-%m-%d-%H%M")
FILE="example.org.$NOW.tar"
BACKUP_DIR="/backups"
WWW_DIR="/var/www/example.org/"

# MySQL database credentials
DB_USER="mysqluser"
DB_PASS="mysqlpass"
DB_NAME="example_org"
DB_FILE="example.org.$NOW.sql"

# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^var/www/example.org,www,'
DB_TRANSFORM='s,^var/www/example.org/backups,database,'

# Create the archive and the MySQL dump
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump --user=${DB_USER} --password=${DB_PASS} $DB_NAME > $BACKUP_DIR/$DB_FILE

# Append the dump to the archive, remove the dump and compress the whole archive.
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
Share

Install Zoneminder on Ubuntu 18.04 with shell script

This will install Zoneminder by using a shell script with one basic command (how easy is that!).

You will need a Ubuntu 18.04 install with LAMP (Apache, MySQL and PHP) installed desktop or server. As an alternate you may use Mariadb in lieu of MySQL

Shell script file contents:

#!/bin/sh
clear
read -p "This script installs Zoneminder 1.32.x on Ubuntu 18.04 AMD64 with LAMP (MySQL) installed...
Press Enter to continue or Ctrl + c to quit" nothing
clear
read -p "You must be logged in as root using sudo su ...
Press Enter to continue or Ctrl + c to quit" nothing
clear
read -p "Next we will add the PPA repository, install and configure the system to run Zoneminder. 
Press enter to continue" nothing
apt install -y software-properties-common
clear
add-apt-repository ppa:iconnor/zoneminder-1.32
apt update
clear
awk '$0="date.timezone = "$0' /etc/timezone >> /etc/php/7.2/apache2/php.ini
clear
apt install -y zoneminder
systemctl enable zoneminder
service zoneminder start
adduser www-data video
a2enconf zoneminder
a2enmod rewrite
chown -R www-data:www-data /usr/share/zoneminder/
service apache2 reload
clear
read -p "Install complete.Press enter to continue" nothing
clear

Copy the contents of the script, open a terminal and run: Continue reading “Install Zoneminder on Ubuntu 18.04 with shell script” »

Share

Archiving a large backup across multiple discs on Linux

We have two options (as we obviously don’t want to delete our data!)

  • Use a different backup medium
  • Split the backup across multiple volumes

Sometimes the former just isn’t appropriate, as much because of the cost of harddrives vs Optical Media (i.e. CD’s/DVD’s).

This short tutorial will explain how to create a single backup archive, and then split it across multiple CD’s/DVD’s.

In this tutorial, I want to backup to DVD (as my photo collection would require more than 40 CD’s).

So from BASH;

tar -cf Pictures_backup.tar Pictures/
split -d -b 4480m Pictures.backup.tar

This will then give you multiple files to burn to disc (each beginning with x). Burn these files with your favourite CD Burner. To restore the files, copy them all to one directory and then run the following Continue reading “Archiving a large backup across multiple discs on Linux” »

Share

PHP Session test Script

I’ve just found a quality script to test php session- unless you’re in dark after some php.ini session tweaking done.

To check if sessions really work you can use this code:

<?php
// Start Session
session_start();
// Show banner
echo '<b>Session Support Checker</b><hr />';
// Check if the page has been reloaded
if(!isset($_GET['reload']) OR $_GET['reload'] != 'true') {
   // Set the message
   $_SESSION['MESSAGE'] = 'Session support enabled!<br />';
   // Give user link to check
   echo '<a href="?reload=true">Click HERE</a> to check for PHP Session Support.<br />';
} else {
   // Check if the message has been carried on in the reload
   if(isset($_SESSION['MESSAGE'])) {
      echo $_SESSION['MESSAGE'];
   } else {
      echo 'Sorry, it appears session support is not enabled, or you PHP version is to old. <a href="?reload=false">Click HERE</a> to go back.<br />';
   }
}
?>
Share

How to keep a job running in Linux

There are many ways to keep a process running on linux but I haven’t seen any that are as easy to implement as the script below.

Basically the script does a ps ax and then a grep for your process. If it’s not running it will re-start the process.

You install the script into your crontab i.e. crontab -e

As a bonus this mechanism will re-start your process after a re-boot. Continue reading “How to keep a job running in Linux” »

Share