带有export命令的shell脚本和通过crontab的notify-send不工作。导出的变量由命令设置 [英] Shell script with export command and notify-send via crontab not working. Exported variable is set by a command

查看:205
本文介绍了带有export命令的shell脚本和通过crontab的notify-send不工作。导出的变量由命令设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过cron执行的shell脚本向gnome桌面发送通知气泡。



为了能够与桌面通信环境 DBUS_SESSION_BUS_ADDRESS env变量需要在脚本中设置。我试图通过下面的代码实现这一点不工作。

 #!/ usr / bin / env sh 
export DISPLAY =:0
dbus = $(env | grep DBUS_SESSION_BUS_ADDRESS | sed'/ DBUS_SESSION_BUS_ADDRESS = //')
export DBUS_SESSION_BUS_ADDRESS = $ dbus
[...]
notify-send -u criticalBlahBlubb

  export DBUS_SESSION_BUS_ADDRESS ='unix:abstract = / tmp / dbus-HjnsLUTTrn,guid = 17c8962443279ebbe24dcd66536278dd'

我做错了什么?

解决方案

env cron 运行的脚本只在我的框中设置了以下变量。

  SHELL = / bin / sh USER = clement PATH = / usr / bin:/ bin PWD = / home / clement HOME = / home / clement SHLVL = 2 LOGNAME = clement _ = / usr / bin / env 

,因此 grep
$ b

  $(env | grep DBUS_SESSION_BUS_ADDRESS | sed's / DBUS_SESSION_BUS_ADDRESS = //')

没有给出任何输出。以下内容应该有效。

 #!/ bin / bash 
PID = $(pgrep gnome-session)
dbus = $(grep -z DBUS_SESSION_BUS_ADDRESS / proc / $ PID / environ | cut -d = -f2-)
export DBUS_SESSION_BUS_ADDRESS = $ dbus
notify-send -u criticalBlahBlubb


I'd like to send a notification bubble to the gnome desktop from a shell script that is executed by cron.

To make it able to communicate with the desktop environment DBUS_SESSION_BUS_ADDRESS env variable needs to be set within the script. I'm trying to achieve this by the following code which isn't working.

#!/usr/bin/env sh
export DISPLAY=:0
dbus=$(env | grep DBUS_SESSION_BUS_ADDRESS | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
export DBUS_SESSION_BUS_ADDRESS=$dbus
[...]
notify-send -u critical "Blah" "Blubb"

When hardcoded it works fine

export DBUS_SESSION_BUS_ADDRESS='unix:abstract=/tmp/dbus-HjnsLUTTrn,guid=17c8962443279ebbe24dcd66536278dd'

Problem is, the dbus session address changes so hardcoding isn't an option.

What am I doing wrong?

解决方案

env when invoked from a script run by cron, only had the following variables set in my box.

SHELL=/bin/sh USER=clement PATH=/usr/bin:/bin PWD=/home/clement HOME=/home/clement SHLVL=2 LOGNAME=clement _=/usr/bin/env

and so grep in

$(env | grep DBUS_SESSION_BUS_ADDRESS | sed 's/DBUS_SESSION_BUS_ADDRESS=//')

didn't give any output. The following should work.

#!/bin/bash
PID=$(pgrep gnome-session)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
export DBUS_SESSION_BUS_ADDRESS=$dbus
notify-send -u critical "Blah" "Blubb"

这篇关于带有export命令的shell脚本和通过crontab的notify-send不工作。导出的变量由命令设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