• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to restart the Apache web server by using Bash Script on Ubuntu 21.04

  • 00:42 lsb_release -a
  • 00:58 apt install ncat -y
  • 03:39 ls -la restart.sh
  • 03:53 systemctl status apache2
  • 04:22 systemctl stop apache2
  • 04:46 sh restart.sh
  • 05:00 systemctl status apache2
  • 06:30 systemctl stop apache2
  • 06:50 systemctl status apache2
6748

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

Tags:
isaac
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What can able to do with Bash?

A

Bash scripting allows you to build analytics pipelines in the cloud and work with data stored across multiple files.

Q

What does -- mean in bash?

A

More precisely, a double dash ( -- ) is used in most Bash built-in commands and many other commands to signify the end of command options,
after which only positional arguments are accepted.

Q

Is cron is a daemon?

A

Cron is a clock daemon, whose name originates from Chronos, the Greek word for time.
It enables users to automate the execution of commands.

Q

What is cron in linux?

A

Unix-like operating system command
The cron command-line utility, also known as a cron job is a job scheduler on Unix-like operating systems.

Q

How To Run Bash Script In Linux?

A

Use "sh" and "./" to bash run script fil

Related Tutorials in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

Related Tutorials in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

How to install Meld tool in Ubuntu
How to install Meld tool in Ubuntu
Feb 25, 2017
How to install Dconf-Editor on Ubuntu 18.04
How to install Dconf-Editor on Ubuntu 18.04
Jul 14, 2018
How to install and update OpenSSL on Ubuntu 16.04
How to install and update OpenSSL on Ubuntu 16.04
Mar 9, 2017
How to install GLib 2.0 on Ubuntu 17.04
How to install GLib 2.0 on Ubuntu 17.04
May 22, 2017
How to Install Android Emulator on Ubuntu 20.4.1
How to Install Android Emulator on Ubuntu 20.4.1
Jul 13, 2021
How to install Apache from Source Code on CentOS 7
How to install Apache from Source Code on CentOS 7
Oct 21, 2017
How To Install AnyDesk on Ubuntu 16.04
How To Install AnyDesk on Ubuntu 16.04
Apr 4, 2018
How to install Genymotion 2.12.1 on Ubuntu 18.04
How to install Genymotion 2.12.1 on Ubuntu 18.04
Jul 9, 2018

Related Forums in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

Related Forums in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

CentOS
connor class=
How To Completely Remove Apache package On CentOS 7.6
May 14, 2019
Web Server
jacob class=
How to remove httpd completely from server
Apr 7, 2017
Ubuntu
matthew class=
Failed to enable unit: Refusing to operate on linked unit file sshd.service
Apr 15, 2019
Ubuntu
mason class=
Passwd: You may not view or modify password information for root On Ubuntu 19.04
May 27, 2019
Ubuntu
isaac class=
/etc/apt/sources.list Permission denied
May 18, 2017
Ubuntu
yousuf class=
lsb_release command not working : Debian
Jan 18, 2018
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Ubuntu
mason class=
"E: Package 'php-mcrypt' has no installation candidate" error on Ubuntu 20.4.1
Mar 15, 2021

Related News in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

Related News in How to restart the Apache web server by using Bash Script on Ubuntu 21.04

How To Install Mixxx on Ubuntu 16.04
How To Install Mixxx on Ubuntu 16.04
Oct 11, 2017
Ubuntu 17.04 released with greater expectations
Ubuntu 17.04 released with greater expectations
Apr 15, 2017
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Mar 1, 2017
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Sep 2, 2017
Ubuntu Unity is no more: One Linux dream has been axed
Ubuntu Unity is no more: One Linux dream has been axed
Apr 7, 2017
What’s next for Ubuntu Linux Desktop?
What’s next for Ubuntu Linux Desktop?
Apr 11, 2017
Say Hi to Ubuntu's new mascot
Say Hi to Ubuntu's new mascot
Mar 22, 2019
Attackers take advantage of Apache Struts vulnerabilities
Attackers take advantage of Apache Struts vulnerabilities
Mar 17, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help keel johnston ?
Unhide the folders on windows Explorer

Give any solutions to unhide folder using command prompt?

forum3

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.