LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   CentOS (https://www.linuxquestions.org/questions/centos-111/)
-   -   cron question (https://www.linuxquestions.org/questions/centos-111/cron-question-4175617810/)

LouW 11-17-2017 08:52 AM

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

jlinkels 11-17-2017 10:09 AM

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

MensaWater 11-17-2017 10:15 AM

Quote:

Originally Posted by LouW (Post 5781747)
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.

LouW 11-17-2017 10:19 AM

can't find file there
 
Quote:

Originally Posted by jlinkels (Post 5781769)
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!

LouW 11-17-2017 10:21 AM

placement
 
Quote:

Originally Posted by MensaWater (Post 5781772)
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

michaelk 11-17-2017 11:00 AM

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.

MensaWater 11-17-2017 11:01 AM

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".

michaelk 11-17-2017 11:20 AM

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.

LouW 11-17-2017 11:22 AM

Quote:

Originally Posted by michaelk (Post 5781786)
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

LouW 11-17-2017 11:24 AM

Quote:

Originally Posted by michaelk (Post 5781794)
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

michaelk 11-17-2017 12:20 PM

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

LouW 11-17-2017 12:36 PM

Quote:

Originally Posted by michaelk (Post 5781816)
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...

scasey 11-17-2017 01:45 PM

Quote:

Originally Posted by LouW (Post 5781774)

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.

LouW 11-17-2017 03:25 PM

Quote:

Originally Posted by scasey (Post 5781847)
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...

scasey 11-17-2017 04:31 PM

Quote:

Originally Posted by LouW (Post 5781881)
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?


All times are GMT -5. The time now is 07:49 AM.