56

I need unix cron command to run every 12 hours.

I have 500+ sub blogs in my server.

This is the file i want to run every 12 hours

http://*.mysite.com/somedir/index.php

Where * is my subdomain of my blogs.

I need cron command for all blogs. Is it possible to run all of them with single command? OR do i have to create command for each blog?

Giri
  • 4,849
  • 11
  • 39
  • 48
  • What don't you understand: the format of `crontab` or how to access/run a PHP script? –  Feb 22 '12 at 10:59
  • Do you have list of sites somewhere? (to what should the '*' expand?) – wildplasser Feb 22 '12 at 11:00
  • do you want to run the script locally, or call it from a different machine? If you have it locally you could easily create one command. Calling a curl or wget on each page one by one will prove a bit harder. – Jens Timmerman Feb 22 '12 at 11:01
  • @Tichodroma Yes i want the format of crontab for every 12 hours. I don't want to run all 500+ blog script at a time. For example give me the code to run 1AM and 1PM. Thanks – Giri Feb 22 '12 at 11:04
  • @JensTimmerman I'm not sure what you mean local. I have a hosting account in namecheap. That is where my files are stored – Giri Feb 22 '12 at 11:05
  • Be specific over the requrement, what is that you are trying to run in the cronjob regarding those blogs? – Anil Feb 22 '12 at 11:07
  • @user1091558 so you don't have access to the machine, you can only upload documents, and you want to run a cron on your own unix machine (not hosting the sites) – Jens Timmerman Feb 22 '12 at 11:08

9 Answers9

151

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.

You could use 0 1,13 * * * which means for every 1AM and 1PM.

0 1,13 * * * rm /var/www/*/somedir/index.php > /home/someuser/cronlogs/some.log 2>&1

where * can be replaced by different domain names.

XzAeRo
  • 566
  • 5
  • 11
Anil
  • 3,912
  • 5
  • 35
  • 46
  • 3
    This is the best answer since it shows you how to do this with arbitrary hours, not just 12PM / 12AM – curveorzos Jul 26 '16 at 19:35
  • explain me rm in this string? :) – Matas Lesinskas May 26 '18 at 03:22
  • Note that you can also define a minute, e.g. "47 1,13 * * *" which means "every 1:47am and 1:47pm". This can be helpful to reduce loads on your server; instead of running all crons at the 0th minute of each hour, space them out – KayakinKoder Jan 18 '19 at 17:36
34

I think the right way is -> 1 */12 * * * (actually, any number in the minute position will do the trick.)

If you set -> * */12 * * * it will be executed every minute at 12h and again at 24h.

Giovani Dardani
  • 476
  • 4
  • 4
11

Assuming your sites live in /var/www/sitename and you have the php shell installed in /usr/bin/php you can easily create a cron job that runs all those files.

run

crontab -e

and add this line

42 */12 * * * /usr/bin/php /var/www/*/somedir/index.php  >> ~/cronjob.log 2>&1

The * here in /var/www/*/somedir is just a wildcart. This means it will catch every directory in your /var/ww folder.

f.ex:

[jens@localhost ~]$ ls -l temp
total 28
-rw-rw-r--. 1 jens jens 1641 Feb 21 16:12 somefile.py
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test2
drwxrwxr-x. 2 jens jens 4096 Feb 22 15:10 test3
drwxr-xr-x. 8 jens jens 4096 Jan 27 10:21 emptydir
-rw-rw-r--. 1 jens jens  548 Jan 27 16:15 Unsaved Document 1

[jens@localhost ~]$ ls temp/*/testfile.php 
temp/test2/testfile.php  temp/test3/testfile.php  temp/test/testfile.php

As you can see, this returns the testfile.php in each subfolder of temp, namely folder test, test2 and test3. Emptydir is also a folder, but since it has no testfile.php in it, nothing willhappen with it.

If your directory structure is arbitrarily deep you can use **

e.g. 42 */12 * * * /usr/bin/php /var/www/**/index.php >> ~/cronjob.log 2>&1

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48
  • Hi i'm not sure what `*` mean in this line `/var/www/*/somedir/index.php` You want me to use the command as it is or i have to replace it with my subdomains? – Giri Feb 22 '12 at 11:11
  • @user1091558: that is what I asked you under the OT. Do you have a list of sites (or a list of scripts)? – wildplasser Feb 22 '12 at 11:17
  • Oh i'm sorry. I'm using wordpress multisite. I have 500+ blogs under one installation. So i have only list of sites – Giri Feb 22 '12 at 11:40
  • 1
    Quick question about your example */12. Would `4/12` in the hour field match both 4am and 4pm? – Marcos Aug 13 '12 at 17:34
  • @marcos It's better to just use '4,16' if this is the behaviour you want. – Jens Timmerman Aug 20 '12 at 15:54
  • 1
    wouldn't you need to change the first * to a number (such as 0). i think this runs every minute for every hour that it is allowed to run (which is two hours a day of course). so that's 120 times run a day? – celwell Jul 22 '13 at 22:56
  • @celwell no, it this context * stands for a range from beginning to end. – Emirikol Oct 17 '13 at 13:30
  • 2
    @Emirikol you're right that * is just a shorthand for 0-59, which is, in turn, a shorthand for a long list of 0,1,2,...,59. Which still means that celwell is right. – WGH Feb 11 '14 at 12:17
  • my bad, I was looking at the second * in the expression instead of the first one. – Emirikol Feb 12 '14 at 13:19
7

Use "*/12" to mean "every 12 hours."

Geremia
  • 4,745
  • 37
  • 43
6

You need some kind of master-script (called by cron), which expands the list of sites, and calls "/usr/bin/php /var/www/*/somedir/index.php", whith the '*' replaced by a list entry. This can be done in a shellscript, a perl or python script, or maybe even a php script. For sh this could be: (untested)

#!/bin/sh
cd /home/subdir/for/cron

LIST="a b c d e f g h i j k l m o p q r s t u v w x y z"

for x in $LIST; do
   /usr/bin/php /var/www/${x}/somedir/index.php 2>$1 > /tmp/${x}.log
done

If it is inconvenient to have the list hardcoded like this, there are other methods: backticks, or read < file_with_all_the_names_in_it

wildplasser
  • 43,142
  • 8
  • 66
  • 109
4

0 */12 * * * means "At minute 0 past every 12th hour."

Check out https://crontab.guru for a nice calculator.

Alex K
  • 14,893
  • 4
  • 31
  • 32
1

Write command in console

crontab -e

edit with editor (I like nano)

add line

0 1,13 * * * php /home/catalog/public_html/crons/index.php

close with

press ctrl + x

press y then press enter done :)

Check if saved with

crontab -l

command

if you want to test if it will work test just running it manualy with

php /home/catalog/public_html/crons/index.php

command

Matas Lesinskas
  • 414
  • 6
  • 13
-1

Use this it will Run after each 12 hour * */12 * * * php /var/www/"Your domain"/cronfile.php

Hasibur
  • 205
  • 1
  • 10
  • 1
    This should be 0 */12 * * * /path/to/php /path/to/somedir/index.php. If you were to use an asterisk in the first position, it would run every minute of every 12th hour. – davelupt Apr 21 '15 at 11:59
-1

->cron('0 */12 * * *');

This cron will run the scheduler at every 12 hours.