Cron job scheduled is running once, but reports are generating twice

Team,

Hope you all are doing fine

I have one admin server which is being used dedicately to run cron jobs on hourly basis, fetching the details from Database which is in a different server.These cronjob are run on every hourly/5 minutes basis depending as per end user requirement.The script are based on admin server itself where the cron is enabled
The cron jobs are basically reports which end users need for their records

OS version is Red Hat Enterprise Linux Server release 6.6 where the cronjob is set

Now the tricky point:
1)Since last week, everything was getting run once, but when we query the database we are seeing duplicate entries for each cronjobs and the reports are published twice

2) Also, even when I have disabled/removed/changed the timestamp from crontab, we still see the duplicate entries on the database.

Need to find out why this is happening and its remedy,Can anyone guide please.

Regards
Whizkid

You might consider to add a few lines of code to your crontab scripts to write to a log file and record each time the cron file runs.

You might consider creating a new database table which records each time the database is queried by the cron file script and add login information and time stamps to the (new) db table, if one does not exist.

Basically, I am suggesting that if you have better logging in place, you can easily discover how things are running as they are.

Thanks Neo,

But i am trying to do things in the existing setup, the cron was working fine without any duplicate.
Also, I do have restrictions on creating/modifying the DB as its running live environment

You say,

in your thread title but how can you be so sure that it's only running once?

IMHO the most likely thing is that it's being spawned (by cron) more than once for some reason.

So what are the possible reasons?

  1. There is more that one crontab entry for this same job either both with the same user or once each with different users. To check look for your cron tables in /var/spool/cron (or possibly /var/spool/cron/crontabs ). In there you will find one file for each and every user that has cron jobs scheduled. You can list these to see the contents using cat . Look in all the user files for this job entry; as I said, one could be running, for example, as root, and a second as some other user. Do not edit these files directly without coming back for advice first. However, you should be able to use normal cron tools to remove any excess entry that you find.

  2. Perhaps, somehow, you have more than one cron daemon running. When the system boots it should start just one daemon. To check look for the cron daemon(s) in the ps -eaf listing.

Thank you Hick,

I have cross check it, daemon are running only once & also apart from the [/var/spool/cron/user1] I dont see any other user apart from user1 who has enabled the jobs running.. I confirm there is no duplicates.

So check the cron script itself to ensure that some editing session hasn't put in duplicate lines.

Logging is your friend :slight_smile:

1 Like

I have cross-checked it, and the cron jobs are set only once,and scripts are all fine no duplicate lines

So as neo suggested, why don't you insert a timestamp generation logging command into the script that will write a file every time the job runs.

What's the last modified time on the duplicate output reports that you have? Are they created/modified at exactly the same time?

Maybe something like this, just an example, in the script:

echo $(date +%s)  >> /tmp/mylogfile.txt
# echo $(date +%s)  >> /tmp/mylogfile.txt
# cat /tmp/mylogfile.txt
1561379037
# echo $(date +%s)  >> /tmp/mylogfile.txt
# cat /tmp/mylogfile.txt
1561379037
1561379165
# echo $(date +%s)  >> /tmp/mylogfile.txt
# cat /tmp/mylogfile.txt
1561379037
1561379165
1561379175

Or even something better :slight_smile:

1 Like

Thanks Neo/Hick.. giving it a try.. so logging is a remedy :slight_smile:

--- Post updated at 12:55 AM ---

@hick ..the duplicate record is in (45 seconds),from the first one

You need to be more specific in your analysis and posts.

For example:

Please post the last 10 entries of the DB timestamps vis-a-vis the timestamps from the script.

For example, you informed us that there is a duplicate DB entry 45 seconds from the first entry; but you did not yet inform us when the crontab runs, nor did you provide the crontab entry and nor did you provide the crontab script.

You seem to be asking for us to make up a solution or possible cause to your problem but you are not willing to provide any details.

Why not just provide the details so we can help you?

Hi Neo,

I did cross checked it the log has that mechanism covered in it,logs are getting generated as per timestamp when its run.. and a file is created with the date time stamp for the cron scheduled.

eg logifile25062019.txt (DDMMYY)

But still on the DB side we are observing duplicate entries for that job scheduled