Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using crontab to execute script every minute and another every 24 hours [closed]

Tags:

crontab

I need a crontab syntax which should execute a specific PHP script /var/www/html/a.php every minute. The execution on every minute must start at 00:00. The other task which must execute a script at 00:00 /var/www/html/reset.php (once every 24 hours).

like image 714
Michael Avatar asked Mar 22 '11 21:03

Michael


People also ask

How do I schedule a script in crontab to run every 5 minutes?

basic 3. /usr/bin/vim. tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.

How do I schedule a cron job every minute?

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. An asterisk in the every field means run given command/script every minute.

How do I schedule a cron job every 2 hours?

The answer is from https://crontab.guru/every-2-hours.

What does 0 * * * * mean in crontab?

0 * * * * -this means the cron will run always when the minutes are 0 (so hourly)


2 Answers

every minute:

* * * * * /path/to/php /var/www/html/a.php

every 24hours (every midnight):

0 0 * * * /path/to/php /var/www/html/reset.php

See this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

like image 50
Jan Hančič Avatar answered Sep 30 '22 19:09

Jan Hančič


This is the format of /etc/crontab:

# .---------------- minute (0 - 59) # |  .------------- hour (0 - 23) # |  |  .---------- day of month (1 - 31) # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # |  |  |  |  | # *  *  *  *  * user-name  command to be executed 

I recommend copy & pasting that into the top of your crontab file so that you always have the reference handy. RedHat systems are setup that way by default.

To run something every minute:

* * * * * username /var/www/html/a.php 

To run something at midnight of every day:

0 0 * * * username /var/www/html/reset.php 

You can either include /usr/bin/php in the command to run, or you can make the php scripts directly executable:

chmod +x file.php 

Start your php file with a shebang so that your shell knows which interpreter to use:

#!/usr/bin/php <?php // your code here 
like image 28
Greg Avatar answered Sep 30 '22 17:09

Greg