后台任务

为了正常运行,必须定期执行iTop维护操作和异步任务。为了简化安装,已将所有的后台任务集中到单一文件(webservices/cron.php)中运行。

以下功能依靠cron.php的激活:

  • Notification(通知) 如果没有调用该web服务,则不会在单据到达阈值(TTO/TTR)时发送通知。 (Task:CheckStopWatchThresholds)

  • 检查ticket SLA. 到达限制的单据将进入升级TTO/TTR状态。

  • 自动备份。

  • 自动删除在一个对象创建或编辑时被取消期间的无用附件和插入的图片 (Task: InlineImageGC 每小时运行)

  • 异步的电子邮件。默认情况下,该选择处于禁用状态。要启用它,将配置文件里'email_asynchronous'参数设置为 1。(Task: ExecAsyncTask)

许多扩展插件也依赖于cron文件,例如,从邮件创建单据,自动审批,联系客户等

Windows上的调度安排

使用schedtask.exe 或者用 'at' 命令安排cron.php 每5分钟执行一次。

schtasks.exe /create /sc minute /mo 5 /tn "iTop CRON" /tr "\"/var/www/itop/webservices/cron.cmd\""

Linux上的调度安排

编辑 crontab 每5分钟执行一次脚本:

*/5 * * * * www-data /usr/bin/php /var/www/html/itop/webservices/cron.php --param_file=/etc/itop/params >>/var/log/itop-cron.log 2>&1

不要使用root用户运行cron.php!

从网站调度安排

也可以通过网页 服务器执行 cron.php。如果你无权限访问系统(例如在共享主机上),这将非常有用。在这种情况下,你可以依靠一个Web cron服务调用它。但同本地计划任务相比,频率和执行次数可能会受到限制。

参数

参数描述默认值
param_file参数文件的路径cron.params
auth_user用户登录 - 仅CLI 模式,如果未使用参数文件-
auth_pwd用户密码 - 仅CLI 模式,如果未使用参数文件-
verbose

如果设置为1,将提供更多信息,请使用此信息进行故障排除。请注意:由于该进程在后台连续运行,因此日志文件往往很快就会变得很大。请勿长时间将此选项设置为1。

0
status_only(iTop 2.0.1中的新增功能) 如果设置为1,则会显示有关计划任务的状态,并且该过程将立即停止(可以在cron.php正常执行时被执行)0

参数文件

由于在命令行上传递给cron.php的参数对系统上的其他用户可见,因此建议的做法是将所有敏感参数从命令行移开。这就是“参数文件”的目的。

参数param_file可以与大多数REST / CLI Web服务一起使用。按照约定,cron.php服务搜索参数文件名«cron.params»以读取其参数。

  • 参数文件包含键/值对

  • 注释以#开头(在#之后找到的任何字符都将被忽略)

iTop软件包包含一个名为cron.distrib的文件,该文件可用作创建自己的参数文件的模板。

例如:

cron.params
 
# This is a parameter file
#
# If a parameter is given both in the file and in the arguments,
# then the value given as argument is retained
#
 
# Authentication
auth_user = qwertyuiop
auth_pwd = ded!catedL0g1n
 
# My web service
size_min = 20 # Megabytes
time_limit = 40 # Minutes

确保无法从网上访问参数文件 !可以将其移动到Web服务器范围之外的目录中(例如, /etc/itop/在Linux系统上将是一个不错的位置),或者在Web服务器的配置中制定特殊的排除规则。只有从命令行运行cron.php的进程才能访问此文件。

设定值

这些设置已配置到主要的iTop配置文件中:“itop-config.php”

设置描述默认值
cron_max_execution_time

cron.php页面的持续时间(秒),必须短于php设置max_execution_time且短于Web服务器响应超时要求

600
cron_sleepcron.php再次检查是否必须执行某些操作之前的持续时间(秒)2
email_asynchronous

如果设置为1,则电子邮件将离线发送,这需要激活cron.php。例外:电子邮件测试实用程序等某些功能将强制执行序列化模式

