How to add custom environment variables in AnQiCMS `crontab` tasks and make them effective in `crontab`?

Calendar 👁️ 70

Good, as an experienced website operation expert, I am very willing to delve into how to operate in AnQiCMS'scrontabAdd and enable custom environment variables in the task. This not only makes your scheduled tasks more flexible, but also better meets the needs of automated operations and specific environment configurations.


Unlock the potential of automation: in AnQiCMS'scrontabadd custom environment variables in the task

AnQiCMS (AnQiCMS) boasts its high efficiency and customizable features, becoming a powerful assistant for many small and medium-sized enterprises and content operation teams.Among them, the powerful scheduling and task planning functions (such as the "Time Factor - Scheduling Function" and "Task Planning Function" mentioned in the log) have realized the automation of content operation, greatly improving efficiency.These automated tasks typically rely on the Linux systemcrontabto execute.

However, when usingcrontabAt times, many operators may encounter a confusion: why does the script run well in a normal shell environment, but when placedcrontabIt's just that “the soil and water are not suitable”, some environment variables seem to be missing? This is the core issue we need to solve today: how to handle in AnQiCMS'scrontabIn the task, add it elegantly and effectively and make the custom environment variable take effect.

UnderstandingcrontabThe running environment: a 'pure' sandbox

To solve this problem, you first need to understand.crontabThe operation mechanism of the task. Unlike the interactive shell environment we are in after logging in to the Linux system daily,crontabWhen executing scheduled tasks, a very "pure" and minimalist shell environment is usually created. This means that you set up in your own terminal.PATH/LD_LIBRARY_PATHEnvironment variables, as well as any custom environment variables, will not be automatically passed tocrontabthe task. It is like an isolated sandbox, inheriting only a few of the most basic environment variables.

This isolation has its advantages in ensuring system stability and security, but it also requires us to explicitly tell when configuring the scheduled taskscrontabAll the environmental information it needs to know.

Strategy one: directly incrontabEntries define environment variables (simple but limited)

The most direct method is to do it in yourcrontabIn each task definition line, add environment variables directly. This method is very convenient for scenarios where only one or two temporary variables are needed.

Assuming you have a time task script for AnQiCMS, for examplestart.sh(As mentioned in the AnQiCMS installation document), and this script or the Go program it calls needs a name ofANQI_ENV_MODEThe environment variable determines its running mode. You can modify it like this.crontabEntry:

# 打开您的用户crontab进行编辑
crontab -e

# 在文件末尾添加或修改如下行
# 注意:变量名=值 必须放在实际执行的命令前面
*/1 * * * * ANQI_ENV_MODE="production" /www/wwwroot/anqicms.com/start.sh

Advantage:

  • Simple operation, clear at a glance.
  • Suitable for temporary settings of individual or a few variables.

Shortcomings:

  • If you need to set multiple environment variables,crontabthe file will become long and difficult to read.
  • If multiple scheduled tasks require the same environment variables, they need to be defined repeatedly, which is not convenient for centralized management and modification.
  • Once the environment variable values need to be changed, you may need to modify multiple places.crontabEntry, prone to errors.

Strategy two: Introduce environment variables through external scripts (recommended: flexible, neat, easy to manage)

For scenarios where AnQiCMS requires multiple scheduled tasks or tasks require complex environment variable configurations, it is strongly recommended to manage through a separate environment variable configuration file. This method can enable yourcrontabEntries should be neat and highly flexible.

The installation document of AnQiCMS usually has onestart.sha script to start the service,crontabIs calling this script. We can use this entry point, instart.shBefore executing, first “import” our environment variables.

Step one: Create and configure the environment variable file

First, in your AnQiCMS project directory (for example/www/wwwroot/anqicms.com/), create a new shell script file, for example namedanqicms_cron_env.shIn this file, you can useexportcommand to define all the required environment variables.

# 使用您熟悉的编辑器创建文件,例如:
# vim /www/wwwroot/anqicms.com/anqicms_cron_env.sh

# 文件内容示例:
#!/bin/bash

# 确保所有路径都是绝对路径,因为crontab环境可能没有预设PATH
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
export ANQI_ENV_MODE="production"
export ANQI_DEBUG_LOG_PATH="/var/log/anqicms/cron_debug.log"
export ANQI_TASK_TIMEOUT="300s" # 任务超时时间

# ...更多您需要的环境变量

Key points:

  • Absolute path:It is imperative to use the absolute path of environment variables, especiallyPATHwhich can avoid issues due tocrontabenvironmentPATHmissing that cause commands to be unable to be found.
  • exportCommand: exportis indispensable, ensuring that these variables are inherited by child processes (i.e.,start.shoranqicmsInheritance of Go program.
  • Clear comments:Appropriate comments can help you understand the purpose of each variable.

After creating, make sure the file has execute permissions:

chmod +x /www/wwwroot/anqicms.com/anqicms_cron_env.sh

Step two: modifycrontabThe entry to import environment variables

Next, you need to modifycrontabThe entry to make it run in the AnQiCMSstart.shscript, first load the one we just createdanqicms_cron_env.shfile. This can be done bysourcecommand (or its abbreviation form).)to achieve this.

