Setting Up a Cronjob in Jelastic Cloud

| June 11, 2013

Cronjob allows you to configure regularly scheduled tasks so that the jobs can be run automatically at a pre-set point of time. It repeats itself and does not need any regular manual instructions. Cron automates your system and can be used for quite different purposes. This wonderful tool is a standard part of all sysadmins toolkit. Also cronjob has a straightforward syntax and emails all output by default.

Jelastic PaaS provides you with a possibility to run your scripts at specified time with a help of cron, which receives your commands and performs any tasks following the derived scenarios. You can use this opportunity for performing of periodic tasks, such as:

  • data backup;
  • logs clearing;
  • script running etc.

The next instruction will help you to set up cronjob in your Jelastic environment.

Create the environment

1. Log into Jelastic Manager and click on Create environment button.

2. In the Environment topology dialog select the application server you want to use (e.g. Tomcat) and your database (e.g. MySQL). Switch on Public IPv4 for your app server if it's needed for your script running (like in our case) and enter the name of your environment.

cronjob environment

In some minutes your environment with both nodes will be created.

Upload script

In our case we'll use a simple bash script, which counts the number of unique site visits and outputs this info in the report.html file, as an example.

1. Navigate to your application server configuration directory.

cronjob server config

2. Upload the script you would like to perform to the folder where your application is located if you use PHP server or to home folder if you use JAVA server in your environment.

VISITS=$(cat $APACHE_LOG_FILE | awk '{print $1}' | sort | uniq | wc -l);
echo "Visits report Amount of unique visits for today - $VISITS " > $VISITS_REPORT_FILE;
cronjob check visits

Note: Your script should be an executable file. For other cases use built-in interpreters, such as Bash, Perl, Python, SED, AWK or Expect (depending on your script).

Set up cron

1. In the config tab navigate to the cron folder and open apache file (your server name file).

2. Here you can enter all necessary commands to schedule your jobs. The basic format of a crontab schedule you can find in this document.

In our case we'll run our script every minute.

*/1 * * * * /bin/bash /var/www/webroot/ROOT/
cronjob command

Make sure there is a blank line after the last cronjob entry.

If you use interpreters, such as Bash, Perl, Python, SED, AWK or Expect, add the following paths after time settings for explicit specifying 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

3. Save the changes in order to apply new settings.

4. Waite a minute and open Apache in a web browser to check the result.

cronjob result

For better understanding of how to use cronjob for your database server (e.g. for scheduled data backups) in Jelastic use our previous tutorial on how to schedule database backups as an example.

Cronjob is a nifty tool to prevent problems from occurring and provide you with piece of mind. As you see, it is very easy to schedule your tasks with Jelastic new feature. Enjoy!