0

I am running a single instance cron job for certain task. My script looks like below:

# every hour interval
0 * * * * php /project/yii daily/order

# every 30 minutes interval
*/30 * * * * php /project/yii daily/sample

I am using https://crontab.guru/ to get the values. The scheduling works but for order, I set it to run every hour interval but instead it runs every five hours instead. I checked the path and script and it is correct.

I was wondering why is does not run hourly as set?

5
  • Test the scheduling separately with 0 * * * * date >> /tmp/test_x.txt. Also "every 5 hours": does it start over at 0:00 every midnight, or does it run at 01:00 the second day (i.e. strict 5-hour intervals), which would indicate some timer other than cron. Jan 19 at 9:19
  • For hourly action the format should be 0 */1 * * *
    – user492570
    Jan 19 at 14:11
  • @Paul_Pedant It runs every five hours without starting over.
    – nativemash
    Jan 20 at 5:59
  • @elmclose A step of /1 is the default anyway. A field may be an asterisk, which always stands for first-last which for hour is 0-23. If this is needed for hour, then you also need */1 for day of month. Jan 20 at 8:58
  • @nativemash So it runs at 00:00, 05:00, 10:00 .. on the first day, and 01:00, 06:00, 11:00 .. on the second day ? Cron cannot do that. Your php code contains its own timing constraints, or waits for some external event, and skips the missing cron events itself. Maybe it runs for around 4.5 hours, and refuses to allow two copies of itself to run simultaneously. Test by making the command part: date >> /tmp/test_x.txt && php /project/yii daily/order. Then you can see cron log each hourly execution, and then check to see php running (or not) in top. Jan 20 at 9:07

1 Answer 1

-1

Jobs run through cron aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings from ~/.bashrc are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

0

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.