Setting Up a Cronjob

Cron is the time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading emails.

Jelastic gives you a possibility to run your programs at specified time with a help of cron, which receives your instructions and performs any tasks following the derived scenarios. Our customers can use this opportunity to perform periodic tasks, such as:

  • logs clearing;
  • backing up;
  • scripts running etc.
To do so, follow the next instructions:

  1. Create the environment

    1. Log into the
      Jelastic Manager
    2. Click Create environment.

    3. In the Environment topology window choose the application server you want to use (for example, Tomcat) and MySQL (or MariaDB) as your database. Type the name of the environment and click Create.

    4. In a minute your environment with both Tomcat and MySQL nodes will be created.

  2. Upload your script

    • for application server

      1. Click the Config button for your application server.

      2. Upload the scripts you want to perform to:
        1. home folder if you use JAVA server in your environment (as we do in this instruction)
        2. the folder where your application is stored if you use PHP server in your environment

    • for database

    1. Click the Config button for your database (MySQL or MariaDB).

    2. Upload the scripts you want to perform to the scripts folder.

    3. Note: Script you want to perform should be an executable file. If it isn't, you can use built-in interpreters, such as Bash, Perl, Python, SED, AWK or Expect (depending on the script you use).

  3. Setting up cron event scheduler

    1. In the already opened configurations tab navigate to the cron folder and open {} file.

    2. event scheduler
    3. Here you can write all necessary commands to schedule your tasks. They should be composed in a way so that crontab will understand them.

      1. The basic format of a crontab schedule consists of 6 fields, separated by spaces, formatted as follows:

      2. minute hour day month day-of-week command-line-to-execute
        The acceptable values for each of the 6 fields are:
        Field Range of values
        minute 0-59
        hour 0-23
        day 1-31
        month 1-12
        day-of-week 0-7 (where both 0 and 7 mean Sun,
        1 = Mon, 2 = Tue, etc)
        command-line-to-execute the path to the program you want to run

      3. While stating the values you need to follow the next standards:

        • The fields have to be in that exact order, with no empty or missing fields.
        • If needed you can use a wildcard character - "*" (the asterisk). In a crontab file it represents every possible value for the field. For example, specifying "*" in the "hour" means "to run every hour."
        • '/' character is used to specify additional frequency assignments. For example, '* / 3' in the "hour" means "every three hours."
        • For each individual parameter, you can specify multiple values separated by commas. For example, if in the "hour" you type 1,6,19, the job will run at 1:00 am, at 6:00 am and 19 hours. You can also specify the interval, for example, 8-17 would mean that the program will be run every hour from 8 to 17 inclusive.

      4. Pay attention that you should write a full path to your script in the cron command. Depending on the location of your script the path can be as one of the following:
        • home folder for Java compute nodes:
        • /opt/tomcat/temp/{your_script_name} - Tomcat 6,7
          /opt/tomcat/temp/{your_script_name} - TomEE
          /opt/jetty/home/{your_script_name} - Jetty 6
          /opt/glassfish3/temp/{your_script_name} - GlassFish
        • folder with your application files & scripts for PHP compute nodes:
        • /var/www/webroot/{yourfolder}/{your_script_name}
        • scripts folder for MySQL/MariaDB databases:
        • /var/lib/jelastic/bin/{your_script_name}

    4. If your script is located in the home folder of Tomcat and you want it to be performed every minute, your command line can look like this example:

    5. */1 * * * * /opt/tomcat/temp/

      Note: If your script does not have executable flag and you use built-in interpreters, such as Bash, Perl, Python, SED, AWK or Expect (depending on your script), you should add the following paths after time settings for explicit specifing the interpreter:
      • /bin/bash - for Bash
      • /usr/bin/python - for Python
      • /usr/bin/perl - for Perl
      • /bin/sed - for SED
      • /bin/awk - for AWK
      • /usr/bin/expect - for Expect
      • /usr/bin/php - for php-interpreter

      In this case your command will be as following:

      */1 * * * * /bin/bash /opt/tomcat/temp/

    6. Please make sure there is a blank line after the last cronjob entry.

    7. Save the changes in order to apply the settings.

That's all. As you can see, it is pretty easy to schedule your tasks with Jelastic. Enjoy!

Was kommt als Nächstes?