A bash script to test a service established or not.

Need help with C, C++, perl, python, etc?

A bash script to test a service established or not.

Postby hack3rcon » 2020-03-22 20:04

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.
hack3rcon
 
Posts: 443
Joined: 2015-02-16 09:54

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

Postby arzgi » 2020-03-23 13:58

Code: Select all
#!/bin/bash

[start Apache] && [next]




&& operator starts the second only if the first was succesfull
arzgi
 
Posts: 654
Joined: 2008-02-21 17:03
Location: Finland

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

Postby zero00 » 2020-05-12 13:36

Hi,

This can help you..

Code: Select all
#!/bin/bash

systemctl restart apache2

if [ $? = '0'  ]; then
        < next command >
fi
zero00
 
Posts: 1
Joined: 2020-05-12 13:30

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

Postby macky » 2020-09-02 06:02

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
macky
 
Posts: 9
Joined: 2018-11-11 16:18

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

Postby reinob » 2020-09-02 12:54

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.
reinob
 
Posts: 819
Joined: 2014-06-30 11:42

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

Postby Head_on_a_Stick » 2020-09-02 16:46

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
User avatar
Head_on_a_Stick
 
Posts: 12640
Joined: 2014-06-01 17:46
Location: /dev/chair


Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable