How to cleanly kill / close DEVONthink from terminal?

I have a cronjob that shuts down my Mac after certain conditions.

In this script, I kill applications and eject disks and such things.

When sending DEVONthink a SIGTERM (15), it closes - but unclean, as it seems.
At the next start, all databases complain about this.

So, is there a way to cleanly shutdown DEVONthink, including unmounting / closing all databases?

1 Like

You could disable Preferences > Sync > Sync: On Quit or Deactivation and see if that helps.

I would recommend AppleScript:

tell application id "DNtp" to quit
1 Like

That sounds like it could lead to corruption :sweat_smile:
It would be great to sync cleanly, like when regularily closing the App.

I will try this, many thanks!

Okā€¦ as someone who doesnā€™t know a lot about AppleScriptā€¦ what is DNtp ?

I would have assumed:

tell application "DEVONthink 3" to quit

or even

quit application "DEVONthink 3" 

That seems to work like a charm, in my initial test.

Problem is, when DT is not running, it will instead be started by this command, which is a bit irritating.

I am wondering anyways how AppleScript is doing this ā€¦ I still think it could finally be just a signal that getā€™s send to the App (but not 15).

Out of curiousity, I tried all signals on DT:

1 to 8 and 10 to 15 killed the App, but unclean.
16 to 23 did nothing.
24 to 27 killed again, also unclean
28 and 29 did nothing
30 and 31 kiiled , also unclean

Only the Applescript method seems to work.

I made a little shells function that works like a charm, also for other applications:

tell_to_quit() {
        local app=$1

        cat <<EOF | osascript
set appName to "${app}"

if application appName is running then
    tell application id (id of application appName) to quit
else
    return "Not running"
end if

EOF

        return 0
}

This way, you also donā€™t need to know the Application ID.

P.S. It does not work with Cryptomator, as it only triggers a window that asks if the Vault should be locked. But SIGTERM works.

1 Like

You can simplify that script greatly if you want

One-Liner

Reformatted for posting, but you can make this all one line if you remove the \ at the end of the first line.

APP="DEVONthink 3" && pgrep -xq "$APP" && \
osascript -e "tell application \"$APP\" to quit"

The only ā€˜downsideā€™ is that it will not tell you if the app is not running, but it also wonā€™t launch it just to quit it either.

More Verboser Version

If you do want the script to tell you when the app is not running:

APP="DEVONthink 3"

pgrep -xq "APP"

if [[ "$?" == "0" ]]
then
	osascript -e "tell application \"$APP\" to quit"
else
	echo "$APP is not running."
fi
1 Like

This is from page 190 of the user guide (also available through the in-app Help):

Stephen

2 Likes

I think that this does not work. Did you test your code?

Iā€™m quite happy with my generic function as it is.

1 Like

As is suspected, this does not work:

root@mini:/private/var/root# osascript -e 'tell application "DEVONthink 3" to quit'
35:39: execution error: DEVONthink 3 got an error: Application isnā€™t running. (-600)

Just use my version, which will work for any application.

Yes, I tested it, and yes, it works.

If you had actually used the code that I had provided, it would have worked fine.

Instead, you just used half of it and complained that it didnā€™t work.

Since you apparently didnā€™t understand what I wrote, I will explain it piece by piece.


APP="DEVONthink 3"

Step one assigns a variable to the name of the app. This is only necessary to avoid re-typing the name of the app. It could also be replaced with APP="$1" if you wanted to generalize the script to work with any other app that respects AppleScript.


&&

tells it to continue only if the previous command is successful.


pgrep -xq "$APP"

Step two checks to see if $APP (aka ā€œDEVONthink 3ā€) is running.


&&

tells it to continue only if the previous command is successful. If pgrep does not return an exit code of zero, it will stop here.


osascript -e "tell application \"$APP\" to quit"

Step three tells $APP to quit, using AppleScript.


Also, if you are trying to run AppleScript as root but are not logged in and running DEVONthink 3 as root then it will not work. The shell command and AppleScript have to be run by the same user that is running DEVONthink 3.

You can watch a screencast of this code working. The first time it is run, it quits DEVONthink 3. The second time, it does nothing, because it is not running.

(If the embed does not work, you can find it at https://vimeo.com/613682526:

Interesting. Thank you! I have learned something new today.

1 Like

Not sure what you are trying to sell me here :sweat_smile:
The App was running, when I tested this - and the pgrep would have worked and your code would have failed likewise.

The problem is, that ā€œDEVONthink 3ā€ is just the wrong string, eager person.
Try ā€œDEVONthinkā€ and it works.

And with my function without pgrep and for any and all applications.
That pgrep is error-prone anyways, at it may be triggered by some of the helper processes already.

I literally demonstrated with a video that it works as shown.

Nope. You are wrong. Watch the video. It shows that it works as is.

Do you need another video demonstrating that my code, which works with DEVONthink 3 does not work if you just use DEVONthink? Did you test that? Because I did. And it doesnā€™t work.

It might help if you understood how pgrep -x works.

It also uses AppleScript when no AppleScript is necessary, whereas mine will only use it if needed.

But to each their own.

Yes :sweat_smile:

P.S. I understand pgrep very well.
But you do not seem to understand, that I need a functions that can close any and all applications.
And your code may very well find binaries that have this string as (probably part of) the executable name, but are not the app you are searching for.
Look over the border a bit more :hugs::wink:

:wave:

Out of curiosity, I tested what you wrote about root being the wrong process owner, and you are right!
As the regular user who also started DT, your syntax works, even with ā€œDEVONthink 3ā€ ā€¦

But I cannot use it, as the cron script that kills lots of processes and ejects disks and disk images also shutā€™s down the Mac ā€¦ this requires a root script, of course.
Therefor, all my tests were done as root.