Script to Restart JIRA automatically

David Malka June 23, 2015

My JIRA server occasionally dies.  I've not been able to find any 'out of memory' errors, or anything else in the logs that would explain this.  So I give up and do the following little script:

#/bin/sh
#### script to check every ten minutes if JIRA is up and restart it if it isn't
mailPerson="jiraadmin@mydomain.com
statusFile="/tmp/status"
/bin/ps -ef |/bin/grep -q jira
lastStatus=$?
if [ $lastStatus != "0" ]; then
    /etc/init.d/jira start
    statusStatus="The JIRA service was down"
      /bin/cat << EOF > $statusFile
      I have just had to restart JIRA!!
EOF
/bin/cat $statusFile | mail -s "$statusStatus" $mailPerson
fi
exit 0

I set this in root's cron, and set it for every ten minutes.

In a nutshell: it doesn't work.

The same script is running for Confluence (with the ps suitably altered), and that doesn't work either.

Ideas ?

4 answers

0 votes
rrudnicki
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 25, 2015

Hi David, 

 

Your double quotes on 0 are correct. I believe you are expecting a wrong return from the $? variable. 

Have a look in my example:

[root@jira-clone ~]# ls /tmp
VMwareDnD  atlassian-confluence-5.8.4-x64.bin  hsperfdata_root  pluginversioncheckerthing  pulse-Pzck7gM0mRUD  ssl  ssl2  status  vmware-root  vmware-root-1857948889
[root@jira-clone ~]# 
[root@jira-clone ~]# echo $?
0
[root@jira-clone ~]# 
[root@jira-clone ~]# ls /directory-doesnt-exist
ls: cannot access /directory-doesnt-exist: No such file or directory
[root@jira-clone ~]# 
[root@jira-clone ~]# echo $?
2
[root@jira-clone ~]#

 

Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

Some commands return additional exit statuses for particular reasons. For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure.

 

I've made some tests, and I believe what is causing this bad behaviour is your -q parameter. Try to run your script with no -q parameter on grep. 

Another thing you can do to start your scripts and run them in debug mode. You just need to add -x after /bin/sh: Like this:

/bin/sh -x

 

Hope this helps. 

Cheers, 

Renato Rudnicki

 

 

0 votes
David Malka June 24, 2015

Hey Renato,

Yeah, that's what I did.  There was also an issue with the double quotes around the 0, but it seems to be working now.

 

Thanks.  smile

0 votes
rrudnicki
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 24, 2015

Hi David, 

 

If you want to skip the returns of grep line, you can use this:

/bin/ps -ef |/bin/grep -q jira | grep -v grep

 

Regards, 

Renato Rudnicki

0 votes
David Malka June 23, 2015

Got it.  It was returning '0', because the grep was finding itself!

Suggest an answer

Log in or Sign up to answer