2

I understand I can set a cron job to run every 5 minutes with crontab -e by adding a line such as: */5 * * * * /path/to/script.sh.

Is it possible to get the system time in minutes using date +"%M" for example, and then set a cron job to run at date +"%M" plus 5 minutes?

I know I can get date +"%M" + 5 via the following process:

$ MIN=`date +"%M"`
$ export MIN
$ expr $MIN + 5

Is it possible to use this to set a cron job or script to run at "current time in minutes" plus "X minutes"?

I could imagine this being useful in an application in which a user creates a new document and then is prompted to save or title the document X minutes after creating it.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
bc1984adam
  • 173
  • 1
  • 9
  • 1
    What do you mean, cron runs at an exact time. Setting it up to run at current time plus x minutes doesn't even make sense because it would already have to be running to do that... – 123 Jan 12 '16 at 14:42
  • 1
    In your example you'll have to kill the cronjob after you don't need it anymore. Cron isn't made for this. See "at" below. – rundekugel Jan 12 '16 at 15:46

3 Answers3

7

You should use the at command instead.

With at, you can specify the time when a command should be run using time or even keywords like midnight, teatime, tomorrow etc..

You can specify the time after 5 min like this:

at now + 5 min

And then enter the command you want to schedule. Or you can enter your scheduled jobs in a jobs file and give it as a argument for the at command using the -f option.

Sample of a jobs file:

$ cat myjobs.txt
/path/to/a/shell-script.sh
/path/to/any/command/or/script.sh

The following command will execute those jobs after 5 mins:

$ at -f myjobs.txt now + 5 min

Check this link for more information.

Mehdi Yedes
  • 2,267
  • 2
  • 14
  • 14
0

Look at the at command. Maybe this is what you are looking for.

See

NaN
  • 7,441
  • 6
  • 32
  • 51
-2

Yes you can:

*/5 * * * * /path/to/script.sh && crontab -r
socket
  • 1