1

I have some shell code that I'd like to execute daily using cron. Currently, I have the entire code written as a one-liner, and the 6 variations (each designed to run at different times) are all written in the crontab. While this works, it's quite difficult to read, and could be made more efficient by putting the code in a shell script, and running the script with cron.

However, I need to put the script somewhere. I cannot find anything in the man pages or online specifying the 'proper' location for cron jobs. Is there a directory in /etc for cron jobs? Or should I use root permissions to put my script in /usr/lib/cron/jobs/? Should I put the script in ~/.bin/ and use the full path in my crontab?

When I ask where I 'should' put it, I am looking for the location that is designated by the implementation of cron, or, if the doesn't exist, the location that makes the most sense and is most secure.

3
  • 1
    What permissions does the script need, just to run as your user or to run as another user?
    – mmmmmm
    Jul 19 at 16:38
  • 2
    RE: "I cannot find anything in the man pages or online specifying the 'proper' location for cron jobs." -- If you read the manual page for cron, it states: "The cron utility is launched by launchd(8) when it sees the existence of /etc/crontab or files in /usr/lib/cron/tabs." and at the bottom is states: "/usr/lib/cron/tabs Directory for personal crontab files". As far as shell scripts, see the answer from nohillside. Jul 19 at 16:55
  • @mmmmmm No special permissions; just to run as me. Jul 19 at 16:59
2

Both /etc and /usr are part of macOS and get mostly overwritten with each OS upgrade (with the exception of /usr/local). So you don‘t want to store your own content there.

The canonical place for user-provided code is /usr/local/bin, at least if you don‘t mind other users reading the shell script or running it. Otherwise (or to make it easier to update it) ~/.bin or similar is a good place.

3
  • Thank you. I forgot that updates reset those directories (I have to set up my PAM configuration every update to allow sudo with Touch ID), so I'll use .bin Jul 19 at 17:00
  • 1
    ~/.bin is more appropriate for a user cron jobs. Otherwise, use the first suggestion from @nohillside.
    – dan
    Jul 19 at 17:08
  • 2
    The bin directories are really intended for executables that are available for general use from the command line or scripts; for executables that're only intended to be run from one or a few things (e.g. cron jobs), libexec directories (in this case, /usr/local/libexec) is a better choice. Jul 19 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .