# Cron Jobs

A cron job (linux) or scheduled task (windows) is an automated task set up to perform without user interaction. Maian Support has several areas of the system where a cron job can benefit you, these are as follows:
1 - Reading a mailbox via the imap functions.
2 - Sending email digest of currently open tickets.
3 - Creating database schema backup.
4 - Auto closing tickets after x amount of days that are open and awaiting visitor response.

Cron Job Setup

Setting up a cron job can be fairly easy, but is dependant of the server setup. Most servers enable you to set up a cron job via the control panel (such as CPanel, Direct Admin etc) which makes the process nice and easy. This page will not go into detail about cron jobs. If you aren`t sure, contact your web host.
With a cron job you just need 2 things:
1 - The command to run. This is to trigger a file on the server.
2 - The date/time to run the job. Cron jobs can be advanced, running every minute, other minute, hour, 2hrs, day, etc. You decide how often you want it to run.

Database Backup, Email Digest, Auto Close

In Maian Support, the files you need to run these routines are as follows and are all located in the support root:
1 - For the database backup: db-backup.php
2 - For the email digest: email-digest.php
3 - For the auto close routine: close-tickets.php
Note that when the email digest is run, it will automatically trigger the auto close routine if enabled, so you may not need to set these jobs up separately. It depends on how often you want each to run.

Imap Mailboxes

If you are setting up a cron for the mailboxes, this is slightly different as the routine needs to know what mailbox it is reading. The cron will accept 2 parameters, the second parameter for the language is optional. The first parameter is the ID number of the mailbox. You can find this on the imap management page. Refer to the initial screenshot. This parameter name is specified in your settings and on install defaults to "pipe".
So, this would be the same as accessing: index.php?pipe=XX, where XX is your imap ID number and "pipe" is your preferred param name.
The second param is the language parameter, this is "lang". It will default to the default language in the settings if not set. If you are using the multi language option, you can pass an alternative language. So, this param would append and be the same as running: index.php?pipe=XX&lang=YY, where XX is your imap ID number and YY is your specified language. Language must exist in the system.
NOTE: The passing of params in cron jobs may differ on a windows server due to the query strings.

Cron Command Examples - Linux

These are examples of linux based cron commands for the email-digest and also for a mailbox with the additional params. Note that some or all of these may work. If none work, contact your host for assistance. The auto close and db backup routines will be the same as the email digest, but with the different file name as specified above.
1 /usr/local/bin/php /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
/usr/local/bin/php /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
2 php q /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
php q /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
3 /usr/local/bin/php q /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
/usr/local/bin/php q /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
4 curl "http://www.example.com/helpdesk/email-digest.php"
curl "http://www.example.com/helpdesk/index.php?pipe=XX&lang=german"

Cron Command Examples - Windows

These are examples of windows based cron commands for the email-digest and also for a mailbox with the additional params. Note that some or all of these may work. If none work, contact your host for assistance. The auto close and db backup routines will be the same as the email digest, but with the different file name as specified above. Also note how different the params are sometimes set for windows servers. The curl option (if available), may be the same.
1 cmd C:\Windows\php\php.exe C:\website\helpdesk\email-digest.php
cmd C:\Windows\php\php.exe C:\website\helpdesk\index.php pipe[]=XX lang[]=german (NO ? or &, but WITH spaces and square brackets as shown)
2 curl "http://www.example.com/helpdesk/email-digest.php"
curl "http://www.example.com/helpdesk/index.php?pipe=XX&lang=german"

Cron Command Examples - Linux

These are examples of linux based cron commands for the email-digest and also for a mailbox with the additional params. Note that some or all of these may work. If none work, contact your host for assistance. The auto close and db backup routines will be the same as the email digest, but with the different file name as specified above.
1 - /usr/local/bin/php /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
/usr/local/bin/php /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
2 - php q /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
php q /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
3 - /usr/local/bin/php q /home/admin/domains/example.com/public_html/helpdesk/email-digest.php
/usr/local/bin/php q /home/admin/domains/example.com/public_html/helpdesk/index.php?pipe=XX
4 - curl "http://www.example.com/helpdesk/email-digest.php"
curl "http://www.example.com/helpdesk/index.php?pipe=XX&lang=german"

Cron trigger Override via Curl

If the server curl option isn`t available in your control panel and the cron jobs are failing, you can simulate it on the server using Curl code (providing the curl functions are installed). This example shows how to trigger a mailbox from another file.
1 - Create a new .php file in a text editor with the following code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/helpdesk/index.php?pipe=XX");
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
2 - Save the file and upload it to the root of your support system.
3 - Change your cron job in your control panel to access this new .php file. This will send a trigger to the 'index.php' file. Repeat the above process for each cron.