没有不值得去解决的问题,也没有不值得去学习的技术!

Running a Command in Windows 10 Triggers an Error: Call to undefined function App\Console\Commands\posix_getpid()

在 Windows 10 中执行命令行,报错:Call to undefined function App\Console\Commands\posix_getpid()。

1. Running the command line in Windows 10 throws an error: Call to undefined function App\Console\Commands\posix_getpid(). See Figure 1.

Running the command line in Windows 10 throws an error: Call to undefined function App\Console\Commands\posix_getpid().
Figure 1
Plaintext

PS E:\wwwroot\msi_main> php artisan migrate

In ExportRecords.php line 65:

  Call to undefined function App\Console\Commands\posix_getpid()



2. Reference URL: https://www.php.net/manual/zh/intro.posix.php . This extension is not available on Windows platforms. You can also try using getmypid() instead. See Figure 2.

Reference URL: https://www.php.net/manual/zh/intro.posix.php . This extension is not available on Windows platforms. You can also try using getmypid() instead.
Figure 2

3. posix_getpid — Returns the current process ID. Decided to replace it with: getmypid — Gets PHP’s process ID. Reference URL: https://www.php.net/getmypid.

4. Edit /app/Console/Commands/ExportRecords.php. Replace posix_getpid with getmypid.

PHP

    private function setLogPrefix() {
        // $pid = posix_getpid();
		$pid = getmypid();
        $ppid = posix_getppid();  //获取父进程id
        $this->log_prefix = "CommandExportRecords : pid:{$pid} : ppid:{$ppid} :";
        return $this;
    }

5. Continue running the command line, and an error is thrown: Call to undefined function App\Console\Commands\posix_getppid().

Plaintext

PS E:\wwwroot\msi_main> php artisan migrate

In ExportRecords.php line 67:

  Call to undefined function App\Console\Commands\posix_getppid()



6. posix_getppid does not work on Windows. This is an alternative solution. Reference URL: https://www.php.net/manual/zh/function.posix-getppid . Check if it is a Windows system; if so, use the alternative solution. See Figure 3.

posix_getppid does not work on Windows. This is an alternative solution. Reference URL: https://www.php.net/manual/zh/function.posix-getppid . Check if it is a Windows system; if so, use the alternative solution.
Figure 3
PHP

    private function setLogPrefix() {
		if(strncasecmp(PHP_OS, "win", 3) == 0) {
			$pid = getmypid();  // child process ID
			$ppid = shell_exec("wmic process where (processid=$pid) get parentprocessid");
			$ppid = explode("\n", $ppid);
			$ppid = intval($ppid[1]);
		} else {
			$pid = posix_getpid();
			$ppid = posix_getppid();  //获取父进程id
		}
        $this->log_prefix = "CommandExportRecords : pid:{$pid} : ppid:{$ppid} :";
        return $this;
    }

7. Continue running the command line, and no further errors occur. See Figure 4.

Continue running the command line, and no further errors occur.
Figure 4
Plaintext

PS E:\wwwroot\msi_main> php artisan migrate
Migration table created successfully.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系