Jump to content

Wikipedia:Reference desk/Archives/Computing/2016 January 17

From Wikipedia, the free encyclopedia
Computing desk
< January 16 << Dec | January | Feb >> January 18 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


January 17

[edit]

Difference between ? and * in cron

[edit]

At work, I have to use a .NET scheduler library that uses Unix cron expressions for scheduling. The documentation tells me that "?" means "any day/month" and "*" means "every day/month". What is the difference between these? Could anyone give a concrete example where "?" and "*" cause different schedules? Or do such conditions even exist? JIP | Talk 20:46, 17 January 2016 (UTC)[reply]

I looked in the Solaris, FreeBSD, and Linux cron(8) and crontab(5) man pages, and I don't see any of them ascribing any meaning to a ? wildcard. This seems to be specific to the fakey crontab your specific .NET thing does. -- Finlay McWalterTalk 22:31, 17 January 2016 (UTC)[reply]
Indeed; the same applies to GNU mcron; the '?' symbol is not a documented feature of either type of cron syntax supported by the GNU variant. Your best bet is to check the documentation that specifically applies to your scheduler tool or library, because it evidently uses its own variation of the standard syntax. Nimur (talk) 22:57, 17 January 2016 (UTC)[reply]
I don't understand it either. Usually a "?" wildcard in a string means any ONE character whereas "*" means any string of characters. Bubba73 You talkin' to me? 23:30, 17 January 2016 (UTC)[reply]
Well, "usually on Windows"... but even then, use caution: '?' and '*' wildcard behavior varies wildly between the ordinary Windows command interpreter, the Windows Power Shell, Windows Power Shell "cmdlets", and third-party programs that run on Windows. Here's an MSDN blog on the difference in wildcard expansion between Windows and unix...; and even that doesn't mention the special case of cron syntax.
For what it's worth, our article on cron mentions that '?' expands differently on nncron, a proprietary freeware variant of cron developed for Windows. It even self-describes its '?' expansion as "non-standard." Nimur (talk) 02:32, 18 January 2016 (UTC) [reply]
cron syntax isn't a special case of shell filename globbing. It's a totally different thing. I guess it's possible that shell syntax influenced cron's use of * for "any value", but *2 doesn't mean any value ending in 2, etc. I don't know why Bubba thought there was any connection. -- BenRG (talk) 04:10, 18 January 2016 (UTC)[reply]
I thought so because of Wildcard character, but I admit that I don't know anything about cron. Bubba73 You talkin' to me? 04:47, 18 January 2016 (UTC)[reply]
On NetBSD UNIX, the crontab format is explained in crontab(5), i.e. "man 5 crontab", and this version does have a meaning for "?". It means that when reading the crontab file, cron is to select any one value randomly from the permitted ones. So for example "20 ? * * *" specifies that the job is to run once a day at 20 minutes past some hour: for example, it might be at 3:20 or 7:20 or 23:20. "? ? * * *" would run it once a day at a random time. Also, "?" can be followed (without spaces) by a range, which asks for a random selection from that range. "?40-42 * * * *" would run the job once an hour at either 40, 41, or 42 minutes past the hour.
Note that if cron ever rereads the crontab file, a new random selection will be made. So if you edit the crontab file, even if you did not change the entry where "?" was used, then this might cause your "daily" job to switch to a different time, perhaps causing it to execute a second time during the same day/hour/etc. (if it switched to a later time), or not at all one day (if it switched to an earlier time). Similarly if cron had to be restarted for some reason.
The man page points out that this feature is useful if the same crontab entry is used on a large number of machines and would cause each of them to connect to the same host, for example. --76.69.45.64 (talk) 05:26, 18 January 2016 (UTC)[reply]