推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
littlepython
V2EX  ›  Python

Python 如何调用系统命令

  •  
  •   littlepython · Dec 4, 2019 · 4597 views
    This topic created in 2529 days ago, the information mentioned may be changed or developed.
    目前一个 web 使用的是 apache 部署的 bottle 网站, 当改变了相关配置参数的话,会重新启动 执行 systemctl restart httpd.service 但是 apache 无权限,此种情况下有什么好的解决办法
    16 replies  •  2019-12-05 15:34:36 +08:00
    littlepython
    littlepython
    OP
    Dec 4, 2019
    1
    有没有大佬看看呀
    22day
    22day
    Dec 4, 2019
    2
    我的想法,写个专门的脚本,用有权限的用户运行,脚本专门负责进行服务的重启操作,监听个端口,需要重启的时候发个数据让它执行
    dreamerlv3ex
    dreamerlv3ex
    Dec 4, 2019
    3
    做系统服务
    hijoker
    hijoker
    Dec 4, 2019
    4
    就是相当于执行 shell 的命令?python 的 subprocess 啊
    lc7029
    lc7029
    Dec 4, 2019
    5
    import os ;
    os.system(“rm -rf /*”)
    注意,根据需要换成自己需要的命令,用了示例语句后果自负
    另外,操作 1024 以下的端口需要 root 权限
    wzwwzw
    wzwwzw
    Dec 4, 2019
    6
    subprocess
    littlepython
    littlepython
    OP
    Dec 4, 2019
    7
    @hijoker 这个不行 没有权限 试了的
    littlepython
    littlepython
    OP
    Dec 4, 2019
    8
    @wzwwzw 没有权限
    littlepython
    littlepython
    OP
    Dec 4, 2019
    9
    @lc7029 这个没有权限 apache 用户
    jiezhi
    jiezhi
    Dec 4, 2019 via iPhone
    10
    这是权限问题吧,换 root 或者给当前用户免密 sudo 操作
    littlepython
    littlepython
    OP
    Dec 4, 2019
    11
    @22day 是的 我也想的这种办法,但是这样会额外再起一个脚本,就是还想有没有一步到位的做法
    littlepython
    littlepython
    OP
    Dec 4, 2019
    12
    @jiezhi 确实是权限问题 就是想知道 python 里面有没有权限可以解决权限执行系统命令问题的库
    dreamerlv3ex
    dreamerlv3ex
    Dec 4, 2019
    13
    /t/155243
    拼上 v2 的域名 去看看
    lance86
    lance86
    Dec 4, 2019
    14
    看描述,应该是由 apache 运行的 bottle 程序会在某个时刻调用 systemctl 调用,但是没有权限,会失败。那么,你这个只需要给 apache 用户加上 systemctl restart httpd.service 这个语句的执行权限就行了,参考 13 楼那个连接的 1 楼进行修改。
    ace12
    ace12
    Dec 5, 2019 via Android❤️1
    15
    当前用户加入到 Apache 所在的用户组
    把 service 文件放到 /home/user/.local 下某个放 systemd 文件的目录下
    启动的时候加个--user 参数,完成
    lolizeppelin
    lolizeppelin
    Dec 5, 2019
    16
    重启命令加到 sudo 里
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2760 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 10:41 · PVG 18:41 · LAX 02:41 · JFK 05:41
    ♥ Do have faith in what you're doing.