【问题标题】:Is it possible to determine what called a bash shell script?是否可以确定所谓的 bash shell 脚本?
【发布时间】:2021-02-23 12:50:01
【问题描述】:

有什么方法可以确定 shell 脚本的调用方式吗?

我有一个 bash 脚本,它在 macOS 上的熊掌记应用程序的注释中添加了一行。它组成一个 x-callback url 并打开它。

这些是最相关的最后几行(到此为止的一切都是获取和设置变量值):

#compose URL
theURL="bear://x-callback-url/add-text?id=${noteId}&mode=prepend&new_line=yes&text=$urltext"

#x-callback
open "$theURL"

脚本在每个工作日通过launchd 运行一次。这是.plist的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/sbin</string>
    </dict>
    <key>Label</key>
    <string>jobname</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/username/Library/Scripts/scriptname.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>15</integer>
            <key>Weekday</key>
            <integer>1</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>15</integer>
            <key>Weekday</key>
            <integer>2</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>15</integer>
            <key>Weekday</key>
            <integer>3</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>15</integer>
            <key>Weekday</key>
            <integer>4</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>15</integer>
            <key>Weekday</key>
            <integer>5</integer>
        </dict>
    </array>
</dict>
</plist>

当我从命令行运行脚本并通过launchd(如上)在 18:00 以外的任何时间(例如上面的 12:15)运行它时,它按预期执行。

但是,当我将 launchd 作业更改为在 18:00 运行时,我在注释中看到重复的行 - 所以我的结论是 2 个 launchd 作业同时运行。

我已经搜索了所有可能的目录 [见下文] 以查找虚拟作业 .plist,但没有。

我已经修改了脚本以在运行时将“$0”和日期时间戳写入文件,因此我可以看到它与被调用的脚本相同,并且我可以看到它运行了两次。

所以,最后,我的问题是:如何找到在 18:00 运行脚本的内容?


潜在的launchd.plist 位置:

~/Library/LaunchAgents
/Library/LaunchAgents
/System/Library/LaunchAgents

甚至:

/Library/LaunchDaemons
/System/Library/LaunchDaemons

【问题讨论】:

    标签: bash macos launchd


    【解决方案1】:

    转储pstree -aps $$ 的输出可能会有所帮助。 -a 显示命令行参数,-p 显示进程 ID,-s 显示整个进程树直到根,$$ 获取当前进程的 PID。

    【讨论】:

    • 感谢您的回复。我想我最终通过重新启动Mac解决了这个问题。我的猜测是,launchd 在启动时加载了 [18:00] 作业,但在停用时未能卸载它。无论当时发生了什么,也使那份工作与“新”工作脱钩,所以我失去了对它的控制。
    【解决方案2】:

    这并没有直接回答问题,而是提出了我试图解决的问题存在的原因:

    launchd 在启动时加载了 [18:00] 作业,但在停用时无法卸载它。不管后来发生了什么,也使那份工作与“新”工作脱钩,所以我失去了对它的控制。重新启动清除了“幻影”作业。

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2017-06-09
      • 2012-04-08
      • 2011-09-15
      • 1970-01-01
      • 2017-01-18
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多