我正在尝试从cron运行bash脚本,但似乎没有任何工作。
脚本:
#!/bin/sh
printf '%s' $(date) '_END' >> battery_log.txt
export DISPLAY=:0
kdialog --msgbox "$(upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage")"
crontab(作为sudo):
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/king/Scripts
#
# m h dom mon dow command
* * * * * bash /home/king/Scripts/my_battery_script
我已经检查确认:
the script is executable
root user owns the script
I can run the script from within a shell using ./my_battery_script
I can run other bash scripts from within cron (I created a test script that wrote to a text file). Worked fine
kdelibs-full is installed
为什么这个脚本不会运行?
修改:更多详情
脚本的实际执行记录如下:
++ date
+ printf %s Mon Jan 11 22:29:01 EST 2016 _END
MonJan1122:29:01EST2016_END+ export DISPLAY=:0
+ DISPLAY=:0
++ upower -i /org/freedesktop/UPower/devices/battery_BAT0
++ grep -E 'state|to\ full|percentage'
++ cat /sys/class/thermal/thermal_zone0/temp
++ awk '{print "deg C: "$1/1000}'
+ /usr/bin/kdialog --msgbox ' state: discharging
percentage: 58% \n cpu temp: deg C: 44.5'
No protocol specified
kdialog: cannot connect to X server :0
答案 0 :(得分:2)
+ /usr/bin/kdialog --msgbox ' state: discharging percentage: 58% \n cpu temp: deg C: 44.5' No protocol specified kdialog: cannot connect to X server :0
假设您实际上有一台X服务器正在运行以显示0,这通常意味着您有一个操作系统级安全措施,阻止root访问该显示。
这里简单的答案是在您的用户帐户下运行此cron作业,而不是以root身份运行。
答案 1 :(得分:0)
尝试在脚本中添加kdialog的路径,看起来像找不到命令