How to restart apple finder every two hours automatically by script

psg_casper
New Contributor

Hello all,
can anybody help me with creating a script for my client computers, that restarts the apple finder every two hours automatically?

Why do I need this? We own a Acronis Access Server working with a NetApp Server to connect by afp protocoll. The big problem at this acronis server is, that every mac client, or better: the apple finder, sends millions of inquires to the server the whole day. After working a few hours the server is getting slower and slower. The only solution is to restart the finder, because it´s a bug of apple that is in the finder since Version 10.9.5 and was never fixed. I had several discussions with acronis etc. They also told me to buy Pathfinder and use in instead of apple finder, but it didn´t really works fine for us. Too much problems with adobe programs and so on. The other solution would be to connect by smb, but then we have the problem, that the whole computer is crashing while working with adobe programs.

So it would be very nice if someone of you could help me with this...
Thanks a lot in advance

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

While I personally think what you're doing here is a kludge and is likely to cause problems for your Mac clients down the line (unless you couple this with forcing restarts every few days to help keep them working well), its not my place to really judge this, so I'll stop there other than saying I would encourage you to keep pressure on your vendors, both Apple and NetApp, to come up with a real resolution to the problem.

That out of the way, to make my post useful, you'd want to create a LaunchAgent that you can deploy to all your Macs into /Library/LaunchAgents/ which would look something like this:

<?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>Label</key>
    <string>com.org.restartFinder</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/killall</string>
        <string>Finder</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>7200</integer>
</dict>
</plist>

You'd save the above as something like com.org.restartFinder.plist, move into /Library/LaunchAgents/ and set the POSIX permissions to 644 and root:wheel as the owner:group. Package it up in something like Composer and use it as a deployment.

To load this LaunchAgent automatically after its installed, you will need to add a postinstall script that uses some code to load the agent as the logged in user. This can be added directly to the package install, if saved as a .pkg, or added as an after script in a Casper policy.

#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")

if [[ -e "/Library/LaunchAgents/com.org.restartFinder.plist" ]] && [[ "$loggedInUID" -ne 0 ]]; then
    /bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/bin/launchctl load /Library/LaunchAgents/com.org.restartFinder.plist"
else
    echo "No user logged in, or plist was not found. Exiting."
    exit
fi

Once this is installed and loaded, the Finder should restart every 2 hours, as indicated by the StartInterval section in the LaunchAgent (7200 seconds)

Hope this helps.

View solution in original post

10 REPLIES 10

tthurman
Contributor III
sudo killall Finder

Or...

sudo killall Dock

Just my thoughts.

hjcao
Contributor

Only issue I see with this is what if your user is working on something in finder? Trying to find a file or saving or whatever. That could cause major issues if it falls on the restart.

michael-brodt
New Contributor III

Just out of curiosity, what happens if you connect and browse using Bridge?

psg_casper
New Contributor

to tthurman: I know this, but it didn´t answer my question. I would like to have an automatically running script on every client that restarts the finder.

to hjcao: yes, you´re right, but my users have to use Pathfinder instead of apple finder, so it shouldn´t be a problem. At the moment I send a unix command (killall finder) by ARD to every client manually and nobody ever had any problems

to michael-brodt: It´s the same, because the finder is connected to the server. It doesn´t matter which program you are using. If you are connected to an afp-server, the finder bombs it with inquires. I can see it at the log of acronis access: every client sends about 10 million inquires the day!! It´s no joke, it´s a bug of apple

mm2270
Legendary Contributor III

While I personally think what you're doing here is a kludge and is likely to cause problems for your Mac clients down the line (unless you couple this with forcing restarts every few days to help keep them working well), its not my place to really judge this, so I'll stop there other than saying I would encourage you to keep pressure on your vendors, both Apple and NetApp, to come up with a real resolution to the problem.

That out of the way, to make my post useful, you'd want to create a LaunchAgent that you can deploy to all your Macs into /Library/LaunchAgents/ which would look something like this:

<?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>Label</key>
    <string>com.org.restartFinder</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/killall</string>
        <string>Finder</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>7200</integer>
</dict>
</plist>

You'd save the above as something like com.org.restartFinder.plist, move into /Library/LaunchAgents/ and set the POSIX permissions to 644 and root:wheel as the owner:group. Package it up in something like Composer and use it as a deployment.

To load this LaunchAgent automatically after its installed, you will need to add a postinstall script that uses some code to load the agent as the logged in user. This can be added directly to the package install, if saved as a .pkg, or added as an after script in a Casper policy.

#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")

if [[ -e "/Library/LaunchAgents/com.org.restartFinder.plist" ]] && [[ "$loggedInUID" -ne 0 ]]; then
    /bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/bin/launchctl load /Library/LaunchAgents/com.org.restartFinder.plist"
else
    echo "No user logged in, or plist was not found. Exiting."
    exit
fi

Once this is installed and loaded, the Finder should restart every 2 hours, as indicated by the StartInterval section in the LaunchAgent (7200 seconds)

Hope this helps.

psg_casper
New Contributor

Thanks a lot for that, I will try it out. And sure you´re right, it´s a kludge and maybe it will cause some problems, I know. But I need a fast solution for this problem until the vendors will finally fix it. Maybe never, no one knows.....

But if your or somebody else has a better solution for my problem, please feel free to help. I would be glad not to do such ugly things with my clients...

Look
Valued Contributor III

What are the enquiries they are sending, if they are somehow unique and basically unnecesary could you not do something at a networking / security / firewall level to just throw them away?
If it's just the afp connection itself could you not try and script having it just dump / renew the afp connection (or reset it somehow) periodically.

psg_casper
New Contributor

Thank you mm2270, it works and no one had any problems with it so far....

jarch
New Contributor

Hi psg_casper
I know this could be kind of old thread and my question little out of topic, but did you finally could fix the problem with OS X 10.9 and Acronis?. We are experiencing the same problem you have and we can't find any solution.
Thanks!

psg_casper
New Contributor

Hi jarch,
sorry for my late reply, I saw your question today the first time.
Our solution right now is, that we don´t use Acronis anymore. We decided to upgrade all our clients to an actual version of macOS and use the smb-protocol for serverconnections. It works much better now and we have less problems than before.