LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2015, 07:56 AM   #1
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Rep: Reputation: Disabled
Question Cron job starts 6 hours late, rest of cron jobs work fine


Hi all,

I am a sysadmin, but I mostly have experience in Windows.
Since Linux is much more attractive to businesses I'm now learning the ropes.

At my workplace we use Ubuntu 14.04 (we have around 7/8 linux servers), and we have a set of cron jobs that are executed everyday around midnight (01:00 or 01:30), but one machine in particular executes the cron jobs way too late (it begins around 07:30).

I already searched in dmesg, an in /var/log/syslog, but I'm not getting any wiser. I also Googled the problem, and various sites tell me that the timezone may be set-up wrong, but that's not the case.

Here I'll give an example of the output of the "date" command.
The first output is from the server in question with the cron problem:

root@caccini:/var/log# date
Fri Sep 18 14:46:54 CEST 2015

and a few moments later:

phouwen@encina:~$ date
Fri Sep 18 14:47:19 CEST 2015

Can anyone assist me or point me in the right direction?

(I also found that maybe if I ran "date -u", it would give a diffirent time, so here it goes:

root@caccini:/etc# date -u
Fri Sep 18 12:54:58 UTC 2015
phouwen@encina:~$ date -u
Fri Sep 18 12:55:03 UTC 2015
)


Thanks!

Last edited by pieterhouwen; 09-18-2015 at 08:15 AM.
 
Old 09-18-2015, 08:30 AM   #2
cgtueno
Member
 
Registered: Jul 2003
Posts: 363

Rep: Reputation: 50
A couple of things offered for consideration:

1) are you editing the crontab file that is actually being used by cron, and did you restart cron after editing crontab to make it read the changes ?
2) are you aware that: if a user was not allowed to execute jobs when their crontab was last edited (ie. user was not listed in cron.allow),
then the user needs to be added to cron.allow AND THEN crontab needs to be re-edited to allow that user's jobs to execute, and CRON needs to be restarted to pickup the changes.
3) do you have a process running on the ssytem around that time, that is actually blocking the scrip from executing ?

I suggest that you write a quick script that dumps the date/time, and add that job to crontab, then restart cron (to pick up the job changes).
Use that approach to ensure that the job is starting at the correct system time. (Definitive evidence).
Even consider adding a dup of the active processes to that script.

If you have a mis-match between the crontab scheduled job time and the execution of the test job then you have your answer.

C.
 
Old 09-18-2015, 09:34 AM   #3
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
If you use the approved way of editing your crontab i.e. by using the crontab command, there should be no need to restart anything.
crontab -l from the appropriate user will display your settings - perhaps you mistyped a setting?

date gives your local time, including DST.

date -u gives the UTC time.

Last edited by JeremyBoden; 09-18-2015 at 09:39 AM.
 
Old 09-18-2015, 10:29 AM   #4
cgtueno
Member
 
Registered: Jul 2003
Posts: 363

Rep: Reputation: 50
JeremyBoden

Eeek!

(Smiles) Point taken. But I am a purist and prefer to edit crontab directly.

C.
 
Old 09-28-2015, 05:04 AM   #5
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Hi all, thanks for your quick reply.

Executing "crontab -l" gives me the following:

root@caccini:/etc# crontab -l
no crontab for root

While "nano /etc/crontab" gives me:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
01 0 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 4 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 5 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

Any suggestions?
 
Old 09-28-2015, 06:28 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
crontab -l lists the cron jobs of the active user.

the /etc/crontab files are system wide cron jobs. Mostly used by services. Also logrotate run through the crontab settings (should be /etc/cron.daily).
Check out the directories given and see which cronjobs are there. Guess /etc/cron.daily is where yours are hiding. Coming from the time you wrote in your first post.
As your settings for cron.daily is scheduled for the first hour of the day (01 0 * * *) you need to check why some jobs are run at 1:30 (also taken from your first post). Either the crontab was edited or there is a sleep 1800 at the start of the scripts to have it sleep for half a hour and then go to work. Maybe someone used this method but did the math wrong? grep "sleep" /etc/cron.* for a quick hint.

Can you post the time of the systems that are running correctly. Just to be sure that the time is good on all systems. Maybe use ntp on all machines?
 
Old 09-28-2015, 06:36 AM   #7
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
What user did you create the crontab for?
Try
Code:
sudo crontab -u username -l
(where username is the user who you specified the crontab for).

I've never tried to get root to run crontab jobs via the crontab mechanism - its a bit insecure isn't it?

However, root will run the scripts in /etc/cron.daily on a daily basis.
Similarly for hourly(?), weekly & monthly - it isn't usually time-critical as to when a root job is run.
 
