gpt4 book ai didi

php - Python 没有正确重启 linux 进程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:10 24 4
gpt4 key购买 nike

我有一个 PHP 项目,我使用 Python 将其部署到生产服务器上。

这是部署计划:

  1. 找到新的php.ini文件(路径已定义)

  2. 用这个文件替换当前的

  3. 通过os.system('service php-fastcgi restart')重启PHP-FPM进程,其中php-fastcgi是进程的真实名称。

Python 在脚本执行期间不会显示任何错误,但 PHP 会以默认配置重新启动。当我尝试手动重启它时(在 Linux 终端中),它运行良好并且新的 php.ini 配置加载成功。你能解释一下我的 Python 脚本的这种奇怪行为吗?

更新

这是Python脚本的一部分。

    php_ini_path_replace = '/etc/php5/cgi/php.ini'
php_ini_path_source = os.path.join(destination, 'production', 'config', 'main-php.ini')

try: # Read source file
source_conf_file = open(php_ini_path_source, 'r')
php_ini_lines = source_conf_file.readlines()
except IOError:
print('Something is wrong with source file')

try:
actual_conf_file = open(php_ini_path_replace, 'w')
actual_conf_file.writelines( php_ini_lines )
print('PHP CGI configuration was succesfully changed.\nDon\'t forget to restart the PHP')
except IOError:
print('Something is wrong with actual file. May be it\'s in use')

os.system('service php-fastcgi restart')

最佳答案

使用copyfile而不是手动打开和关闭文件。

import shutil

php_ini_path_replace = '/etc/php5/cgi/php.ini'
php_ini_path_source = os.path.join(destination, 'production', 'config', 'main-php.ini')

try:
shutil.copyfile(php_ini_path_source, php_ini_path_replace)
except (Error,IOError):
print('Error copying the file')

os.system('service php-fastcgi restart')

关于php - Python 没有正确重启 linux 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com