AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1275206
Accepted
Abrar Hossain
Abrar Hossain
Asked: 2020-09-15 20:29:26 +0800 CST2020-09-15 20:29:26 +0800 CST 2020-09-15 20:29:26 +0800 CST

从 cron 运行脚本时令人困惑的行为

  • 772

我有一个简单的脚本。这是代码:

#/bin/bash


saveLoc="/root/test";
dt=$(date '+%d-%m-%Y-%H:%M:%S');
scrot_name_fmt="${dt}.jpeg";
streamer_name_fmt="${dt}.jpeg";


# This captures the current screen
cd `echo ${saveLoc}/1`;
/usr/bin/scrot -z -b `echo $scrot_name_fmt`;

# This captures the webcam
cd `echo ${saveLoc}/2`;
/usr/bin/streamer -f jpeg -o `echo $streamer_name_fmt`;

echo "${dt} saved";

这个脚本做了两件事:

  1. 截图并保存在/root/test/1目录中
  2. 拍摄网络摄像头并将其保存在/root/test/2目录中

当我运行脚本时,它按预期工作,我看到每个目录添加了两个图像文件。当脚本由 cron 运行时,我看到在目录 2 中拍摄了网络摄像头,但在目录 1 中没有截图。这是 cron 作业设置:

* * * * * root sh /root/test/script.sh >> /root/test/logScript.log

不知道可能是什么问题,我们将不胜感激。谢谢

编辑

我尝试将以上内容分为 2 个脚本。我从终端运行了两个脚本,它按预期工作。包含该scrot命令的脚本 1 在 cron 中不起作用,但在我手动运行时起作用。包含streamer在终端和由 cron 运行时的脚本 2。

编辑 2

经过几个小时的艰苦搜索,我发现了错误。gnome-screenshot 无法连接到 X11 服务器。这是我尝试截屏时遇到的错误:

Unable to init server: Could not connect: Connection refused

(gnome-screenshot:13349): Gtk-WARNING **: 06:48:01.878: cannot open display:

编辑 3

根据@raj 的建议,我导出了XAUTHORITY. 它没有用。这是所有更改后的脚本:

#/bin/bash

export XAUTHORITY=/run/user/1000/gdm/Xauthority;
save_loc="/root/test";
dt=$(date '+%d-%m-%Y-%H:%M:%S');
scrot_name_fmt="${dt}.jpeg";
streamer_name_fmt="${dt}.jpeg";


# This captures the current screen
cd `echo $save_loc/1`;
echo `pwd`;
DISPLAY=:0
/usr/bin/gnome-screenshot -b -f `echo $scrot_name_fmt`;

# This captures the webcam
cd `echo $save_loc/2`;
echo `pwd`;
/usr/bin/streamer -f jpeg -o `echo $streamer_name_fmt`;

echo "screenshot ${dt} saved";
echo "cam-shot ${dt} saved";

最终编辑

我正在为@bac0n 添加这个。这与他在回答中建议的原则上相似。这是在 Ubuntu 20.04 中测试的

# Locate Xauthority
for I in /run/user/*; do
    # get the number of directory 
    d="`echo $I | awk -F '/' '{ print $4 }'`";
    if [ $d -eq "121" ]; then
        # Root user; continue
        continue;
    else
        # now locate the Xauthority in gdm
        # loads Xauthority for current user only
        for J in /run/user/$d/gdm/*; do
            AUTHUSER="`echo $J | awk -F '-' '{ print $3 }'`";
            for K in $USER; do
                [ "${AUTHUSER}" = "${K}" ] || continue;
                USER="${K}";
                export XAUTHORITY="/run/user/${d}/gdm/Xauthority";
                break;
            done;
        done;
    fi;
done;

这可行,但看起来很难看。

bash cron scrot-command
  • 1 1 个回答
  • 345 Views

1 个回答

  • Voted
  1. Best Answer
    user986805
    2020-09-17T23:59:38+08:002020-09-17T23:59:38+08:00

    Maim 截图工具旨在成为比 scrot 更好的选择。Systemd.timer 用作基于时间的激活。默认情况下,您的计时器会激活同名服务。调度您的任务,您可以使用由日历表达式定义的实时计时器或从计时器本身激活的那一刻开始的单调计时器(或者您可以将两者结合使用)。

    /etc/systemd/system/maim-screenshot.timer:

    [Unit]
    Description=Take a screenshot every 15 minutes.
    
    [Timer]
    OnBootSec=5m
    OnUnitActiveSec=15m
    
    [Install]
    WantedBy=timers.target
    

    /etc/systemd/system/maim-screenshot.service:

    [Unit]
    Description=Maim Screenshot service.
    
    [Service]
    UMask=0077
    ExecStart=/opt/bin/maim_screenshot.sh
    

    该脚本迭代所有登录会话,寻找 x11 会话。然后该脚本尝试使用位于用户主目录中的用户 Xauthority 文件。Xauthority 文件将允许 maim 以 root 身份运行。

    /opt/bin/maim_screenshot.sh

    #!/bin/bash
    #
    # Based on maim (make image) utility.
    
    # Screenshot folder.
    mkdir -m 750 -p /opt/images; images=$_
    
    b=($(loginctl show-seat seat0 \
        -p Sessions --value --no-legend))
    
    mapfile -t < <(loginctl show-session ${b[@]} \
        -p User -p Display -p Type --all | tr -s \\n)
    
    declare -A A
    ((c=${#MAPFILE[@]} / ${#b[@]}))
    for ((d=0, e=0; $d < ${#b[@]}; d++, e+=$c)); do
        A=()
        for f in "${MAPFILE[@]:$e:$c}"; do
            [[ $f =~ ^([^=]*)=(.*)$ ]] && \
            A[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]}
        done
        if [[ ${A[Type]} = x11 ]] \
            && [[ $(getent passwd ${A[User]}) \
            =~ ([^:]*:){5}([^:]+) ]] \
            && [[ -r ${BASH_REMATCH[2]}/.Xauthority ]]
        then
            XAUTHORITY=${BASH_REMATCH[2]}/.Xauthority \
            maim -x "${A[Display]}" -i root "/$images/screenshot_$EPOCHSECONDS.jpg"
        fi
    done
    exit 0
    

    最后我们需要使脚本可执行并启动计时器。

    $ chmod +x /opt/bin/maim_screenshot.sh
    $ systemctl enable --now maim-screenshot.timer
    
    • 0

相关问题

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 如何每 5 秒运行一次脚本?

  • 如何使 cron 电子邮件成为我的 @gmail 帐户

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve