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?










share|improve this question







New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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?










share|improve this question







New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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?










share|improve this question







New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









JohnC--

112




112




New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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


















  • 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
















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










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.






share|improve this answer








New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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.






share|improve this answer








New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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















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.






share|improve this answer








New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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













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.






share|improve this answer








New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









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.







share|improve this answer








New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 2 days ago









JohnC--

112




112




New contributor




JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JohnC-- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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




    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










JohnC-- is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

數位音樂下載

格利澤436b

When can things happen in Etherscan, such as the picture below?