MacOS – Use script without root

bashmacosscriptterminal

I write a maintenance script with different commands.
the script has sudo commands inside.

Now I want to use crontab, but my first problem is:
How can I use the script on user level without root privilegs?
Is that possible?

After that I want to copy the script on /usr/local/maintenance and want to use crontab to run the script every first thursday f.e.

EDIT:
ok, I decide it now for launchD. Now I have a plist in Global Agents, it works fine. But only the user files.
How can I use launchD for sudo commands?

Best Answer

First, the best way to run a cron script as root is to add it to the root user's crontab. EDITOR="vi" ; sudo crontab -u root -e will allow you to edit the root crontab. Once you are doing that you don't need to use `sudo; inside your script.

As for running it every Thursday, check the man page for the crontab table man crontab -s 5 and it will explain all you need to know.