Problem with my bash script or crontab
up vote
0
down vote
favorite
My LAMP installation on Ubuntu 18.04 LTS is working fine. But sometimes when MySQL is down it doesn't gets restarted automatically. So i google around and found below the bash script which checks whether mysql is running and will start the service if it is not running. Problem is that when mysql is down i receive the email but mysql is not started. Also, the user running the cron job has sudo access. My cronjob is: */5 * * * * sudo bash check_mysql.sh > /dev/null 2>&1
The bash script i used:
#!/bin/bash
email='myemail@example.com'
subject='mysql process down and up'
MYSQL_START='sudo /etc/init.d/mysql start'
MYSQL='mysqld'
PGREP='/usr/bin/pgrep'
#check pid
$PGREP $MYSQL
if [ $? -ne 0 ]; then
$MYSQL_START | mail -s "$subject" $email <<< 'Mysql was down and successfully started'
fi
Is there anything wrong with the script or the cronjob is wrong?
command-line bash mysql cron
New contributor
add a comment |
up vote
0
down vote
favorite
My LAMP installation on Ubuntu 18.04 LTS is working fine. But sometimes when MySQL is down it doesn't gets restarted automatically. So i google around and found below the bash script which checks whether mysql is running and will start the service if it is not running. Problem is that when mysql is down i receive the email but mysql is not started. Also, the user running the cron job has sudo access. My cronjob is: */5 * * * * sudo bash check_mysql.sh > /dev/null 2>&1
The bash script i used:
#!/bin/bash
email='myemail@example.com'
subject='mysql process down and up'
MYSQL_START='sudo /etc/init.d/mysql start'
MYSQL='mysqld'
PGREP='/usr/bin/pgrep'
#check pid
$PGREP $MYSQL
if [ $? -ne 0 ]; then
$MYSQL_START | mail -s "$subject" $email <<< 'Mysql was down and successfully started'
fi
Is there anything wrong with the script or the cronjob is wrong?
command-line bash mysql cron
New contributor
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
You don’t needsudo
in a script for the system’s cron, it is run by root.
– dessert
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My LAMP installation on Ubuntu 18.04 LTS is working fine. But sometimes when MySQL is down it doesn't gets restarted automatically. So i google around and found below the bash script which checks whether mysql is running and will start the service if it is not running. Problem is that when mysql is down i receive the email but mysql is not started. Also, the user running the cron job has sudo access. My cronjob is: */5 * * * * sudo bash check_mysql.sh > /dev/null 2>&1
The bash script i used:
#!/bin/bash
email='myemail@example.com'
subject='mysql process down and up'
MYSQL_START='sudo /etc/init.d/mysql start'
MYSQL='mysqld'
PGREP='/usr/bin/pgrep'
#check pid
$PGREP $MYSQL
if [ $? -ne 0 ]; then
$MYSQL_START | mail -s "$subject" $email <<< 'Mysql was down and successfully started'
fi
Is there anything wrong with the script or the cronjob is wrong?
command-line bash mysql cron
New contributor
My LAMP installation on Ubuntu 18.04 LTS is working fine. But sometimes when MySQL is down it doesn't gets restarted automatically. So i google around and found below the bash script which checks whether mysql is running and will start the service if it is not running. Problem is that when mysql is down i receive the email but mysql is not started. Also, the user running the cron job has sudo access. My cronjob is: */5 * * * * sudo bash check_mysql.sh > /dev/null 2>&1
The bash script i used:
#!/bin/bash
email='myemail@example.com'
subject='mysql process down and up'
MYSQL_START='sudo /etc/init.d/mysql start'
MYSQL='mysqld'
PGREP='/usr/bin/pgrep'
#check pid
$PGREP $MYSQL
if [ $? -ne 0 ]; then
$MYSQL_START | mail -s "$subject" $email <<< 'Mysql was down and successfully started'
fi
Is there anything wrong with the script or the cronjob is wrong?
command-line bash mysql cron
command-line bash mysql cron
New contributor
New contributor
New contributor
asked 2 days ago
JohnC--
112
112
New contributor
New contributor
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
You don’t needsudo
in a script for the system’s cron, it is run by root.
– dessert
2 days ago
add a comment |
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
You don’t needsudo
in a script for the system’s cron, it is run by root.
– dessert
2 days ago
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
You don’t need
sudo
in a script for the system’s cron, it is run by root.– dessert
2 days ago
You don’t need
sudo
in a script for the system’s cron, it is run by root.– dessert
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
After digging more on google, i change the command from:
sudo /etc/init.d/mysql start
to
sudo service mysql start
and now it works.
New contributor
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
After digging more on google, i change the command from:
sudo /etc/init.d/mysql start
to
sudo service mysql start
and now it works.
New contributor
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
add a comment |
up vote
1
down vote
After digging more on google, i change the command from:
sudo /etc/init.d/mysql start
to
sudo service mysql start
and now it works.
New contributor
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
After digging more on google, i change the command from:
sudo /etc/init.d/mysql start
to
sudo service mysql start
and now it works.
New contributor
After digging more on google, i change the command from:
sudo /etc/init.d/mysql start
to
sudo service mysql start
and now it works.
New contributor
New contributor
answered 2 days ago
JohnC--
112
112
New contributor
New contributor
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
add a comment |
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
1
1
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
Can you please mark your answer as correct so that askubuntu closes the question
– akabhirav
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
@akabhirav since i am new to the community i can do it only after 2 days.
– JohnC--
2 days ago
add a comment |
JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.
JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.
JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.
JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094117%2fproblem-with-my-bash-script-or-crontab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is the output of systemctl status mysql.service (or mysql) ?
– Manula Waidyanatha
2 days ago
You don’t need
sudo
in a script for the system’s cron, it is run by root.– dessert
2 days ago