Link to home
Start Free TrialLog in
Avatar of Daniele Brunengo
Daniele BrunengoFlag for Italy

asked on

CentOS crontab doesn't work

Hello, I'm trying to understand why the heck my CentOS server won't run crontab as it should. All other crons run just fine, but I have setup the system crontab as shown below. It should run backup.sh at midnight, but it doesn't.

This is the job definition in the /etc/crontab file

0 0 * * * root /home/backup.sh

Open in new window


Here's part of today's logs:

Mar 23 23:00:01 srvdati CROND[17331]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:01:01 srvdati CROND[17337]: (root) CMD (run-parts /etc/cron.hourly)
Mar 23 23:01:01 srvdati run-parts(/etc/cron.hourly)[17337]: starting 0anacron
Mar 23 23:01:01 srvdati run-parts(/etc/cron.hourly)[17346]: finished 0anacron
Mar 23 23:01:01 srvdati run-parts(/etc/cron.hourly)[17337]: starting mcelog.cron
Mar 23 23:01:01 srvdati run-parts(/etc/cron.hourly)[17365]: finished mcelog.cron
Mar 23 23:10:01 srvdati CROND[17506]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:20:01 srvdati CROND[17651]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:30:01 srvdati CROND[17795]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:40:01 srvdati CROND[17941]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:50:01 srvdati CROND[18087]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 23 23:53:01 srvdati CROND[18095]: (root) CMD (/usr/lib64/sa/sa2 -A)
Mar 24 00:00:01 srvdati CROND[18240]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 00:01:01 srvdati CROND[18246]: (root) CMD (run-parts /etc/cron.hourly)
Mar 24 00:01:01 srvdati run-parts(/etc/cron.hourly)[18246]: starting 0anacron
Mar 24 00:01:01 srvdati anacron[18258]: Anacron started on 2017-03-24
Mar 24 00:01:01 srvdati anacron[18258]: Jobs will be executed sequentially
Mar 24 00:01:01 srvdati anacron[18258]: Normal exit (0 jobs run)
Mar 24 00:01:01 srvdati run-parts(/etc/cron.hourly)[18260]: finished 0anacron
Mar 24 00:01:01 srvdati run-parts(/etc/cron.hourly)[18246]: starting mcelog.cron
Mar 24 00:01:01 srvdati run-parts(/etc/cron.hourly)[18279]: finished mcelog.cron
Mar 24 00:10:01 srvdati CROND[18420]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 00:20:01 srvdati CROND[18564]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 00:30:01 srvdati CROND[18709]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 00:40:01 srvdati CROND[18855]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 00:50:01 srvdati CROND[19000]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:00:01 srvdati CROND[19142]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:01:01 srvdati CROND[19150]: (root) CMD (run-parts /etc/cron.hourly)
Mar 24 01:01:02 srvdati run-parts(/etc/cron.hourly)[19150]: starting 0anacron
Mar 24 01:01:02 srvdati anacron[19162]: Anacron started on 2017-03-24
Mar 24 01:01:02 srvdati anacron[19162]: Jobs will be executed sequentially
Mar 24 01:01:02 srvdati anacron[19162]: Normal exit (0 jobs run)
Mar 24 01:01:02 srvdati run-parts(/etc/cron.hourly)[19164]: finished 0anacron
Mar 24 01:01:02 srvdati run-parts(/etc/cron.hourly)[19150]: starting mcelog.cron
Mar 24 01:01:02 srvdati run-parts(/etc/cron.hourly)[19183]: finished mcelog.cron
Mar 24 01:10:01 srvdati CROND[19262]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:20:01 srvdati CROND[19407]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:30:01 srvdati CROND[19553]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:40:01 srvdati CROND[19698]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 24 01:50:01 srvdati CROND[19843]: (root) CMD (/usr/lib64/sa/sa1 1 1)

Open in new window


I have no emails in the root mailbox either, don't know what else to check.
Avatar of gheist
gheist
Flag of Belgium image

Do you need job to run at exact second? Maybe you can introduce backup in /etc/cron.daily/* setting good times for it in /etc/anacrontab

Is your script executable (+x), is it unwritable by others (go-w) and needs no console input?
You can add -x to crond_arguments in /etc/sysconfig/crond to have more logs on why it rejects script

Are all programs specified with full paths and full extra environment (cron does not parse various (.)*profile files)
Does the script start with #!... ? Is it /bin/bash?
You can add -x to most shells in that place to have them print all commands it runs.
Can you show the script?
Avatar of Daniele Brunengo

ASKER

The script works perfectly if I run it by hand, and doesn't need any user input.

Anyway I've tried for instance with this script:
#!/bin/bash
mail -s "PROVA" myemail@mydomain.com <<< 'TEST'

Open in new window


Adding -x to crond arguments resulted in crond not starting (unknown command).

Anyway, there is no reference anywhere to these unexecuted scripts.
Avatar of arnold
Why do you gave root?
Try
0 0 * * * /home/backup.sh

Open in new window


List the permissions of /home/backup.sh
If you in the shell execute the script /home/backup.sh

You are using commands (mail) presuming that mail us in the path.
IMHO, scripts should inject messages directly into /usr/sbin/sendmail versus using email client ...
The direct injection provides the script creator ...........
Please don't focus on the script, no script is working with this method. The system is not even trying to load the script.

About root, isn't the syntax supposed to be like this:

*  *  *  *  * user-name command
In your posting of the log, there was no reference to the line you posted. In which crontab is the line? Are there other lines in there?
Mar 24 00:00:01 srvdati CROND[18240]: (root) CMD (/usr/lib64/sa/sa1 1 1)

I do not see a reference  to cron trying at 0:0 to run this script suggests ..........
While elevating your rights, run crontab -e us this where the command is.
Or is it in your local user (non root) crontab.
Usually to run a command as specific user the su -u user -c command ....
crontab -e shows you the user crontab, which is empty. This is the system crontab, located in /etc/crontab.

There is no reference to the script and that's exactly the problem. The system isn't even trying to run the script and I don't know why.
I'd expect to find it in /etc/cron.d not /etc/crontab
There is cron, Anacron, it is kit clear that your reference is correct.


if you need to run it daily, add it to root's crontab or to the Anacron cron.daily.
ASKER CERTIFIED SOLUTION
Avatar of Daniele Brunengo
Daniele Brunengo
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So this is the correct code at the end of the file:

0 0 * * * root /home/backup.sh
(and then a line break)

Open in new window

glad to see.
It was a simple mistake on my part.