If set to 1, the emails are sent off line, which requires cron.php to be activated. Exception: some features like the email test utility will force the serialized mode

0

授权用户Allowed users

只允许管理员执行 cron.php.

排除故障

当任务不再运行时,需要了解以下几点:

cron.php在做什么?

  • cron.php程序启动后,要求实现该php接口的任务,并将其记录在priv_backgroundtask表中。

  • 该表中存在的条目取决于安装的iTop扩展。

  • priv_backgroundtask 包含每个任务的多个信息,例如:

    • 当它最后一次运行时,
    • 下一次运行应该何时开始,
    • 该任务的平均执行时间是多少?
    • 当前管理员要求的任务是active还是 paused(字段状态)
  • cron.php程序,查找活动的任务并通过下一次运行

    • 一个接一个地执行任务,要求每个任务停止执行在超过 cron_max_execution_time之后。该任务当然会在有意义的时候停止,例如对于Mail To Ticket Automation,它可以在处理2个收到的邮件之间停止。
    • 在下一次运行时要求任务将该信息存储在表中,
  • 然后,它检查自身启动cron_max_execution_time是否大于 ,如果是,它会自己停止(这是因为永无止境的进程通常会消耗未完全释放的内存

  • 否则,它会等待一cron_sleep秒钟,然后再次检查表以查找准备好执行的任务。

当...

一个任务似乎永远不会执行:

当特定任务遇到使其陷入无限循环的问题时,cron.php可能会被php的max_execution_time参数停止(通常在CLI中运行时不会停止 );在所有情况下,紧随其后的任务将永远不会执行。在cron.php接下来的运行中,由于任务总是以相同的顺序执行,因此可能会再次出现相同的问题。为了识别故障任务,您应该 暂停 通过在priv_backgroundtask表中设置它们的状态来查看一个怀疑有此问题的任务, 然后再查看是否停止了该故障任务。这种情况通常是由于扩展程序带来的错误所致,该任务面临非计划的情况。

日志文件应该被监控以生成警报,以提前通知此类问题。

2_6_0/admin/cron.txt · Last modified: 2019/01/09 16:40 (external edit)

原贴链接:https://www.itophub.io/wiki/page?id=2_7_0%3Aadmin%3Acron


Background tasks

In order to operate properly, iTop maintenance operations and asynchronous tasks must be executed on a regular basis. In order to ease the installation, all the background tasks have been grouped to be launched from a single file: webservices/cron.php

The following features rely on the activation of cron.php

  • Notification when a ticket reach a threshold (TTO/TTR) will not happen without this web service to be called. (Task:CheckStopWatchThresholds)

  • Check ticket SLA. Tickets reaching the limits will be passed into Escalation TTO/TTR states.

  • Automated Backups.

  • Automatic deletion of useless attachments and in-line images, created during an object creation or edition which was cancelled (Task: InlineImageGCwhich runs  hourly)

  • Asynchronous emails. By default, this option is disabled. To enable it, set 'email_asynchronous' to 1 in the configuration file (Task: ExecAsyncTask)

  • Many Extensions rely on this cron as well, for exemple: Ticket Creation from Email, Approval Automation, Communication to Customers,…

Scheduling on Windows

Use schedtask.exe or the 'at' command to schedule cron.php to run every 5 minutes.

schtasks.exe /create /sc minute /mo 5 /tn "iTop CRON" /tr "\"/var/www/itop/webservices/cron.cmd\""

Scheduling on Linux

Edit the crontab to execute the script every 5 minutes:

*/5 * * * * www-data /usr/bin/php /var/www/html/itop/webservices/cron.php --param_file=/etc/itop-cron.params >>/var/log/itop-cron.log 2>&1

Do not run cron.php using root user! You must launch the command with the same user that is used by the web server running iTop.

Scheduling from the web

The page cron.php can also be executed through the web server. This is useful if you don't have access to the system (for example on shared hosting systems). In such a case you can rely on a web cron service to run it for you. But the frequency and execution times will probably be restricted compared to a natively scheduled job.

Arguments

ArgumentDescriptionDefaut value
param_filePath to the parameters file 
auth_userUser login - CLI mode only, if no parameters file is used-
auth_pwdUser password - CLI mode only, if no parameters file is used-
verboseif set to 1, more information will be given, use this for troubleshooting. Beware: since the process runs continuously in background, the log files tend to become big pretty quickly. Don't set this options to 1 for a long period of time.0
status_only(New in iTop 2.0.1) if set to 1, a status about the scheduled tasks is displayed and the process stops immediatly (can be launched in parallel to the normal execution of cron.php).0

Parameter file

Since the arguments passed to cron.php on the command line are visible to other users on the system, a recommended practice is to move all the sensitive parameters away from the command line. This is the purpose of the “parameter file”.

The argument param_file can be used with most of the REST/CLI web services. By convention, the cron.php service searches for a parameter file name « cron.params » to read its parameters from.

  • A parameter file contains key/value pairs.

  • Comments start with a # (any character found after `#` will be ignored)

The iTop package contains a file named cron.distrib that can be used as a template for creating your own parameter file.

Example:

itop-cron.params
 
# This is a parameter file
#
# If a parameter is given both in the file and in the arguments,
# then the value given as argument is retained
#
 
# Authentication
auth_user = qwertyuiop
auth_pwd = ded!catedL0g1n
 
# My web service
size_min = 20 # Megabytes
time_limit = 40 # Minutes

Make sure that the parameter file is not reachable from the web! Either move it to a directory outside of the web server's scope (for example /etc/itop/ on Linux systems would be a good location) or make a special exclusion rule in your webserver's configuration. Only the process that runs cron.php from the command line should be able to access this file.

Settings

Those settings are configured into the main iTop configuration file: “itop-config.php”

SettingDescriptionDefaut value
cron_max_execution_timeDuration (seconds) of the page cron.php, must be shorter than php setting max_execution_time and shorter than the web server response timeout600
cron_sleepDuration (seconds) before cron.php checks again if something must be done2
email_asynchronousIf set to 1, the emails are sent off line, which requires cron.php to be activated. Exception: some features like the email test utility will force the serialized mode0

Allowed users

Only administrators are allowed to execute cron.php.

Troubleshooting

When a task is no more running, there are a few things to know:

What is the cron.php doing?

  • The cron.php program, when started, ask for Tasks which implements this php interface, and log them in the table priv_backgroundtask.

  • The entries present in this table depends on the iTop installed extensions.

  • priv_backgroundtask contains multiple information for each task, such as:

    •  

    when it run for the last time,

    •  

    when is the next run supposed to start,

    •  

    what is the average execution time of this task,

    •  

    Is the task currently active or paused as requested by an administrator (field status)

    •  

  • The cron.php program, look for tasks which are active and next run is passed,

    •  

    execute the tasks one after the other asking each task to stop its execution after cron_max_execution_time. The task would of course stops at a point where it makes sense, for example for Mail To Ticket Automation, it could stop between the processing of 2 received Mails.

    •  

    ask the task when should be the next run and store that info in the table,

  • Then it checks if it-self was started more than cron_max_execution_time, if yes it commits suicide (this is because a never ending process usually consumes memory which is not totally freed)

  • else it waits for cron_sleep second before checking the table again for tasks ready to be executed.

What can be done when...

A task seems to never be executed:

When a particular task encounters an issue which make it hangs in an infinite loop, cron.php might be stopped by the php max_execution_time (usually not when running in CLI); in all cases the tasks which are following that one will never be executed. In the following run of cron.php, as the tasks are always executed in the same order, the same issue will probably occur again. For identifying the faulty task, you should paused one after the other the tasks which you suspect to have this issue by setting their status in the priv_backgroundtask table and see if you have stopped the faulty one. Such situation is usually due to a bug of the extension bringing that task, the task facing an unplanned situation.

The logfile should be monitored to generate alarm to get advanced notice of such issue.

标签:
由 superadmin 在 2020/08/27, 15:56 创建
    

需要帮助?

如果您需要有关XWiki的帮助,可以联系:

深圳市艾拓先锋企业管理咨询有限公司