0

I have set several cron jobs. I recently added a new cron job that copies a file of last day from another server and is executed each day (for example at 06:00 am) but when I check next day the file hasn't been copied.

When I arrrive office and realize that job wasn't executed, then I edit the job with crontab -e (with same root user) and set the job to be executed in next 30 min. This time the cron job is executed successfully.

Why could be that the job is not being executed at 04:00 or 06:00 am but during day it works?

How can I check the reason why the job is not executed?

I'm working in GNU/Linux CentOS (2.6.32) system. Thanks for any help.

UPDATE

The files that I need to copy are created in the origin server before 23:00 hours.

I had the cron job like this: (The first line is the job that fails. related script to copy the files is script1.sh)

[root@srvc ~]# crontab -l
0 6 * * * /path/to/scripts/script1.sh
0 5 * * * /path/to/scripts/script3.sh
0 8 * * 1 /path/to/scripts/script4.sh

When I checked today about 11:00 am that file wasn't copied, I edited cron job to be executed at 11:10 am and the file was copied successfully.

[root@srvc ~]# crontab -l
10 11 * * * /path/to/scripts/script1.sh

The script1.sh content is this:

#!/bin/bash

dyear=`date +'%Y' -d "1 day ago"`
dmonth=`date +'%b' -d "1 day ago"`
ddate=`date +%Y-%m-%d -d "1 day ago"`

sshpass -p 'ThePassword' scp -r [email protected]:/path/to/files/*$ddate* /Destination/path/$dyear/$dmonth/

The files to be copied have in their name the format Logfile.2019-02-20

3
  • Can you please update the question with the scheduling numbers. IE the first 5 fields on the line for your job that is not running?
    – Eddie Dunn
    Mar 5, 2019 at 18:10
  • Hi Eddi, I've added an update. Thanks
    – Ger Cas
    Mar 5, 2019 at 18:44
  • Well that part looks fine. I would check out the submitted answerer's suggestions.
    – Eddie Dunn
    Mar 5, 2019 at 18:58

1 Answer 1

0

Lots of reasons.

It is impossible to tell why the job has not run, and perhaps it ran without copying the file. Because the file wasn't there yet. Because the server was down. Because the network had a problem. Because ssh was temporarily stopped on the other machine. Pick a few.

If you want to know what happened, log events. Put in the start of your cronjob

logger "Starting my cronjob"

and, in stead of scp someone@server:file /somewhere, do

ssh someone@server ls -l file | logger
scp someone@server:file /somewhere 
ls -l /somewhere/file | logger

and finally at the end of the cronjob:

logger "This cronjob has ended"

What I put here is perhaps a bit exaggerated, but the point is, that if you don't provide yourself with information, you will never know what is happening. We might be debugging your crontab-rule, when the problem is acually in your scp.

Oh, yeah, cron may also have put some stuff in your syslog.

1
  • Hi Ljm, may you check my update in my original post for you can see the small script I call in the cron job. Where should I add the logger option in my script in order to work and where that log will be stored?
    – Ger Cas
    Mar 5, 2019 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .