> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/sys/acct.2.md).

# acct.2

`acct` — 启用或禁用进程记账

## 名称

`acct`

## 库

Lb libc

## 概要

`#include <unistd.h>`

```c
int
acct(const char *file);
```

## 描述

`acct()` 系统调用启用或禁用系统记账记录的收集。如果参数 `file` 是空指针，则禁用记账。如果 `file` 是一个*已存在的*路径名（以空字符结尾），则启用记录收集；对于每个在正常条件下终止的进程，都会向 `file` 追加一条记账记录。异常终止条件包括重启或其他致命系统问题。对于从未终止的进程，`acct()` 无法产生记录。

有关 `acct()` 所用记录结构的更多信息，请参见

`#include <sys/acct.h>`

以及 [acct(5)](/man/man5/acct.5.md)。

此调用仅允许超级用户使用。

## 注释

当记账文件所在的文件系统空间耗尽时，记账会自动禁用；当空间再次可用时，记账会重新启用。控制此行为的值可以使用以下 [sysctl(8)](/man/man8/sysctl.8.md) 变量修改：

**`kern.acct_chkfreq`** 指定检查空闲磁盘空间的频率（以秒为单位）。

**`kern.acct_resume`** 空闲磁盘空间百分比高于此值时，进程记账将恢复。

**`kern.acct_suspend`** 空闲磁盘空间百分比低于此值时，进程记账将暂停。

## 返回值

出错时返回 -1。文件必须存在，且该调用仅能由超级用户执行。

## 错误

`acct()` 系统调用在以下任何一种情况为真时会失败：

**\[EPERM]** 调用者不是超级用户。

**\[ENOTDIR]** 路径前缀中的某个组件不是目录。

**\[ENAMETOOLONG]** 路径名的某个组件超过 255 个字符，或整个路径名超过 1023 个字符。

**\[ENOENT]** 指定的文件不存在。

**\[EACCES]** 路径前缀的某个组件拒绝搜索权限，或路径名不是常规文件。

**\[ELOOP]** 在转换路径名时遇到过多的符号链接。

**\[EROFS]** 指定的文件位于只读文件系统上。

**\[EFAULT]** `file` 参数指向进程分配地址空间之外。

**\[EIO]** 在对文件系统进行读写操作时发生 I/O 错误。

**\[EINTEGRITY]** 在从文件系统读取数据时检测到损坏的数据。

## 参见

[acct(5)](/man/man5/acct.5.md), accton(8), sa(8)

## 历史

`acct()` 函数首次出现于 Version 7 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/sys/acct.2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
