Pular para o conteúdo principal
Base de Conhecimento da FocusVision

Setting Up Automation with Cronwrap

  Requires Decipher Cloud

1.  Overview

The Decipher software allows scheduling automated jobs using a tool called cronwrap. User scripts are created in the shell environment and are scheduled to run periodically.

2.  Create Your Script

Your shell script can be written in Bash, Python or any other Unix programming language. It is recommended to use Bash or Python.

It is recommended you run store these script files in your project directory or in your /home/user folder.  In the example below, the bash script file is located in the /home/user/myscript.sh.

Please see here for various example scripts written by Decipher.  

3.  Scheduling/Testing Your Script With Cronwrap

3.1  Create A Config File (.conf):

Navigate to folder: /home/hermes/v2/data/cronwrap.d

You will create your config file here.  Copy the template config file below and name it myscript.conf (or whatever you like).

#Group name
[my_automations]
  comments = "My Automation Project"

  #Job Name
  [[my-job-1]]

    name = "My Job Name"
    command = "bash /home/user/myscript.sh"
    maxtime = 1200

    fail_only_recipients = "fail@domain.com fail2@domain.com"
    all_recipients = "recipient@domain.com recipient2@domain.com"

    error_list = 
    success_list =

Note that indenting the file is optional but recommended.  To add more jobs, simply add another job with the relevant variables. e.g. my-job-2

Below are the definitions for each of the variables described in the .conf file.

Variable Description
name Name of job
command Script or command you need to run
maxtime If the job doesn't finish on given maxtime (seconds), the process will get killed
fail_only_recipients Recipients who will get an email ONLY if job fails
all_recipients Recipients who will get an email all the time
error_list Job will fail if these strings are matched in stdout. They can be regular expressions.
success_list Job will fail if these strings are NOT found in stdout. They can be regular expressions.

Note:  If you are creating a conf file on the v2 server, navigate to folder: /home/jaminb/v2/data/cronwrap.d instead.

3.2  Scheduling The Job To Run

In the shell, type the below command:

crontab -e

The server may prompt you for a text editor to use as your default. nano or vim are recommended. To change this, you can run select-editor in the terminal to re-select your editor.

Once inside crontab, you will supply the config group/task to run and the time to run it.  Please be sure to include lines 1-2 at the top of your crontab.  This will setup the environment variables required.

PATH=$PATH:/usr/local/bin:/home/hermes/bin:/usr/bin:/usr/local/bin:/bin:/usr/sbin:/sbin
HERMES2_HOME=/home/hermes/v2

0 6 * * 1-5 /home/hermes/bin/cronwrap --group=my_automations --task-name=my-job-1

Note:  If you are using this on the v2 server, change 'home/hermes/bin' to 'home/jaminb/bin' in the code block above.

In the above example (line 3), the script will run at 6am local server time on Monday-Friday only. To get the local server time, type date in the command line and press enter.

The first 5 positions represent minute, hour, day, month, day_of_week respectively.

  • minute — any integer from 0 to 59
  • hour — any integer from 0 to 23
  • day — any integer from 1 to 31 (must be a valid day if a month is specified)
  • month — any integer from 1 to 12 (or the short name of the month such as jan or feb)
  • day_of_week — any integer from 0 to 6, where 0 represents Sunday

Here are some more examples.

Example Syntax
Run once a year at midnight of Janurary 1 0 0 1 1 *
Run once a month at midnight of the first day of the month 0 0 1 * *
Run once a week at midnight on Sunday morning 0 0 * * 0
Run once a day at midnight 0 0 * * *
Run once an hour at the beginning of the hour 0 * * * *

For further assistance, you can access the crontab generator page.

3.3  Testing Your Script

To test your automation, duplicate your crontab -e entry above (line 3 in this example) and change the the time to run 5 minutes from your current time.

Example, let's say it was 8:15am local server time.  You would duplicate your entry and adjust the following to run at 8:20am local server time:

#Original group/task
0 6 * * 1-5 /home/hermes/bin/cronwrap --group=my_automations --task-name=my-job-1

#My test run, Duplicated and adjusted time to 8:20am.
20 8 * * * /home/hermes/bin/cronwrap --group=my_automations --task-name=my-job-1

Wait until 8:20am local server time and ensure the job ran successfully.  Once deemed successful, you can delete the duplicate entry by going back into crontab.

4.  Reviewing Automation Report Page

You can use the format of the link below to review automation jobs for your server. This report will show you the status of each cron run and show all automations scheduled on your server.

https://your-server.decipherinc.com/apps/automations 

*where <your-server> is your cloud server sub-domain.

For example, if your portal login page is this URL: https://client.decipherinc.com/apps/portal,

You would use this link format to access the automation report:
https://client.decipherinc.com/apps/automations

* where <client> is the client's name.

5.  Script/Automation Examples

The following scripts are examples written by our training staff. These scripts are samples to help you start writing your own custom scripts. Please update relevant areas of the script, such as survey path, email addresses, path to script, etc.

5.1 Generating latest excel data file and emailing to a list of users

Click here to download the files. Files included are listed below:

  • example-excel-data-file-email.conf
  • crontab -e.txt
  • example-excel-data-file-email.sh

5.2 Using the Decipher REST API to generate a list of active users

Click here to download the files. Files included are listed below:

  • example-api-user-list-python.conf
  • crontab -e.txt
  • example-api-user-list-python.sh
  • example-api-user-list-python.py (this file is executed in example-api-user-list-python.sh script file)

5.3  Using the Decipher REST API to get a weekly CSV file + Datamap and uploading to an SFTP.

Click here to download the files. Files included are listed below:

  • example-api-csv-data-sftp.conf
  • crontab -e.txt
  • example-api-csv-data-sftp.sh

Note: This script requires you to know how to setup an SSH private/public key using ssh-keygen. For a tutorial on how this is done, visit this page.