How to create a Cron job?

Asked

Viewed 1,583 times

3

I have a real estate website that every day we do an XML import of the properties added the day before. This import is done by a component where I choose the file and give import and it runs the function.

I edited the component to instead of choosing the file, already take it straight from a file that is always available with the same name (eg www.site.com.br/imoveis.xml), updating the XML data automatically every day.

I have no experience with cron Jobs, how do I create a schedule for every day he does this import? Or at least the way I do it.

  • What is the server operating system? You have shell/SSH access to it?

  • is linux, the access I have is Cpanel that has cron jobs. what I was able to do is access a specific url, which if logged in, it works right, but I couldn’t get it logged in by itself

1 answer

6


The simplest way to do that is:

  1. Define what cron will do, what it will handle, whether in the database or in server files.

  2. Create/test scripts with routines and functions that do the manipulations that were initially defined. Example: Create routines that read the available XML and update the values in the database.

  3. CRON: To create a scheduled task, do the following:

Click on the "Cron Jobs" icon inside cPanel, in Advanced. Verify that the current email address is valid. Otherwise, enter a new email and click on "Update Email".

Select the configuration you want.

Alternatively, you can adjust the time settings individually. Minute, hour, day, month and day of the week can be modified to achieve your goal.

In the entry box to the right of "Command, type the name" of the file type, then add a space and provide the path to the file you want the command to be executed. (Link to your script)

Click on the "Add New Cron Job" button. You just create a cron job to run a file on a given date and time with the desired repeat.

Scheduled tasks are easy to edit and delete. Click on the "Cron Jobs" button inside cPanel. Scroll down to the last section called "Cron Current Jobs". Find the correct cron job and click "Edit" or "Delete" within the actions. Editing does not have good automated tools, so it can be easier to copy command, cron work delete and recreate.

Creation in command lines. Examples only! sections in bold should be changed.

PHP

Command to run a cron PHP 5.5 job:

/opt/php55/bin/php/home/USUARIO/public_html/arquivo.php

Command to run a cron PHP 5.4 job:

/opt/php54/bin/php/home/USUARIO/public_html/arquivo.php

Command to run a cron PHP 5.3 job:

/opt/php53/bin/php/home/USUARIO/public_html/arquivo.php

Optional flag sometimes required for a cron PHP job:

php -q /home/USUARIO/public_html/arquivo. php

Command to use a specific php.ini file:

php -c /home/USUARIO/public_html/php.ini /home/USUARIO/public_html/arquivo.php

Command to get a remote file:

/usr/bin/GET http://www.seudomini....br/arquivo.php

Command to run a cron CGI job:

/home/perl/USUARIO/public_html/cgi-bin/arquivo.pl

SSH Extras

Command to run a cron shell script job:

/bin/sh/home/USUARIO/public_html/arquivo.sh

Command to import a database:

mysql -u mysql_user -p senha database_name < backup.sql

Command to export a database:

mysqldump -u mysql_user -p senha database_name > backup.sql

Sources:

http://forum.hostgator.com.br/topic/241-criar-e-excluir-um-trabalho-cron/ http://www.unixgeeks.org/security/newbie/unix/cron-1.html

  • Cool, already gave me a light! Now what I need is to build php to do the job. I need cron to access the link that does the task (domain.com/Administrator/import.xml). only that this link can only be accessed by admin, it is possible in the cron itself to pass login and password for it access?

  • Maybe (possibly) not even need to force a login by code, you just need to access the files and directories and work with bank as I understand, for security could create a hash or verification token so that the only one to access the script is the cron itself.

  • Still have some doubt, we could help in something else, otherwise could accept the answer by clicking on the "V" just below the votes?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.