Php Python没有';不要正确地重新启动linux进程

Php Python没有';不要正确地重新启动linux进程,php,python,linux,process,Php,Python,Linux,Process,我有一个PHP项目,我使用Python将其部署到生产服务器上 以下是部署计划: 查找新的php.ini文件(已定义路径) 用此文件替换当前文件 通过os.system('service PHP fastcgi restart')重新启动PHP-FPM进程,其中PHP fastcgi是进程的真实名称 Python在脚本执行期间不会显示任何错误,但PHP会使用默认配置重新启动。当我尝试手动重新启动它时(在Linux终端中),它工作得很好,新的php.ini配置成功加载。你能解释一下我的Python脚

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

以下是部署计划:

  • 查找新的
    php.ini
    文件(已定义路径)

  • 用此文件替换当前文件

  • 通过
    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')
    

    writelines()
    写入的数据可能会保留在进程内缓存中,直到文件被刷新(如在C中)。随后启动的其他进程可能会看到一个空文件或部分文件。完成编写后,需要添加的是
    source\u conf\u file.close()
    。(这是一个令人恼火的问题,因为当Python进程完成时,文件会被刷新,如果您稍后尝试查看它,它会显示正确。)

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

    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')
    

    我认为您最好粘贴shell cmd中的返回代码或字符串,它可以帮助我们找到根本原因

    有些人建议:

    请记住关闭文件处理程序。您可以将
    一起使用。比如:

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

    把你所说的真实剧本贴出来怎么样?谢谢!我不喜欢Python。您不需要在重写文件之前清理文件句柄吗?也许可以尝试获取独占文件锁?至少在其他语言中我会担心这些。谢谢!我会再试试的