user
How to schedule a cron job at midnight and midday (twice a day)?
alphonsio

To schedule a cron job twice a day, use the following syntax (it will run the task at midnight and midday):

  0  0,12 *  *  * 	user-name 	/path/to/command

To schedule a cron job to run at midnight and midday (twice a day), you need to add an entry to your crontab file. The crontab syntax for scheduling a cron job at specific times is as follows:

* * * * * 
- - - - -
| | | | |
| | | | +----- day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- minute (0 - 59)

For a cron job that runs at midnight (00:00) and midday (12:00), you can use the following crontab entry:

0 0,12 * * * 

Here's what each part means:

  • 0: The minute field, indicating the 0th minute of the hour (i.e., on the hour).
  • 0,12: The hour field, indicating 00:00 (midnight) and 12:00 (noon).
  • *: The day of the month field, meaning every day of the month.
  • *: The month field, meaning every month.
  • *: The day of the week field, meaning every day of the week.
  0  0,12 *  *  * 	user-name 	/path/to/command
# |   |   |  |  |
# |   |   |  |  .---- Every day of the week
# |   |   |  .------- Every month
# |   |   .---------- Every day
# |   .-------------- When hours equal zero or twelve (, = list separator)
# .------------------ When minutes equal 0

Example

If you want to run a script located at /path/to/your/script.sh at midnight and midday, you would add the following line to your crontab file:

0 0,12 * * * /path/to/your/script.sh

How to Edit the Crontab File

  1. Open the crontab file for editing by running the following command in your terminal:

    crontab -e
  2. Add the cron job line to the file.

  3. Save and close the file. Your cron job is now scheduled to run at midnight and midday every day.

Verification

To verify that your cron job has been scheduled, you can list all the cron jobs by running:

crontab -l

This will display the list of all cron jobs for the current user. You should see your newly added job in the list.