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.
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.
Re: A bash script to test a service established or not.
Code: Select all
#!/bin/bash
[start Apache] && [next]
&& operator starts the second only if the first was succesfull
Re: A bash script to test a service established or not.
Hi,
This can help you..
This can help you..
Code: Select all
#!/bin/bash
systemctl restart apache2
if [ $? = '0' ]; then
< next command >
fi
Re: A bash script to test a service established or not.
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
Re: A bash script to test a service established or not.
You might also want to tell us what you actually want, instead of how you think the solution should look like.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.
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.
- 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.
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: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
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
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
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
An override file could be used to change "Restart=on-abort" to "Restart=on-failure":
Code: Select all
# systemctl edit mariadb.service
Code: Select all
[Service]
Restart=
Restart=on-failure
deadbang