-4

I'm trying to deploy my war file to Tomcat 7 from jenkins. I'm writing a shell scripting program in jenkins to start, shutdown and restart tomcat every 15 minutes. my code is going wrong. Can anyone help me with this?

*/15 * * * * /home/user_name/ Documents/tomcat/bin/startup.sh

Grace Green
  • 49
  • 2
  • 11

2 Answers2

0

as root do:

echo "*/15 * * * * service tomcat restart" | crontab -

for learning try:

man crontab
man 5 crontab
man service

If you need only a script that "check[s] if the Tomcat is up or not if not then start[s] it automatically" as you write below in a comment then that script would be:

 service tomcat status || service tomcat start
  • I am a novice with shell scripting and have written some minor shell scripts to copy files and such that. Now I have a requirement to write a shell script to go check if Tomcat running in the same server is up or not, if not then run the Tomcat startup script. Then put this script in jenkins. At this point I have few ideas but I just don't know how to bind them into a running script or even know if its the right direction to follow – Grace Green Nov 19 '17 at 18:58
  • I just want to create a shell script that will be put in a Jenkins job to run every say 15 minutes daily and the script will check if the Tomcat is up or not if not then start it automatically without having to do it manually. – Grace Green Nov 19 '17 at 19:00
  • I tried with that code. it's not recognizing it. can you check the question once now please. – Grace Green Nov 21 '17 at 05:12
  • try to format your code in your question in a way that is makes sense. The way the code in your question is formated now it doesn't make much sense. Did you read `man service`? – Tomáš Pospíšek Nov 21 '17 at 07:24
  • yeah, you can see it now – Grace Green Nov 21 '17 at 09:01
  • what are the exact symptoms of "it's not recognizing it"? Is there an error? What error? Does Jenkins there to be a shell script or a command? If it expects a command, then you'll need to save the shell script in a file, `chmod +x $that_script_file` and put the full path of that script file into that form in the Jenkins web interface. – Tomáš Pospíšek Nov 23 '17 at 08:58
  • Sorry for the late response. Actually I've set-up it manually. "startup.sh" file is in my Documents folder. "*/15 * * * * /Documents/tomcat/bin/startup.sh". This throws an error that 15 is not recognised – Grace Green Nov 28 '17 at 14:10
  • Where did you put this into: `*/15 * * * * /Documents/tomcat/bin/startup.sh`? – Tomáš Pospíšek Nov 28 '17 at 19:54
  • I've put this in cron.hourly folder. Is the script correct? – Grace Green Nov 29 '17 at 04:06
  • `/etc/cron.(daily|hourly|weekly)` all contain normal executables which will get executed at the indicated intervals. In contrast `/etc/crontab` and `/etc/cron.d` contain instructions on *when* and *how* to run what. So what you can do is to either replace the above into `/etc/crontab` (as a line) or `/etc/cron.d` (as a file) or to leave the the file where it is but then replace the contents with a correct shell script: `#!/bin/bash` (newline) `/absolute/path/to/Documents/tomcat/bin/startup.sh` – Tomáš Pospíšek Nov 29 '17 at 08:03
  • So or so you have to replace `/Documents/tomcat/bin/startup.sh` with the correct absolute path. That would be something like `/home/your_username/Documents/tomcat/bin/startup.sh`. – Tomáš Pospíšek Nov 29 '17 at 08:04
  • Tried your program several times, it's showing an error of 15 not being recognised – Grace Green Dec 03 '17 at 08:06
  • Maybe you can bring the issue up on the Jenkins users' mailing list: https://jenkins.io/mailing-lists/ – Tomáš Pospíšek Dec 04 '17 at 08:05
  • Thanks for the help Tomas – Grace Green Dec 12 '17 at 06:28
0

I also had same problem. I wanted to restart tomcat when my web application is not responding. So, i wrote below script and added to crontab.

restart_tomcat.sh

#!/bin/bash

export JAVA_HOME=/opt/jdk1.7.0_79
export PATH=$PATH:/opt/jdk1.7.0_79/bin

status="$(/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_my_webapp| awk -F':' '{print($1)}')"

if [ "$status" != "HTTP OK" ]
then
    echo "`date` ---- Restarting tomcat"
    /path/to/apache-tomcat-8.5.6/bin/shutdown.sh
    sleep 15
    /path/to/apache-tomcat-8.5.6/bin/startup.sh
fi

crontab

*/10 * * * * /path/to/restart_tomcat.sh.sh
Hasitha
  • 738
  • 8
  • 16