『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 3725|回复: 4

[技术探讨] command line switch为何总报command failed

[复制链接]

26

主题

176

帖子

726

积分

高级会员

Rank: 4

积分
726
金钱
550
HASS币
0
发表于 2018-5-8 20:32:52 | 显示全部楼层 |阅读模式
本帖最后由 hdcola 于 2018-5-8 20:40 编辑

我写了一个这样的配置

switch:
  - platform: command_line
    switches:
      remote_helper:
        command_on: "sudo systemctl start frpc"
        command_off: "sudo systemctl stop frpc"
        command_state: "sudo systemctl is-active frpc.service"
        value_template: '{{ value == "active" }}'


为何总报

Command failed: sudo systemctl is-active frpc.service

我的sudoer配置是不需要密码的,而且on和off执行都没有问题,就是在switch在off的状态下报这个error
回复

使用道具 举报

30

主题

999

帖子

4119

积分

论坛元老

Rank: 8Rank: 8

积分
4119
金钱
3115
HASS币
0

活跃会员

发表于 2018-5-9 09:23:02 | 显示全部楼层
应该是权限问题, homeassistant用户不具备sudo的权限
回复

使用道具 举报

26

主题

176

帖子

726

积分

高级会员

Rank: 4

积分
726
金钱
550
HASS币
0
 楼主| 发表于 2018-5-9 11:52:47 | 显示全部楼层
wqqs 发表于 2018-5-9 09:23
应该是权限问题, homeassistant用户不具备sudo的权限

我已经加好和sudo 权限,使用homeassistan用户做相同命令的执行是没有问题的
回复

使用道具 举报

26

主题

176

帖子

726

积分

高级会员

Rank: 4

积分
726
金钱
550
HASS币
0
 楼主| 发表于 2018-5-10 07:27:03 | 显示全部楼层
自问自答吧。先看看 command line switch的实现方法

    @staticmethod
    def _query_state_value(command):
        """Execute state command for return value."""
        _LOGGER.info("Running state command: %s", command)

        try:
            return_value = subprocess.check_output(command, shell=True)
            return return_value.strip().decode('utf-8')
        except subprocess.CalledProcessError:
            _LOGGER.error("Command failed: %s", command)


这里执行命令时,加了一个try,看了一下man systemctl我才想明白:

       is-active PATTERN...
           Check whether any of the specified units are active (i.e. running). Returns an exit
           code 0 if at least one is active, or non-zero otherwise. Unless --quiet is specified,
           this will also print the current unit state to standard output.


我直接python用了下subprocess.chect_output,发现确实丫exit 3了。解决也简单,我写了一个shell
#!/bin/sh
systemctl is-active frpc.service
exit 0


然后把command_state改为执行这个shell就完事儿了。
回复

使用道具 举报

26

主题

176

帖子

726

积分

高级会员

Rank: 4

积分
726
金钱
550
HASS币
0
 楼主| 发表于 2018-5-10 07:30:44 | 显示全部楼层
又想了一下,还有一个不写shell的更简单的方法:
subprocess.check_output('systemctl is-active frpc.service || exit 0',shell=True)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-20 18:24 , Processed in 0.623793 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表