Old 09-28-2015, 06:41 AM   #8
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Grep "sleep" /etc/cron.* did not give me any useful information:

root@caccini:/etc# grep "sleep" cron.*
grep: cron.d: Is a directory
grep: cron.daily: Is a directory
grep: cron.hourly: Is a directory
grep: cron.monthly: Is a directory
grep: cron.weekly: Is a directory
root@caccini:/etc#

I can't seem to figure out why. Also, I'm gonna edit the title of this post seeing as it's not only that one cron job that doesn't start at specified time, but all of them. (although there are some differences in how much too late they run.

And Jeremy, the specified command didn't give any valuable output:

root@caccini:~# crontab -u root -l
no crontab for root


---------- Post added 09-28-15 at 01:42 PM ----------

Last edited by pieterhouwen; 09-28-2015 at 06:43 AM.
 
Old 09-28-2015, 06:46 AM   #9
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
I can't seem to find an option which enables me to change the title of the thread. Perhaps I'm overlooking something?
 
Old 09-28-2015, 10:14 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by pieterhouwen View Post
root@caccini:~# crontab -u root -l
no crontab for root
Then it sounds like root doesn't have a crontab. How exactly did you add this command to the cron?

Last edited by suicidaleggroll; 09-28-2015 at 10:15 AM.
 
Old 09-28-2015, 10:15 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by JeremyBoden View Post
I've never tried to get root to run crontab jobs via the crontab mechanism - its a bit insecure isn't it?
I don't see why it would be. Could you share why you think it would be a problem?
 
Old 09-28-2015, 10:46 AM   #12
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
The crontab implementation either runs as a non-root user that is able to gain access to run as root
or crontab runs as a root daemon that can safely start non-root user applications.

Either way, running arbitrary commands/scripts by a process that switches its effective user name seems a little bit fraught?

BTW
Code:
man crontab
says, in part...
Quote:
If the /etc/cron.allow file exists, then you must be listed (one user per line) therein in order to be allowed to use
this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be
listed in the /etc/cron.deny file in order to use this command.

If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be
allowed to use this command, or all users will be able to use this command.

If both files exist then /etc/cron.allow takes precedence. Which means that /etc/cron.deny is not considered and your
user must be listed in /etc/cron.allow in order to be able to use the crontab.

Regardless of the existance of any of these files, the root administrative user is always allowed to setup a crontab.
For standard Debian systems, all users may use this command.
 
Old 09-29-2015, 02:58 AM   #13
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Then it sounds like root doesn't have a crontab. How exactly did you add this command to the cron?
Actually I didn't. It was already setup when I started working at the company. How can root have no crontab while in /etc/crontab every cron job is run by root?
 
Old 09-29-2015, 03:05 AM   #14
pieterhouwen
LQ Newbie
 
Registered: Sep 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pieterhouwen View Post
Grep "sleep" /etc/cron.* did not give me any useful information:

root@caccini:/etc# grep "sleep" cron.*
grep: cron.d: Is a directory
grep: cron.daily: Is a directory
grep: cron.hourly: Is a directory
grep: cron.monthly: Is a directory
grep: cron.weekly: Is a directory
root@caccini:/etc#
I re-ran the command a little different now, output is below:

root@caccini:/etc# grep "sleep" cron.*/*
cron.daily/apt:# sleep for a random interval of time (default 30min)
cron.daily/apt:random_sleep()
cron.daily/apt: debug_echo "sleeping for $TIME seconds"
cron.daily/apt: sleep $TIME
cron.daily/apt:# sleep random amount of time to avoid hitting the
cron.daily/apt:random_sleep

After looking at the code of apt a little more I see:

# sleep for a random interval of time (default 30min)
# (some code taken from cron-apt, thanks)
random_sleep()
{
RandomSleep=1800
eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
if [ $RandomSleep -eq 0 ]; then
return
fi

Could this be the source of the problems?
 
Old 09-29-2015, 03:22 AM   #15
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
No.

You have a cron job (called apt) that appears to be trying to run roughly every 30 minutes.
Many cron jobs can run simultaneously.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why is cron 4 hours late? Lenf2 Linux - Newbie 5 03-27-2013 07:20 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Cron running 4 hours late ResOrb Slackware 2 06-15-2007 07:45 AM
cron jobs work fine when invoked from webmin but fail on schedule jillu Linux - Enterprise 3 01-20-2005 01:34 PM
Cron/Vixie runs jobrotate 6 hours late SEL Linux - General 0 04-21-2002 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:36 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
Open Source Consulting | Domain Registration