How to restart the Apache Web Server by using Bash Script on Debian 11.3
- 00:26 lsb_release -a
- 00:36 apt install ncat -y
- 00:58 nano restart.sh
- 02:37 chmod +x restart.sh
- 02:55 ls -la restart.sh
- 03:07 systemctl status apache2
- 03:22 sh restart.sh
- 03:34 systemctl stop apache2
- 03:48 sh restart.sh
- 04:03 systemctl restart apache2
- 04:20 systemctl status apache2
- 04:36 crontab -e
- 05:22 systemctl stop apache2
- 05:32 systemctl status apache2
- 05:51 systemctl status apache2
To Restart the Apache web server by using Bash Script on Debian 11.3
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 Server
Installation procedure:
Step 1: Check the OS version by using the below command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
Step 2: Install ncat by using the below 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.Debian.com/Debian 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 by using the below command
root@linuxhelp:~# vi restart.sh
#!/bin/bash
if nc -z -w2 192.168.6.137 80 2>/dev/null; then
echo "Active"
else
echo "inactive"
systemctl restart apache2
fi
Step 4: Set the execution permission for the script file by using the below command
root@linuxhelp:~# chmod +x restart.sh
Step 5: Login to list the script file to view the changes by using the below command
root@linuxhelp:~# ls -la restart.sh
-rwxr-xr-x 1 root root 126 Jul 10 04:36 restart.sh
Step 6: Check the status of the Apache web server by using the below command
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 2022-07-28 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>
Jul 28 04:29:25 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step 7: Run the Script file by using the below command.
root@linuxhelp:~# sh restart.sh
Active
Step 8: Stop the Apache web server by using the below command
root@linuxhelp:~# systemctl stop apache2
Step 9: Again run the Script file by using the below command
root@linuxhelp:~# sh restart.sh
inactive
Step 10: Restart Apache web server by using the below command
root@linuxhelp:~# systemctl restart apache2
Step 11: Check the status of the Apache web server by using the below command
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 2022-07-28 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>
Jul 28 04:29:25 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step 12: Set cron job for restart.sh file by using the below command
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 13: Stop the Apache web server by using the below command
root@linuxhelp:~# systemctl stop apache2
Step 14: Check the status of the Apache web server by using the below command
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: Inactive (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.
Step 15: After one minute again Check whether the status of the apache web server is in active state by using the below command.
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: Inactive (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.
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to Restart the Apache web server by using Bash Script on Debian 11.3. Your feedback is much welcome.
Comments ( 0 )
No comments available