1

I have a command in "/etc/crontab" which runs a database program every 7 days. Now, I see that this command has been commented by another user for 2 weeks. I immediately uncommented the line, and today I see that this command has not been executed by crontab.

Why? I think after restarting /etc/init.d/cron restart, crontab would restart and see the command line, which says to execute a command every 7 days. Then when is it going to start that program! isn't it immediately at the start of service crond restarting? it seems not.

I cannot wait 7 days for the program now, I want crontab to start the this 7 days from tonight. how can I do that?

the script is:

0 0 */7 * *        root  source /opt/db_maintain/run.sh
  • Do you want the crontab to execute the script right now? Or today at midnight? – tachomi May 27 '15 at 13:29
  • You know, an "edit war" as root user on a system is a very very bad idea. Can your script/database run as a non-root user? If so, you can have your own crontab file! – Otheus May 27 '15 at 14:56
1

*/7 doesn't mean "every 7 days from now". It means "every days that are a multiple of 7". So it runs the command if #day modulo 7 == 0 → 7th, 14th, 21st, 28th of the month.

The french wiki page of Cron provides many examples: http://fr.wikipedia.org/wiki/Cron#Exemples

You may want to use Fcron: http://fcron.free.fr/

1

Today is Wednesday. If you want cron to execute a command at midnight every Thursday, here's what you put in your crontab:

0 0 * * 4    root /path/to/command
0

You can also install the at service/daemon which spools jobs based on time features like "now + 7 days". First, launch atd (through the init scripts or systemd or upstart etc). Then queue up your job with

at -f /path/to/command now + 7 days

to start it 7 days from right now, or

at 2am tomorrow

and read the commands to be run tomorrow at 2 in the morning. You'll want to do a test or two before waiting 7 days to find it didn't work. Try now + 1 minutes

One way to have it run every 7 days is to prepend the batch with another at command:

cat >~/bin/weekly-job.sh << AT
at -f ~/bin/weekly-job.sh now + 7 days
~/bin/database-job.sh
AT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.