# 再次打开您的用户crontab进行编辑
crontab -e

# 修改原有的AnQiCMS定时任务行,在前面加上 source 命令
# 假设您的AnQiCMS路径是 /www/wwwroot/anqicms.com/
*/1 * * * * source /www/wwwroot/anqicms.com/anqicms_cron_env.sh && /www/wwwroot/anqicms.com/start.sh

Explanation:

  • `source /www/wwwroot/anqicms

Related articles

What is the relationship between configuring Nginx reverse proxy and `crontab` after manual deployment of AnQiCMS?

As an experienced website operations expert, I fully understand the importance of every link in the process of building and maintaining a website, especially for a system like AnQiCMS (AnQiCMS) that pursues efficiency and stability. The configuration of various items after deployment needs to be carefully crafted.Today, let's delve into the relationship between Nginx reverse proxy and `crontab` scheduled tasks after AnQiCMS manual deployment, and how they work together to ensure the stable operation of your website.## After manual deployment of AnQiCMS, configure Nginx

2025-11-06

AnQiCMS `crontab` task did not run on time, how to troubleshoot and correct it?

As an experienced website operations expert, I know that scheduled tasks (`crontab`) play a crucial role in content management systems, especially in platforms like AnQiCMS that pursue efficiency and automation.It not only ensures timely content release and regular data backup, but also provides automatic recovery protection in case of system anomalies.

2025-11-06

Why does AnQiCMS `start.sh` need to check PID to avoid repeated startup?

## Core Guardian: Deep Consideration of PID Check in AnQiCMS `start.sh` Script As an expert in website operations for many years, I am well aware that every detail of system stability is crucial.For a content management system like AnQiCMS that追求high efficiency and simplicity, even though its underlying Go language has excellent concurrent processing capabilities, we still need a rigorous mechanism to ensure the robustness of its operating environment.Today, let's delve deeply into something that seems simple

2025-11-06

What is the diagnostic value of the log output `running.log` in the AnQiCMS `crontab` configuration?

AnQiCMS as an efficient and customizable enterprise-level content management system, its stable operation is the foundation of website operation.It is crucial to understand the internal operation mechanism and diagnostic tools during the deployment and maintenance of AnQiCMS.Among them, the `running.log` file generated by the `crontab` configuration is a highly diagnostic 'black box' that records the startup status and critical information of the system's core services in the early stages of operation.As an experienced website operation expert, I will take you deep into the analysis of `running.log`

2025-11-06

Who is the default user for `crontab` tasks in AnQiCMS? How to modify it?

As an experienced website operations expert, I am well aware of the importance of AnQiCMS as an efficient content management system in daily operations.The configuration of scheduled tasks (`crontab`) is a key factor in ensuring the stable operation of the system, timely publication of content, and timely update of data.For many AnQiCMS users, especially those who are new to the Linux environment, they may be puzzled about the execution user of the `crontab` task.Today, let's delve deeply into this issue.

2025-11-06

How to update all related `crontab` tasks at once when upgrading AnQiCMS multi-site in bulk?

In the multi-site operation of AnQi CMS, efficiency and stability are always the core pursuit.As a CMS system developed based on the Go language, AnQiCMS has won the favor of many operators with its excellent performance and multi-site management capabilities.However, when faced with multi-site batch upgrades, how to巧妙ly handle the resulting `crontab` task updates to ensure a smooth transition for all sites, this is undoubtedly a problem that many operation experts need to ponder.Today, let's delve into this topic and provide you with a set of effective strategies.###

2025-11-06

AnQiCMS `crontab` error message提示 “command not found”, how to solve?

## AnQiCMS `crontab` error提示 “command not found” in-depth analysis and solution As an expert in website operation for many years, I know how crucial stable automated tasks are when managing a high-efficiency content management system like AnQiCMS.AnQiCMS leverages its high-performance architecture in the Go language and rich features, providing us with powerful support for content publishing, SEO optimization, and even scheduled tasks.

2025-11-06

How to use `crontab` to regularly clean up the old log files generated by AnQiCMS?

As an experienced website operation expert, I know that AnQiCMS (AnQiCMS) provides efficient content management for enterprises while, like all excellent systems, it silently records a large amount of operation logs in the background.These log files are crucial for understanding the system's operational status and troubleshooting.However, over time, they will also quietly occupy the valuable server storage space, even affecting system performance.Therefore, regularly cleaning these old log files is an indispensable operation and maintenance link to ensure the long-term stability and efficient operation of AnQiCMS. Today

2025-11-06