Print a cron formatted time for 2 minutes in the future (for crontab testing)

crontest () { date +'%M %k %d %m *' |awk 'BEGIN {ORS="\t"} {print $1+2,$2,$3,$4,$5,$6}'; echo $1;}
usage = crontest "/path/to/bin" This version of this function will echo back the entire command so it can be copied/pasted to crontab. Should be able to be automagically appended to crontab with a bit more work. Tested on bash and zsh on linux,freebsd,aix
Sample Output
49 15 12 03 *   /path/to/bin

By: CoolHand
2015-03-12 19:56:56

1 Alternatives + Submit Alt

  • Another function to stick into your .bashrc This spits out the time two minutes in the future, but already formatted for pasting into your crontab file for testing without any thought required on your part. Frequently things don't work the way you expect inside a crontab job, and you probably want to find out now that your $PATH is completely different inside of cron or other global variables aren't defined. So this will generate a date you can use for testing now, and then later you can change it to run at 5:37 am on a Sunday evening. Show Sample Output

    crontest () { date '-d +2 minutes' +'%M %k %d %m *'; }
    unixmonkey365 · 2011-09-16 00:47:24 1

What Others Think

It's a nice idea, but using awk is a bit heavy. Why not put $1, or $* (for all command arguments) into the date command: crontest () { date -d +'2 minutes' "+%M %k %d %m * $*"; } crontest my-command abc def 30 14 09 04 * my-command abc def
flatcap · 302 weeks and 5 days ago
"date -d" does not work on AIX and possibly other Unix (unless using GNU date). That is why I went for the more difficult (and costly) solution with awk..
CoolHand · 302 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: