0

I need to setup 2 cron jobs with very precise timing for my PHP code.

Cron 1:

Run cron job every 10 minutes, every day between 9:24:59 to 15:14:59. i.e cron job starting from every morning 9 hours 24 minutes 59 seconds to afternoon 15 hours 14 minutes 59 seconds. The cron should run every 10 minutes, example:

9:24:59
9:34:59
9:44:59
9:54:59
...
14:54:59
15:04:59
15:14:59

Cron 2:

Run cron job every 10 minutes, every day between 9:15 to 15:05. For example:

9:15
9:25
9:35
9:45
...
14:45
14:55
15:05
  • https://superuser.com/questions/411406/set-a-cron-every-certain-hours-between-certain-hours – catcon Jul 22 '19 at 22:02
  • 2
    Welcome to StackOverflow! You can configure a cronjob close to your needs with the right syntax, but if you need it on the second scale, you're out of luck with cron. You'll probably need to just have a background script running all the time for that (or let the script sleep first when you call it early) or some cron tool. A tool you'll probably gonna love for configuring cron times: [https://crontab.guru/](https://crontab.guru/#*/10_9-15_*_*_*) – ArendE Jul 22 '19 at 22:14
  • What's with the bizarre timing requirements? – Sammitch Jul 23 '19 at 00:52
  • @Sammitch I am trying to construct 10 minutes candlesticks for NSE stocks. Usually candlesticks are constructed with (OHLC) Open, High, Low, Close prices of the stocks, but as per my requirement I don't have to construct candlesticks with upper shadow & lower shadow, so I have planned to neglect High & low price of the stocks at a specific time period. I have just planned to construct candlesticks with Open & Close prices of the stocks. Instead of listening and recording every tick by tick data of stocks through websocket,... – Lokeswaran Selvaraj Jul 23 '19 at 22:03
  • @Sammitch I will just record last traded price of stocks at a specific time to construct candlesticks. For example, I will record last traded price of the stocks at 9:15 as Open price of the first 10 minutes candlesticks and last traded price at 9:24:59 as Close price of the first 10 minutes candlesticks. Similarly for every 10 minutes. First candlestick: open price - 9:15 ; close price - 9:24:59 ; Second candlestick: open price - 9:25 ; close price - 9:34:59 ; – Lokeswaran Selvaraj Jul 23 '19 at 22:03
  • Ok well trying to implement this with cron is going to be a _real bad time_, even if you could just run every 10 minutes on the tens. You're going to want to write a daemon for this as a start, and how you scale out from there is a whole other conversation. – Sammitch Jul 23 '19 at 23:29

2 Answers2

1

You can specify the time range for the script in the crontab file using bash syntax. For example see: Cron jobs and random times, within given hours and How to check if the current time is between 23:00 and 06:30.

Using bash commands in a single line can be difficult to understand and maintain. A simpler option is to use 3 cron tab entries. The first entry will run the script every 10 min from 09:25 to to 09:55. The second entry will run the script every 10 min from 10:05 to 14:55. The third entry will run the script from 15:05 to 15:15. These three crontab entries will cause the script to run at 09:25, 09:35, 09:45 .... 15:15. Following are the three crontab entries:

25-55/10 9 * * * script-path
5-55/10 10-14 * * * script-path
5-15/10 15 * * * script-path

You should confirm that the script runs at the correct times

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24
  • I don't have shell script privilege with the server since I am using shared hosting server, I cannot initiate the cron job exactly at 9:24:59 from my php code. I have to create the cron job through the cpanel's cron job editor and run the desired php file. – Lokeswaran Selvaraj Jul 23 '19 at 22:14
  • Thanks for the solution. Syntax for my cron tab editor is a bit different but figured that out and every thing works fine. I really appreciate your help. As per my Cron editor, Cron 1 syntax: 15,25,35,45,55 9 * * * script-path and 5,15,25,35,45,55 10,11,12,13,14 * * * script-path and 5 15 * * * script-path. For Cron 2 syntax: 24,34,44,54 9 * * * sleep 59; script-path and 4,14,24,34,44,54 10,11,12,13,14 * * * sleep 59; script-path and 4,14 15 * * * sleep 59; script-path. – Lokeswaran Selvaraj Jul 24 '19 at 20:58
0

To make it run any process every 10 minutes just add:

*/10 * * * *

I would make my limitations of time from my PHP function.

Crontabs explanation

Source

  • You can also set the hour range using `-`: `*/10 9-15 * * *` – catcon Jul 22 '19 at 23:00
  • @user3116428 I don't have shell script privilege with the server since I am using shared hosting server, I cannot initiate the cron job exactly at 9:24:59 from my php code. I have to create the cron job through the cpanel's cron job editor and run the desired php file. – Lokeswaran Selvaraj Jul 23 '19 at 22:11