Search code examples
bashmacoslaunchd

Is it possible to determine what called a bash shell script?


Is there any way to determine how a shell script was called?

I have a bash script that prepends a single line in a note in the Bear app on macOS. It composes an x-callback url and opens it.

These are the final lines of most relevance (everything leading up to this point is getting and setting the variable values):

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

#x-callback
open "$theURL"

The script is run once each weekday via launchd. Here is the contents of the .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>

When I run the script from the command line and when I run it via launchd (as above) for any time other than 18:00 (e.g. 12:15 in the above), it performs as expected.

However, when I change my launchd job to run at 18:00, I get duplicate lines in the note - so my conclusion is that 2 launchd jobs are running at the same time.

I have scoured all the potential directories [see below] for a phantom job .plist, but there is none.

I have modified the script to write out "$0" and a date time stamp to a file when it runs, so I can see it's the same script that's being called and I can see that it's running twice.

So, finally, my question: how can I find what's running the script at 18:00?


Potential launchd .plist locations:

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

and even:

/Library/LaunchDaemons
/System/Library/LaunchDaemons

Solution

  • This doesn't directly answer the question, but proposes a reason the problem that I was trying to solve existed:

    launchd loaded the [18:00] job on boot but failed to unload it when it was deactivated. Whatever happened then also decoupled that job from the 'new' one so I lost control of it. Rebooting cleared out the 'phantom' job.