Using sudo command in shell script in Automator app

applescriptautomatorterminal

I'm planning to make a small Automator app to make these commands into something I can run regularly:

rm -rf ~/Library/Caches/*
rm -rf ~/Library/Saved\ Application\ State/*
sudo rm -rf /Library/Caches/*
sudo rm -rf /System/Library/Caches/*
atsutil databases -removeUser
sudo atsutil databases -remove
sudo atsutil server -shutdown
sudo atsutil server -ping
sudo rm -rf /var/folders/*

Since it involves sudo, which I understand is a dangerous command, is this recommended for someone new to Automator, and it won't damage the Mac?

I tried the command suggested at iMac not shutting down since upgrading to OS X 10.11, El Capitan to fix problems with my Mac shutting down (it's MacOS Sierra 10.12.6 on an old Mac Mini).

I would welcome any advice on this, as I know the basics of Automator, just whether it's recommended to do so with sudo commands as an Automator app.

Best Answer

Sudo is not (in and of itself) dangerous. Sudo merely removes protective restrictions, putting the burden of running safe code on you, rather than protecting you behind the scenes. Sudo can be dangerous when, for instance:

  • You make a coding mistake which has unintended consequences: e.g., if you intend to run:

    • sudo rm -Rf /Users/yourname/something/something/

      but instead you type:

    • sudo rm -Rf /Users/yourname/ something/something/

      (with an accidental space after 'yourname')

    the second script (with the erroneous space) will delete all of the data for user 'yourname' without warning.

  • You run someone else's code which happens to be malicious. Malicious code run without sudo can do some damage, but malicious code run with sudo can compromise your system entirely.

As long as you're careful, sudo is safe enough. Just be aware of the potentials for harm.