A bash script to test a service established or not.

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
hack3rcon
Posts: 530
Joined: 2015-02-16 09:54

A bash script to test a service established or not.

#1 Post by hack3rcon »

Hello,
I want to write a bash script to restart a service and if the service launched successfully and established then execute the next command.
For example, restart the Apache service and when the service established then the next command execute.

Thank you.

arzgi
Posts: 800
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 4 times

Re: A bash script to test a service established or not.

#2 Post by arzgi »

Code: Select all

#!/bin/bash

[start Apache] && [next]


&& operator starts the second only if the first was succesfull

zero00
Posts: 1
Joined: 2020-05-12 13:30

Re: A bash script to test a service established or not.

#3 Post by zero00 »

Hi,

This can help you..

Code: Select all

#!/bin/bash

systemctl restart apache2

if [ $? = '0'  ]; then
        < next command >
fi

macky
Posts: 9
Joined: 2018-11-11 16:18

Re: A bash script to test a service established or not.

#4 Post by macky »

Might work as well on mariadb.

Code: Select all

#!/bin/bash
/usr/bin/systemctl status mariadb.service | grep "stopped\|dead"
if [ $? -eq 0 ]; then

	printf 'mariadb is not running....'

	sudo /usr/bin/systemctl start mariadb.service

else

     printf  'mariadb is running'
fi

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: A bash script to test a service established or not.

#5 Post by reinob »

hack3rcon wrote:Hello,
I want to write a bash script to restart a service and if the service launched successfully and established then execute the next command.
For example, restart the Apache service and when the service established then the next command execute.

Thank you.
You might also want to tell us what you actually want, instead of how you think the solution should look like.
If you need a service to depend on another (apache), then systemd already has the means to implement that.
Perhaps your "next command" (or service) does not even need to be started after apache, even if you think it should be like that.

User avatar
Head_on_a_Stick
Posts: 13450
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: A bash script to test a service established or not.

#6 Post by Head_on_a_Stick »

macky wrote:

Code: Select all

#!/bin/bash
/usr/bin/systemctl status mariadb.service | grep "stopped\|dead"
if [ $? -eq 0 ]; then

	printf 'mariadb is not running...'

	sudo /usr/bin/systemctl start mariadb.service

else

     printf  'mariadb is running'
fi
If you're going to use bash then you should use the [[ test instead of [. But you don't need bash and you don't need the test either:

Code: Select all

#!/bin/sh
if systemctl status mariadb.service | grep 'stopped\|dead' >/dev/null 2>&1; then
   printf 'mariadb is not running...\n'
   sudo systemctl start mariadb.service
else
   printf 'mariadb is runnning\n'
fi
Or use the actual command that is intended for this purpose:

Code: Select all

#!/bin/sh
if systemctl is-active mariadb >/dev/null 2>&1; then
   printf 'mariadb is running\n'
else
   printf 'mariadb is not running...\n'
   sudo systemctl start mariadb
fi
Note also the new line after the printf statement ;)

EDIT: actually mariadb.service has:

Code: Select all

# Restart crashed server only, on-failure would also restart, for example, when
# my.cnf contains unknown option
Restart=on-abort
RestartSec=5s
So no script is needed at all.

An override file could be used to change "Restart=on-abort" to "Restart=on-failure":

Code: Select all

# systemctl edit mariadb.service
Then:

Code: Select all

[Service]
Restart=
Restart=on-failure
Black Lives Matter

Debian buster-backports ISO image: for new hardware support

Post Reply