Categories
Uncategorized

Monitor EC2 using crontab

If you want to monitor your Amazon Web Services (AWS) Elastic Cloud Compute (EC2) instances using crontab and AWS SDK. Here in this example I have chosen php (7.1).

Install AWS SDK

Install composer

composer require aws/aws-sdk-php
Categories
Uncategorized

Monitor services (Apache, MySQL, HHVM) using crontab

Want to ensure that your services (Apache, MySQL, HHVM) on your linux instance are running 24*7? Then you can use crontab for that. This job runs every minute to check the status of the service and if it is not running then, it starts the service.

Categories
Uncategorized

Redirect users based on the HTTP Request Header – Node.js

For this example it is recommended that you have the latest most stable release of Node.js + npm installed.
Now you will need express to run a simple server.

Categories
Uncategorized

Create an Observable the right way – Angular 5

Using a subject to create an observable is the recommended method of inter-component communication by the official angular documentation.
The following code was tested with Angular 5.2.2.

Categories
Uncategorized

Add Google Analytics to WordPress in 2 minutes

The following is the fastest way to integrate Google Analytics with your self hosted WordPress website without using any external applications or plugins. By injecting plain javascript into your tag, you won’t have to worry about incurring the overhead of adding a new plugin to your site for a small 2 line code addition.

Categories
Uncategorized

Delete shell history

You can login as the user you want to delete history from and then use the following command. For example if you want to delete from root

sudo -i
cat /dev/null > ~/.bash_history && history -c && exit
Categories
Uncategorized

Automate total backups using crontab

sudo crontab -e

The following cronjobs will run every hour. Make sure you replace the database connection string values (make sure you have the right privileges on the database being used.) and backup folder paths with the appropriate ones.

0 * * * * cd /cron/backup && tar -zcvf backup.tar.gz /var/www/html/
0 * * * *  mysqldump -u user_name -p'password' db_name> /cron/backup/backup.sql

Now save the changes and restart the cron service.

Ctrl+O
Enter
Ctrl+X
sudo service cron restart
Categories
Uncategorized

Optimize all the tables in your database using cronjobs

This php script repairs and optimizes all the tables in the selected database. Make sure you provide the right values in the connection string.

Script

optimize.php

 $tablename)
   {
       if(mysqli_query($link,"REPAIR TABLE `$tablename`")) 
           { 
           echo '\nRepair Success: '.$tablename;
            }
       else 
         die(mysqli_error($link));
       if(mysqli_query($link,"OPTIMIZE TABLE `$tablename`")) 
          { 
           echo '\nOptmize Success: '.$tablename;
          }
       else
          die(mysqli_error($link));
   }
}

mysqli_close($link);

Cronjob

crontab -e
0 * * * * php /var/www/html/optimize.php
Ctrl+O
Enter
Ctrl+X
sudo service cron restart
Categories
Uncategorized

Learn how to schedule MySQL jobs

I am using MariaDB 5.x for this post. However this should work on most recent MySQL databases.
Make sure that MySQL server is running and login with the root or with the user that has privileges of creating events on the database you would like to use.

mysql -u root -p password123 db_name
SET GLOBAL event_scheduler = ON;

(or)

mysql -u root -p
SET GLOBAL event_scheduler = ON;
use db_name;
CREATE EVENT e_hourly
    ON SCHEDULE
      EVERY 1 HOUR
    COMMENT 'Clears out not active users after 24 hours of link being sent.'
    DO
      DELETE FROM Users_table WHERE activated='n' AND DATEDIFF( NOW(),  time ) >1;

The above statement will create an event that runs every hour starting from the moment you execute this script. But if you want it to start executing at some other time (ex: January 30th 2018 at 00:00), then you can use the START command in the following way.

CREATE EVENT 
  ON SCHEDULE 
    EVERY 1 DAY
      STARTS '2018-01-30 00:00:00'
    COMMENT 'Clears out not active users after 24 hours of link being sent.'
DO
DELETE FROM Users_table_message WHERE activated='n' AND DATEDIFF( NOW(),  time ) >1;
SHOW EVENTS;

Cronjobs using Crontab

This can also be done directly using crontab and scheduling a cronjob. Consider the following code for example.

crontab -e

This will usually open crontab file with the nano editor.

#!/bin/bash
mysql --user=root --password=password123 --database=db_name --execute="DELETE FROM Users_table WHERE activated='n' AND DATEDIFF( NOW(),  time ) >1"

Now to save the changes to disk and exit while using the nano editor, use the following commands.

Ctrl+O
Enter
Ctrl+X

Scripts

You can also use php scripts to run the mysql query. For example the following script runs every hour.

0 * * * * /usr/local/bin/php /var/www/html/clean_mysql_script.php
Categories
Uncategorized

Disable Jetpack Site Stats

Make sure you are logged into the WordPress admin console and that you have necessary permissions to toggle the Jetpack modules and navigate to the following link after replacing the domain name with yours. Here you should be able to easily activate and deactivate the Jetpack list of modules easily.
https://www.url-here.com/wp-admin/admin.php?page=jetpack_modules