如何在服务 (systemd) 执行的脚本中执行命令 zenity(文件 .service)

How to execute the command zenity in a script executed by a service (systemd) (file .service)

我有个小问题。我正在用 systemd 做一个服务(守护进程)。脚本如下:

if [ $intento = 5 ];then
      iptables -I INPUT -s ${sublista[0]} -j DROP -m comment --comment "IP bloqueada por sshield"
      date=$(date)
      echo "${sublista[0]} $date" >> /var/cache/sshield.deny
      zenity --notification --text "IP address ${sublista[0]} denied at $date - sshield"
      email ivanherediaplanas@hotmail.com "Nueva regla iptables | ${sublista[0]} denied" "The ${sublista[0]} ip address is denied by brute force's attack ssh.<br><br>Date: $date"
      declare -a ips=(${ips[@]/${sublista[0]}=>$intento/})
fi

思路如下:

If the attempts is more than five, it give the ip address and lock it. Sending a mail and showing a zenity's popup

问题是,弹出窗口没有显示。

zenity --notification --text “IP 地址 ${sublista[0]} 在 $date - sshield 被拒绝”

我相信这是因为,脚本是由 /lib/systemd/system/sshield.service

中的服务文件执行的
[Unit]
Description=Service for protect attacks of brute force ssh's

[Service]
Type=simple
ExecStart=/etc/sshield/sshield.sh
ExecStop=/etc/sshield/sshield.sh stop
RemainAfterExit=yes
Restart=always

[Install]
WantedBy=multi-user.target

我认为问题出在:Type=simple

此外,我尝试这样做:

  echo "${sublista[0]} $date" >> /var/cache/sshield.deny
  sshield --bell "IP address ${sublista[0]} denied at $date - sshield"
  email ivanherediaplanas@hotmail.com "Nueva regla iptables | ${sublista[0]} denied" "The ${sublista[0]} ip address is denied by brute force's attack ssh.<br><br>Date: $date"

sshield --bell "IP address ${sublista[0]} denied at $date - sshield"

命令 sshield 是路径 /bin/sshield 中的一个脚本,我得到它:

elif [[ $argumento == "--bell" ]];then
    if [[ $# -gt 3 ]];then
            echo -e "3[1;31m[-]3[0m Only one value"
            echo "You use '--help' or '-h' for more information"
    elif [[ $# = 1 ]];then
            echo -e "3[1;31m[-]3[0m It needs one value"
            echo "You use '--help' or '-h' for more information"
    else
            zenity --notification --text ""
    fi
else
  [...]

标记:zenity --notification --text "$2"

但是,它不起作用。我该如何解决?

错误在于zenity: image: journalctl -u sshield

用于在服务 (systemd) 中执行 GUI(图形用户界面)。首先,您必须添加:

[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/ivan/.Xauthority"

结果:

[Unit]
Description=Service for protect attacks of brute force ssh's

[Service]
Type=simple
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/ivan/.Xauthority"
ExecStart=/etc/sshield/sshield.sh
ExecStop=/etc/sshield/sshield.sh stop
RemainAfterExit=yes
Restart=always

[Install]
WantedBy=multi-user.target

并在脚本中添加:

export DISPLAY=":0"