Simple Watchdog script

 I will give an example of a simple script for restarting services if they are not running and sending a notification by email:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
SERVICES="ssh apache2 zabbix-server zabbix-agent mysql smbd nmbd asterisk"
DATE=$(date '+%d-%m-%Y %H:%M:%S')
 
for SERVICE in ${SERVICES}
 do
   service $SERVICE status 2>&1>/dev/null
    if [ $? -ne 0 ];
      then
        service $SERVICE stop
        sleep 3
        service $SERVICE start
        echo -e "Starting $SERVICE"
        (echo "Subject:Restarting $SERVICE"; echo "$DATE $SERVICE is not running on $HOSTNAME! Restarting!";) | sendmail test@ixnfo.com
      else
        echo -e "$SERVICE OK"
    fi
done

In the script, specify the email on which you want to receive notifications, and in the “SERVICES” list of services through the space that you want to check.
I first stopped the service in order to surely stop all possible processes, paused for 3 seconds and then started, because with the restart command, some highly loaded services could not start correctly and hang with the status “active (exited)”.

To make the script automatically run for example every 10 minutes, add a line to /etc/crontab:

1
*/10 * * * * root /dir/watchdog.sh > /dev/null 2>&1
Continue reading Simple Watchdog script

Restarting a Linux Server via Web Browser (PHP)

 After opening and reading every result on Google, I figured it's time to make my own thread somewhere.

I'm trying to setup a means of rebooting/doing other system functions through a web interface powered by HTML (for the buttons/text) and PHP (for the execution of the aforementioned functions).

I'm unable to get this to work. I've read that I need to add the web user to the sudoers file, and I've tried. I'm running Nginx on my server, how do I add the user to the sudoers in my case?

Also, I'm aware of the security risks, and would prefer not to be advised of them, thanks.

The following is what I have so far:

HTML:

HTML
<body>

<h3>Restart</h3>
<p>
<form action="restart.php" method="get">
  <input type="submit" value="Press me.">
</form>
</p>

</body>

PHP:

PHP
<?php
echo "This is a test";
echo "<br>";
echo "<br>";
echo shell_exec('ifconfig');
echo "<br>";
echo "<br>";
echo "Restarting server...";
exec ('/usr/bin/sudo /etc/init.d/portmap restart');
shell_exec("/sbin/reboot");
exec("/sbin/reboot");
system("/sbin/reboot");
?>

Mind you that here, I only have so many things attempting to execute, so that I make sure I hit the target when one of them works, if that makes sense. The IFConfig is just a test to make sure that it's actually able to execute.

Sudoers:

Text
# User privilege specification
root    ALL=(ALL:ALL) ALL
www-data reboot = NOPASSWD: /sbin/reboot

Hope help comes. Thanks.

---------------------------------------------

EDIT - I got a response from another location that fixed my problem. In the case that someone is dealing with a similar problem, here's the solution that helped me.

Text
You can do this either by editing your sudoers file :

Sudoers:
www-data ALL=(root) NOPASSWD: /sbin/reboot 

The first ALL is for the hostname if you're hostname is not 'reboot' I advise you to keep ALL as it will work in any hostname. That's why I don't seem to work on your server


restart.php:
exec('sudo /sbin/reboot');
Continue reading Restarting a Linux Server via Web Browser (PHP)
,

Use a script to restart critical Linux services such as the web or database server. Restart nginx, apache2, MySQL or PHP-fpm automatically.

 If you manage your own WordPress web server then you have undoubtedly come across many situations where your web apache, nginx, MySQL or PHP-fpm services have stopped.

Sometimes the reason is unknown and things just crash from time to time.

Using the Linux crontab service we can write a simple bash script to test to see if these services have stopped and restart them.

You can use whatever editor you are comfortable with.

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

Change the service names to the ones you are running, e.g. “apache2” or whatever PHP version you are running.

The script uses the service <name> status command to output the status of a particular service such as mysql.

We then run this through grep looking for the phrase “active (running)”.

If this is not found, we ask the system to restart the service.

Save the file to /opt/launch-crashed-services.sh

Then ensure that it is runnable from the command line using:

sudo chmod +x /opt/launch-crashed-services.sh

Scheduling Service Restarts Using Crontab

It would be a pain to have to SSH into our server every time a service crashes to run the script.

Instead we can call the script directly from a crontab service and have it running as frequently as we need it to.

Edit your root crontab list using:

sudo crontab -e

It’s important to use the root crontab using the command above and not to edit your own user profile crontab, otherwise, it will not work properly.

Add the following line to the bottom of the root crontab list:

*/1 * * * * /opt/launch-crashed-services.sh > /dev/null 2>

This will run the script every minute but you can change that for whatever works for your server.

Now if a critical service crashes, the server will attempt to restart it.

Happy days.

Continue reading Use a script to restart critical Linux services such as the web or database server. Restart nginx, apache2, MySQL or PHP-fpm automatically.