How to restart the Apache web server by using Bash Script on Ubuntu 21.04
To Restart the Apache web server by using Bash Script on Ubuntu 21.04
Introduction:
Bourne Again Shell is a UNIX shell that uses the nc (or netcat) utility to listen on TCP and UDP ports. It supports both IPv4 and IPv6
Prerequisite:
Apache Web Serve
Installation procedure:
Step 1: Check the OS version by using the following command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
Step 2: Install ncat by using the following command
root@linuxhelp:~# apt install ncat -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
ncat
0 upgraded, 1 newly installed, 0 to remove and 162 not upgraded.
Need to get 108 kB of archives.
After this operation, 290 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute/universe amd64 ncat amd64 7.91+dfsg1+really7.80+dfsg1-1 [108 kB]
Fetched 108 kB in 1s (173 kB/s)
Selecting previously unselected package ncat.
(Reading database ... 239262 files and directories currently installed.)
Preparing to unpack .../ncat_7.91+dfsg1+really7.80+dfsg1-1_amd64.deb ...
Unpacking ncat (7.91+dfsg1+really7.80+dfsg1-1) ...
Setting up ncat (7.91+dfsg1+really7.80+dfsg1-1) ...
Processing triggers for man-db (2.9.4-2) ...
Step 3: Create Bash Script file named restart.sh
root@linuxhelp:~# vi restart.sh
#!/bin/bash
if nc -z -w2 192.168.6.115 80 2>/dev/null; then
echo "Active"
else
echo "inactive"
systemctl restart apache2
fi
Step 4:Set the execution permission for the script file
root@linuxhelp:~# chmod +x restart.sh
Step 5: Login to list the script file to view the changes
root@linuxhelp:~# ls -la restart.sh
-rwxr-xr-x 1 root root 126 Nov 10 04:36 restart.sh
Step 6: Check the status of the apache web server
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-11-10 04:29:25 IST; 7min ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 5997 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 6001 (apache2)
Tasks: 55 (limit: 2256)
Memory: 5.2M
CGroup: /system.slice/apache2.service
├─6001 /usr/sbin/apache2 -k start
├─6002 /usr/sbin/apache2 -k start
└─6003 /usr/sbin/apache2 -k start
Nov 10 04:29:25 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Nov 10 04:29:25 linuxhelp apachectl[6000]: AH00558: apache2: Could not reliably determine the server's fully qualified dom>
Nov 10 04:29:25 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step 7: Run the Script file by using the following command
root@linuxhelp:~# sh restart.sh
Active
Step 8: Stop the apache web server
root@linuxhelp:~# systemctl stop apache2
Step 9: Again run the Script file
root@linuxhelp:~# sh restart.sh
inactive
Step 10: Restart apache web server by using the following command
root@linuxhelp:~# systemctl restart apache2
Step 11: Check the status of the apache web server
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-11-10 04:38:19 IST; 58s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 8053 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 8057 (apache2)
Tasks: 55 (limit: 2256)
Memory: 5.1M
CGroup: /system.slice/apache2.service
├─8057 /usr/sbin/apache2 -k start
├─8058 /usr/sbin/apache2 -k start
└─8059 /usr/sbin/apache2 -k start
Nov 10 04:38:18 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Nov 10 04:38:19 linuxhelp apachectl[8056]: AH00558: apache2: Could not reliably determine the server's fully qualified dom>
Nov 10 04:38:19 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step 13: Set cron job for restart.sh file
root@linuxhelp:~# crontab -e
no crontab for root - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.tiny
3. /bin/ed
Choose 1-3 [1]:
crontab: installing new crontab
GNU nano 5.4 /tmp/crontab.ZHK8QN/crontab
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * sh restart.sh
Step 14: Stop the apache web server
root@linuxhelp:~# systemctl stop apache2
Step 15: Check the status of the apache web server
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-11-10 04:41:01 IST; 10s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 8198 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 8202 (apache2)
Tasks: 55 (limit: 2256)
Memory: 4.8M
CGroup: /system.slice/apache2.service
├─8202 /usr/sbin/apache2 -k start
├─8203 /usr/sbin/apache2 -k start
└─8204 /usr/sbin/apache2 -k start
Nov 10 04:41:01 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Nov 10 04:41:01 linuxhelp apachectl[8201]: AH00558: apache2: Could not reliably determine the server's fully qualified dom>
Nov 10 04:41:01 linuxhelp systemd[1]: Started The Apache HTTP Server.
By this the restarting of Apache web server by using Bash Script on Ubuntu 21.04 comes to end
Comments ( 0 )
No comments available