Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

A bash script to test a service established or not.

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
hack3rcon
Posts: 746
Joined: 2015-02-16 09:54
Has thanked: 48 times

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: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 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: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 47 times

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: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

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
deadbang

Post Reply