Create own cronjob on LoxBerry

If you need to create your own cronjob on LoxBerry (to schedule your own scripts).

Using LoxBerry's own schedules

LoxBerry delivers with a hand-full of directories with fixed schedules:

  • /opt/loxberry/system/cron

The directory has folders e.g. cron.05min, cron.30min, cron.daily etc. It's script content is called in the appropriate schedule.

To schedule your script with that directories, keep the following in mind:

  • The script is not allowed to have a file extension (myscript instead of myscript.sh)
  • The script needs to have a correct shebang (e.g. #!/bin/bash or #!/usr/bin/php)
  • The script needs UNIX linebreaks (if you create your script in Windows, use Notepad++ and use the menu item Edit/Format Line Ending/UNIX.
  • The script's owner needs to be loxberry (chown loxberry:loxberry <filename>) and is executed as loxberry. It has no root permissions!
  • The script needs execute permission (chmod +x <filename>).
  • Don't name the script as a plugin is called. A plugin may overwrite your script if it uses the same name.

The content of the file is the script, not a cron schedule!

For delivering such a file during installation, just put your script in your plugin's directory structure into the folder cron. The script has to be named correctly, details can be found here: unterverzeichniscron_optional.

You can also create a script on the fly during plugin configuration for example. Make sure to use your plgin's folder name for the file, otherwise it will not be uninstalled if nessesary.

Using own cronjob schedule (allows root execution)

Automatically install schedule during plugin install

Just put your cronjob schedule in your plugin's directory structure into the folder cron. The schedule must have the name crontab. Details can be found here: unterverzeichniscron_optional.

Manually install schedule during plugin install

Do not use crontab -e as root to create a cronjob. This job will be lost after a reboot.

Cronjobs on LoxBerry are placed in:

  • /opt/loxberry/system/cron/cron.d

The files are crontab schedules including the user:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
# m h dom mon dow user  command
00 00 */1 * * loxberry /opt/loxberry/bin/plugins/lbbackup/runbackup.pl type=DDZ scheduled=true > /dev/null 2>&1

Without the MAILTO="" directive, every output of the script is sent by email. On testing, comment out the directive to get messages (possibly error message).

To schedule your script inside cron.d, keep the following in mind:

  • As this is a system-wide crontab, in the schedule the user root or loxberry must be defined. 
  • The schedule is not allowed to have a file extension
  • The schedule needs UNIX linebreaks (if you create your script in Windows, use Notepad++ and use the menu item Edit/Format Line Ending/UNIX.
  • The schedule file owner needs to be root (chown root:root <filename>) and is executed as root. If your permissions differ, the task is not executed and you may get warning emails.
  • The script and the schedule need exactly the following permission: chmod 755 <filename>). If your permissions differ, the task is not executed and you may get warning emails.
  • Don't name the script as a plugin is called. A plugin may overwrite your schedule if it uses the same name.

The content of the file is a crontab schedule, not a script!

Use Google for help on creating crontab files.

Root execution

If you install this crontab during plugin installation, it is also possible to use "root" as the execution user. But keep in mind, that later on changes to the crontab file (e. g. from plugin configuration) will always replace root with loxberry due to security reasons (see next chapter)

Changing cronjob schedule after installation

If you would like to change your cronjob schedule after installation was finished (e. g. during plugin configuration), you need to use a wrapper script to install your crontab, because the crontab is owned by root and cannot be changed directly from your webform (which is executed as user loxberry).

Create your new crontab somewhere in a temporary folder (e. g. /tmp) and use the wrappterscript /opt/loxberry/installcrontab.sh to install it:

# /opt/loxberry/sbin/installcrontab.sh <pluginname> <crontabfile>
/opt/loxberry/sbin/installcrontab.sh myplugin /tmp/temp_crontabfile

Your new crontabfile will be installed to /opt/loxberry/system/cron/cron.d/myplugin. Note! You cannot use the root user in your newly created crontab! 'root' is always replaced by 'loxberry' when installing.

Own scripts on LoxBerry

Keep also in mind, that LoxBerry Update will delete all scripts inside the /opt/loxberry directory, that are not part of LoxBerry or it's plugins. Use the legacy folder, or keep your scripts outside of /opt/loxberry.

But cronjobs/scripts in /opt/loxberry/system/cron.xxx are not deleted by LoxBerry Update. 

See also