Hostpoint - Hilfe & Support > Help & Support > Products > Webhosting > Setting up cronjobs

Setting up cronjobs

Cronjobs are used to automate recurring tasks. You can control the times and/or days on which certain programs or scripts are executed. To create a new cronjob, log into your Control Panel.

The Cronjob Manager is found under Admin.

Notifications

If your cronjob generates text output when executed, this text will be sent to you by e-mail. You can specify a recipient address for the messages in the E-Mail address field. If you do not want these notifications, add the string ‘>/dev/null 2>&1’ at the end of your command to disable all output.

/usr/local/bin/php -f /home/username/bin/beispiel.php >/dev/null 2>&1

or

/home/username/bin/beispiel >/dev/null 2>&1

Cronjob command

The file you want to execute as a cronjob must either be executable or able to be executed by an interpreter.

Executable files

Binary programs or scripts with a shebang line can be executed directly. Here you should make sure that the file mode allows this as well: The x-bit must be set. If you are unsure, set the mode to 755 or 750.

Shebang line

This special line in the script specifies the interpreter that can read and execute the contents of this script. It is always the topmost line in the first column and it begins with the symbols ‘#!’, followed by the path to the interpreter.

A shebang line for a PHP script on our FreeBSD servers looks like this:

#!/usr/local/bin/php

Additional shebang lines:

Perl:

#!/usr/local/bin/perl

Python:

#!/usr/local/bin/python

Note: Make sure that you do not use any DOS line breaks!

Starting scripts with an interpreter

To execute a script without a shebang line, you must invoke it with the appropriate interpreter. Note that most additional programs running on FreeBSD Unix are found under /usr/local/bin/. For information on this topic, refer to the article on system paths.

The cron command for a PHP script located in /home/username/bin/example.php looks like this:

/usr/local/bin/php -f /home/username/bin/beispiel.php

Other command lines:

Perl:

/usr/local/bin/perl /home/username/bin/beispiel.pl

Python:

/usr/local/bin/python /home/username/bin/beispiel.py

Time of execution

Um zu bestimmen, wann das Kommando des Cronjobs ausgeführt werden soll, müssen Sie fünf Zeitwerte angeben. Der Cron-Daemon prüft minütlich, ob einer Ihrer Cronjobs ausgeführt werden soll. Immer wenn alle fünf Werte zutreffen, wird das Kommando ausgeführt.

 
 
Minute
Enter the minute in which you want your cronjob to be executed (0-59).
Hour
Enter the hour in which you want your cronjob to be executed (0-23).
Day
Enter the day of the month on which you want your cronjob to be executed (1-31).
Month
Enter the month of the year on which you want your cronjob to be executed (1-12).
Weekday
Enter the day of the week on which you want your cronjob to be executed (0-6, Sunday is 0 and 6 is Saturday). 
List

You can also specify multiple comma-separated values. To start a job at xx:20 and xx:50, enter ‘20,50’ for ‘Minute’

Wildcard

You can use a wildcard (*) for fields that you want to ignore. If, for example, you want to execute a job on every day of the year, you do not have to enter every possibility for ‘Day’, ‘Month’ and ‘Weekday’. It is sufficient to complete these fields with an ‘*’, which means “always true”.

Interval

If you want to execute a script at a specific interval, indicate this with ‘*/interval’. The field is considered true if the current value is completely divisible by the interval. If you want a job to be executed every two hours, enter */2 for Hour. This will be true for 0, 2, 4, 6 o’clock, etc.

Hostpoint recommendation

If possible, we recommend starting your cronjob at night between the hours of 1:00 am and 5:00 am, because the server load is less at these times than during the day or at midnight.

Examples

Executing at a specific time

To execute a script for a weekly summary every Saturday night at 23:15:

  

Minute: 15
Hour: 23
Day: *
Weekday: 6
Month: *

Command:

/home/username/bin/zusammenfassung.pl

Execute every 5 minutes

You allow your website users to upload a picture of themselves to the website via e-mail. For this purpose, you want to check this mailbox every five minutes for new pictures to add to the site.

Minute: */5
Hour: *
Day: *
Weekday: *
Month: *

Command:

/usr/local/bin/php -f /home/username/bin/checkmail.php
Repeated execution at night

To create a short movie, you want to download a nighttime picture of your city from a webcam at regular intervals. To do so, you have written a “one-liner” script that you want to execute every 20 minutes between the hours of 20:00 and 6:00 in the morning.

Minute: */20
Hour: 20-23,0-6
Day: *
Weekday: *
Month: *

Command:

/usr/local/bin/wget 'http://www.example.com/webcam.cgi?tilt=-20&yaw=93' -O /home/username/cam-images/`date +\%Y-\%m-\%d`.jpg

Additional resources

Additional information about working with cronjobs can be found here: http://www.howtoforge.de/anleitung/e...-in-cron-jobs/ or by referring to your preferred Unix guide.

You must to post a comment.
Last modified
02:25, 6 May 2015

Tags

Classifications

This page has no classifications.