计划后台任务

为了正常运行,必须定期执行iTop维护操作和异步任务。为了简化安装,所有后台任务都被分组为从单个文件启动:webservices/cron.php

以下功能依赖于cron.php的激活

  • 果不调用此Web服务,则工单到达阈值(TTOOTTR)时不会发生通知。 (任务:检查秒表三个持有)
  • 检查工单SLA。达到限制的工单将传递到升级TTOOTTR状态。
  • 自动备份。
  • 自动删除在对象创建或版本被取消期间创建的无用的附件和嵌入式图像(任务:每小时运行的InlineImageGC)
  • 异步电子邮件。默认情况下,此选项处于禁用状态。要启用它,请将配置文件中的'发送邮件_asynchronous'设置为1(任务: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-cron.params >>/var/log/itop-cron.log 2>&1

不要使用根用户运行cron.php!您必须使用运行iTop的Web服务器使用的相同用户启动命令。

通过网络安排

页面cron.php也可以通过Web服务器执行。如果您无权访问系统(例如在共享主机系统上),这将很有用。在这种情况下,您可以依靠Web cron服务为您运行它。但是与本地计划的作业相比,频率和执行时间可能会受到限制。

争论

论据描述Defaut价值
param_file参数文件的路径 
auth_user用户登录名-仅CLI模式,如果未使用任何参数文件-
auth_pwd用户密码-仅CLI模式,如果未使用任何参数文件-
verbose如果设置为1,将给出更多信息,请使用此信息进行故障排除。请注意:由于流程在后台中连续运行,因此日志文件往往会很快变得很大。请勿长时间将此选项设置为1。0
status_only(iTop 2.0.1中的新增功能)如果设置为1,则会显示有关计划任务的状况,并且流程立即停止(可以与cron.php的正常执行并行启动)。0

参数文件

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

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

  • 参数文件包含键值对。
  • 注释以#开头(在#之后找到的任何字符都将被忽略)

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

例:

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

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

设定值

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

设置描述Defaut价值
cron_max_execution_timecron.php页面的持续时间(秒),必须短于php设置max_execution_time且短于Web服务器回复超时600
cron_sleepcron.php再次检查是否必须执行某些操作之前的持续时间(秒)2
email_asynchronous如果设置为1,则电子邮件将离线发送,这需要激活cron.php。例外:发送邮件测试功用之类的某些功能将强制执行序列化模式0

允许的用户

仅允许管理员执行cron.php。

故障排除

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

cron.php在做什么?

  • cron.php程序在启动时,询问实现此php接口的任务,并将其记录在表priv_backgroundtask中。
  • 该表中存在的条目依赖于安装的iTop扩展。
  • priv_backgroundtask包含每个任务的多个信息,例如:
    • 当它最后一次运行时,
    • 下一次运行应该何时开始,
    • 该任务的平均执行时间是多少?
    • 根据管理员(字段状况)的请求,任务当前处于活动状态还是已暂停?
  • cron.php程序,查找活动的任务并传递下一次运行,
    • 一个接一个地执行任务,要求每个任务在cron_max_execution_time之后停止执行。该任务当然会在有意义的时候停止,例如对于邮件自动创建工单,它可以在处理2个收到的邮件之间停止。
    • 在下一次运行时询问任务,并将该信息存储在表中,
  • 然后,它检查自身启动时间是否超过cron_max_execution_time,如果是,它会自杀(这是因为永无休止的流程通常会消耗未完全释放的内存)
  • 否则,它将等待cron_sleep秒,然后再次检查表以查找准备好执行的任务。

什么时候可以做什么...

似乎永远不会执行任务:

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

应该监视日志文件以生成警报,以使高级注意到此问题。

原贴链接: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/25, 16:36 创建
    

需要帮助?

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

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