为数据库日期时间设置cron作业[关闭]

What is the best way to set a cron job for a datetime that is saved in a database? I have never used cron jobs before so I am unfamiliar with them. I have done some research on it and the only way I see this happening is if I set the cron job at a 30 minute interval then make sure the event times fall on the hour or half hour mark. But even then the job would be running the code even if there is no event.

Depending on the use case and requirements of your application, you can use a shorter interval as well. Unless of course you want to batch-process the jobs on the hour / half hour mark.

I wouldn't worry about the overhead of the code itself triggered by the cron. As long as it just peeks at the database to see if there's an event to run, it should finish quickly if there's no new event.

A few tips if you've never used cron jobs before. Make sure that:

  • The script you're calling is reporting errors (try it out first by invoking it via the shell).
  • You're not discarding the output of the script by e.g. redirecting to dev/null.
  • You've set yourself as the recipient for cron notification emails. So if it fails for any reason, you'll have something to hold onto.