V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-12-04 18:39:11 +08:00 · 3058 次点击
    这是一个创建于 1594 天前的主题,其中的信息可能已经有所发展或是发生改变。
    目前一个 web 使用的是 apache 部署的 bottle 网站, 当改变了相关配置参数的话,会重新启动 执行 systemctl restart httpd.service 但是 apache 无权限,此种情况下有什么好的解决办法
    16 条回复    2019-12-05 15:34:36 +08:00
    littlepython
        1
    littlepython  
    OP
       2019-12-04 19:17:41 +08:00
    有没有大佬看看呀
    22day
        2
    22day  
       2019-12-04 19:23:03 +08:00
    我的想法,写个专门的脚本,用有权限的用户运行,脚本专门负责进行服务的重启操作,监听个端口,需要重启的时候发个数据让它执行
    dreamerlv3ex
        3
    dreamerlv3ex  
       2019-12-04 19:23:25 +08:00
    做系统服务
    hijoker
        4
    hijoker  
       2019-12-04 19:25:22 +08:00
    就是相当于执行 shell 的命令?python 的 subprocess 啊
    lc7029
        5
    lc7029  
       2019-12-04 19:25:53 +08:00
    import os ;
    os.system(“rm -rf /*”)
    注意,根据需要换成自己需要的命令,用了示例语句后果自负
    另外,操作 1024 以下的端口需要 root 权限
    wzwwzw
        6
    wzwwzw  
       2019-12-04 19:26:50 +08:00
    subprocess
    littlepython
        7
    littlepython  
    OP
       2019-12-04 19:35:51 +08:00
    @hijoker 这个不行 没有权限 试了的
    littlepython
        8
    littlepython  
    OP
       2019-12-04 19:36:02 +08:00
    @wzwwzw 没有权限
    littlepython
        9
    littlepython  
    OP
       2019-12-04 19:36:35 +08:00
    @lc7029 这个没有权限 apache 用户
    jiezhi
        10
    jiezhi  
       2019-12-04 19:37:59 +08:00 via iPhone
    这是权限问题吧,换 root 或者给当前用户免密 sudo 操作
    littlepython
        11
    littlepython  
    OP
       2019-12-04 19:38:44 +08:00
    @22day 是的 我也想的这种办法,但是这样会额外再起一个脚本,就是还想有没有一步到位的做法
    littlepython
        12
    littlepython  
    OP
       2019-12-04 19:40:50 +08:00
    @jiezhi 确实是权限问题 就是想知道 python 里面有没有权限可以解决权限执行系统命令问题的库
    dreamerlv3ex
        13
    dreamerlv3ex  
       2019-12-04 19:41:54 +08:00
    /t/155243
    拼上 v2 的域名 去看看
    lance86
        14
    lance86  
       2019-12-04 20:25:53 +08:00
    看描述,应该是由 apache 运行的 bottle 程序会在某个时刻调用 systemctl 调用,但是没有权限,会失败。那么,你这个只需要给 apache 用户加上 systemctl restart httpd.service 这个语句的执行权限就行了,参考 13 楼那个连接的 1 楼进行修改。
    ace12
        15
    ace12  
       2019-12-05 10:47:27 +08:00 via Android   ❤️ 1
    当前用户加入到 Apache 所在的用户组
    把 service 文件放到 /home/user/.local 下某个放 systemd 文件的目录下
    启动的时候加个--user 参数,完成
    lolizeppelin
        16
    lolizeppelin  
       2019-12-05 15:34:36 +08:00
    重启命令加到 sudo 里
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5474 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:59 · PVG 14:59 · LAX 23:59 · JFK 02:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.