LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 11-17-2017, 09:52 AM   #1
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Rep: Reputation: Disabled
cron question


I am having tomcat issues. For some reason tomcat will stop working after 20 minutes to 90 minutes. and connections to mysql will soar due to unclosed connections. This brings everything down.

So I want to run a cron to restart tomcat every 30 minutes. I know its not a good solution but I need to keep things running while trying to find whats wrong. Trouble is the software hasn't changed on my end and its been working for several years, so maybe its an OS change. Or maybe something else.

the cron I want to use is

*/30 * * * * service tomcat restart

I put it in an existing con but it just isn't working.

Any ideas? Or does anyone have a cron to so this?

Lou
 
Old 11-17-2017, 11:09 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,189

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
I assume tomcat has to be restarted as root. So you have to put this either in root's crontab, or in the system crontab. I recommend the latter.

The system crontab is /etc/crontab.

The is one additional column: the user under which credentials the command has to run. So the line becomes:
Code:
*/30 * * * * root service tomcat restart
"Isn't working" does not help much. Check /var/log/syslog for lines containing CRON. It will tell you why it is not running

jlinkels
 
Old 11-17-2017, 11:15 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,585
Blog Entries: 15

Rep: Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537
Quote:
Originally Posted by LouW View Post
the cron I want to use is

*/30 * * * * service tomcat restart

