Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up cron to run a file just once at a specific time? [closed]

Tags:

linux

unix

cron

How do I set up cron to run a file just once at a specific time? One of the alternatives is at but it is not accessible to all users on standard hosting plans. Therefore I was wondering whether there is way to do it using cron?

like image 613
Gajus Avatar asked Mar 29 '11 14:03

Gajus


People also ask

How do I set up cron to run a file just once at a specific time?

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM. Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do I set a cron schedule?

Cron jobs are scheduled at recurring intervals, specified using a format based on unix-cron. You can define a schedule so that your job runs multiple times a day, or runs on specific days and months.


2 Answers

You really want to use at. It is exactly made for this purpose.

echo /usr/bin/the_command options | at now + 1 day 

However if you don't have at, or your hosting company doesn't provide access to it, you can have a cron job include code that makes sure it only runs once.

Set up a cron entry with a very specific time:

0 0 2 12 * /home/adm/bin/the_command options 

Next /home/adm/bin/the_command needs to either make sure it only runs once.

#! /bin/bash  COMMAND=/home/adm/bin/the_command DONEYET="${COMMAND}.alreadyrun"  export PATH=/usr/bin:$PATH  if [[ -f $DONEYET ]]; then   exit 1 fi touch "$DONEYET"  # Put the command you want to run exactly once here: echo 'You will only get this once!' | mail -s 'Greetings!' [email protected] 
like image 52
TomOnTime Avatar answered Nov 13 '22 06:11

TomOnTime


Try this out to execute a command on 30th March 2011 at midnight:

0 0 30 3 ? 2011  /command 

WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.

like image 31
gparis Avatar answered Nov 13 '22 05:11

gparis