To access "Cron jobs" menu, sign into DirectAdmin and then click on the "Advanced Features" icon, followed by the "Cron Jobs" link.
Cron jobs are system commands that can be scheduled to run automatically at a certain time.
For example, if you wanted a "hi" value added to a text file every Monday morning, you could do this through the cron jobs menu.
Values that can be set are minute, hour, day, month, and day of the week (Monday-Sunday; the numbers 0 or 7 represent Sunday). Any value can be ignored by placing an asterisk (*) in the text field.
In the above example, we are setting command echo to put word "hi" in hi_per_hour.txt file every day at midnight.
You can also specify exact times using commas to separate them (e.g. 1,2,3 is minutes 1, 2 and 3).
You can specify spans using a dash (e.g. 5-7 is minutes 5 to 7).
You can specify intervals using a star and a forward slash. (e.g.: */2 is every 2nd minute.)
You can combine them to create a more precise schedule. (e.g.: 1,5,11-15,30-59/2 (minutes 1, 5, 11 to 15 and every 2nd minute between 30 and 59).
Remember to click the "Create" button below when you are finished entering your data.
If you're trying to run a php script, remember to run the php binary, and pass it the php script. For example:
/usr/local/bin/php /home/user/domains/domain.com/public_html/file.php
Debug Tricks for Cron Jobs
Debugging is never an easy task, and it may be even harder when you need to debug a script run by cron.
When you need to test and debug a cron, you can:
- set the timing of your cron task to * * * * *
Doing so, it will run every minute, and you'll not need to wait a long time to see the results. - crontab has it's own environment, which might differ from the environment you set.
So, i.e., if you set special PHP configuration using .htaccess, it may be ineffective for your cron script.
So, check environment configuration inside your own script (i.e., inserting a phpinfo() command). - pipe all the output of your script to a file, that you'll be able to later access in File Manager or via FTP.
You can do it using following code to run your cron job:* * * * * /path/to/script.sh >> ~/crontask.out 2>&1
and later checking the content of file crontask.out