I put it in an existing con but it just isn't working.
HOW exactly did you put it in? There are various cron files. The ones in /var/spool/crontab are per user crontabs but should not be directly edited. Instead you should use "crontab -e <user>" (or if you're already the user just "crontab -e". This insures the cron daemon will pick up your changes. If you directly edit the file instead you have to restart cron for it to see the changes.
 
Old 11-17-2017, 11:19 AM   #4
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
can't find file there

Quote:
Originally Posted by jlinkels View Post
I assume tomcat has to be restarted as root. So you have to put this either in root's crontab, or in the system crontab. I recommend the latter.

The system crontab is /etc/crontab.

The is one additional column: the user under which credentials the command has to run. So the line becomes:
Code:
*/30 * * * * root service tomcat restart
"Isn't working" does not help much. Check /var/log/syslog for lines containing CRON. It will tell you why it is not running

jlinkels
So I checked that directory and no system log. I did add root to the line, lets see if that helps!
 
Old 11-17-2017, 11:21 AM   #5
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
placement

Quote:
Originally Posted by MensaWater View Post
HOW exactly did you put it in? There are various cron files. The ones in /var/spool/crontab are per user crontabs but should not be directly edited. Instead you should use "crontab -e <user>" (or if you're already the user just "crontab -e". This insures the cron daemon will pick up your changes. If you directly edit the file instead you have to restart cron for it to see the changes.
I put it in 0yum-hourly.cron

crontab -e brings up a VERY confusing editor so not sure how to use taht
 
Old 11-17-2017, 12:00 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,082

Rep: Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506
Quote:
I put it in 0yum-hourly.cron
I assume that 0yum-hourly.cron is a file. What directory did you place the file?

There are many directories where you can put the file where it will run as a system cron task.
/etc/crontab
/etc/cron.d
/etc/cron.hourly
/etc/cron.daily
/etc/cron.monthly
/etc/cron.weekly

Code:
*/30 * * * * root /sbin/service tomcat restart
One addition to the above is to use the absolute path to the service command.
 
Old 11-17-2017, 12:01 PM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,585
Blog Entries: 15

Rep: Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537
OK I'm assuming you're on CentOS7 as I don't find that file on 6.

Interesting although file /etc/cron.hourly/0yum-hourly.cron does exist on 7 it calls a command that does NOT exist on 7 on my system so it isn't actually running.

For /etc/cron.hourly the idea is not to edit existing files (unless you're changing what those specifically do) but rather to add a new one for the purpose you want. There are also cron.daily and other directories under /etc. As specified by their directory names they run only once per hour, once per day etc...

You could no doubt create a job that did the restart once and hour, sleep for half and hour and restart by using cron.hourly but that would be klugey in my opinion.

crontab -e edits the main user cron files in /var/spool/cron as I mentioned earlier. By default you are likely using vim as your editor. If you prefer something else like nano or (God forbid) emacs you can set your variable to use that instead. From "man crontab":
Quote:
-e Edits the current crontab using the editor specified by the VIS‐
UAL or EDITOR environment variables. After you exit from the
editor, the modified crontab will be installed automatically.
Were it me I'd run crontab -e as root and add the entry. But I'd leave out the root since any job run by /var/spool/cron/root is automatically running as user root:
Code:
*/30 * * * * service tomcat restart
Finally if this is 7 as I suspect you should be using systemctl rather than service. 7 runs Systemd rather than init. Service was used for init scripts and systemctl is used for Systemd scripts. Some things can still be run with service but you should get in the habit of using the correct tools.
With systemctl the order is slightly different than with service. Instead of "service tomcat restart" you'd run "systemctl restart tomcat". Of course this assumes you have a systemd start script for tomcat. Test with "systemctl status tomcat".
 
Old 11-17-2017, 12:20 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,082

Rep: Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506
Hmm... I'm just running a basic VM CentOS 7 install and I don't anything any file called 0yum-hourly.cron but then I don't do automatic updates.

Last edited by michaelk; 11-17-2017 at 12:49 PM.
 
Old 11-17-2017, 12:22 PM   #9
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
I assume that 0yum-hourly.cron is a file. What directory did you place the file?

There are many directories where you can put the file where it will run as a system cron task.
/etc/crontab
/etc/cron.d
/etc/cron.hourly
/etc/cron.daily
/etc/cron.monthly
/etc/cron.weekly

Code:
*/30 * * * * root /sbin/service tomcat restart
One addition to the above is to use the absolute path to the service command.
It's in hourly
 
Old 11-17-2017, 12:24 PM   #10
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Hmm... I'm just running a basic VM CentOS 7 install and I don't anything any file called 0yum-hourly.cron
Here is the file

#!/bin/bash

*/30 * * * * root service tomcat restart

# Only run if this flag is set. The flag is created by the yum-cron init
# script when the service is started -- this allows one to use chkconfig and
# the standard "service stop|start" commands to enable or disable yum-cron.
if [[ ! -f /var/lock/subsys/yum-cron ]]; then
exit 0
fi

# Action!
exec /usr/sbin/yum-cron /etc/yum/yum-cron-hourly.conf
 
Old 11-17-2017, 01:20 PM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,082

Rep: Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506
A cron task is not a executable command and does not belong in a bash script.

Remove that line from your 0yum-hourly.cron file and as suggested above add the below to your /etc/crontab file. You can use any text editor but you must be root.

Code:
*/30 * * * * root /sbin/service tomcat restart
 
Old 11-17-2017, 01:36 PM   #12
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
A cron task is not a executable command and does not belong in a bash script.

Remove that line from your 0yum-hourly.cron file and as suggested above add the below to your /etc/crontab file. You can use any text editor but you must be root.

Code:
*/30 * * * * root /sbin/service tomcat restart
Hmm ok. But for some reason tomcat has maintained for over two hours now...
 
Old 11-17-2017, 02:45 PM   #13
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,399

Rep: Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784
Quote:
Originally Posted by LouW View Post

crontab -e brings up a VERY confusing editor so not sure how to use taht
That'd be vi or vim -- yes it's "unique" -- I don't use it, personally, except for when doing crontab -e
man vi (or search for vi/vim cheat sheet).

I'd also recommend using systemctl instead of service:
Code:
*/30 * * * * root /bin/systemctl restart tomcat.service
in /etc/crontab as michaelk already said.
 
Old 11-17-2017, 04:25 PM   #14
LouW
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
That'd be vi or vim -- yes it's "unique" -- I don't use it, personally, except for when doing crontab -e
man vi (or search for vi/vim cheat sheet).

I'd also recommend using systemctl instead of service:
Code:
*/30 * * * * root /bin/systemctl restart tomcat.service
in /etc/crontab as michaelk already said.
Ok I changed it to systemctl in crontab

How do I 'restart' this? It was working I think for about 6 hours then tomcat stopped. Webmin reported real memory used jumped to about 6GB out of 8 and cpu usage was at 71%. Normally its single digitals...
 
Old 11-17-2017, 05:31 PM   #15
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,399

Rep: Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784
Quote:
Originally Posted by LouW View Post
Ok I changed it to systemctl in crontab

How do I 'restart' this? It was working I think for about 6 hours then tomcat stopped. Webmin reported real memory used jumped to about 6GB out of 8 and cpu usage was at 71%. Normally its single digitals...
"Restart" of a cron isn't necessary, but have you run the command in the cron from the command line to see if it does what you want?

Reviewing the thread I see that you've never stated which version you're using. Others (including me) presumed it was 7, but you've never verified. systemctl is version 7 only. If you're not running CentOS 7, then changing to systemctl syntax will not work...go back to the service syntax.

BUT, you really need to figure out why tomcat is not working. What do the tomcat and/or http logs contain?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Cron directories: /var/spool/cron/cron.GfqqGO Woodsman Slackware 4 05-11-2011 03:37 PM
cron.hourly cron.weekly cron.monthly and 0anacron. Are they necessary? glore2002 Debian 2 09-30-2009 09:57 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 02:57 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 02:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 09:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration