29

I'd prefer to not keep it in my /home/myusername directory. Is there a best practice of where to store your shell scripts for cronjobs? At the root level?

I'm running ubuntu server 12.04 right now.

tarabyte
  • 2,133
  • 7
  • 37
  • 42
  • 1
    Why don't you want to store it in your home directory? If they're for you personally, that's exactly where they should be -- probably in some well-named subdirectory. If they're for the system has a whole, perhaps they should be somewhere else. (The distinction may be vague if you're the only user.) – Keith Thompson Jan 13 '13 at 23:48
  • 1
    There is really no "answer" to this questions....just many many preferences. – mdpc Jan 14 '13 at 01:27
  • you might find some by default on `/usr/sbin` – tony gil Nov 04 '14 at 19:04

2 Answers2

27

Typically, I put mine in:

  • /usr/local/bin/ for scripts to be run by more than one normal user
  • /usr/local/sbin/ for scripts which are to be run as root

That way you separate the task that the script does, from cron which just automates launching it.

You'll need root to store files there, though.

Sirex
  • 10,990
  • 6
  • 43
  • 57
  • "You'll need root to store files there, though." -- Not if you set the permissions right. I usually make `/usr/local` and its subdirectories group-writable, and add myself to the group (say, `install`). (Of course you'll need root to set the permissions in the first place.) – Keith Thompson Jan 13 '13 at 23:49
  • i'm the main user and have root privileges. i'd prefer to have no one else edit this so am leaning toward /usr/local. there's no standard /mycronjobs directory though that people usually create? – tarabyte Jan 13 '13 at 23:53
  • 1
    if you want noone else to be able to edit it. make it owned by root and set 500 or 700 permissions on it. - you could even do root: @ 550 or 770. for similar effect with you being able to edit without going root. imho it's cleaner to just sudo before editing the script though and have it owned by root. – Sirex Jan 13 '13 at 23:54
  • 8
    @terabyte, the standard directory is usually `~/bin` which is then added to the $PATH. You asked for best practice, well it is saving your personal scripts in your home directory. It is neater and it makes it much easier to upgrade later. – terdon Jan 14 '13 at 00:13
  • if this is a personal script (noone else needs run it) terdon is correct. – Sirex Jan 14 '13 at 00:15
  • At least in my installation (ubuntu 14.04), programs in /usr/local/bin are not available to cron jobs. – juacala Feb 03 '18 at 16:38
  • 1
    @juacala you'll need to give an absolute path, as cron's environment $PATH isnt the same as your user's. – Sirex Feb 06 '18 at 19:47
1

I follow Luke Smith's organization and put my cronjobs on ~/.local/bin/cron/. It seems sensible as ~/.local/bin stores all binaries and UNIX shell scrips of the local user.

Additionally, as @Sirex has suggested, you can use /usr/local/bin/ to store cronjobs globally, that is, for all users, and /usr/local/sbin/ for scripts which are to be run as root. Personally, for sake of organization, I would put them under the directory cron, to separate them from ordinary shell scrips or binaries.

Rubem Pacelli
  • 159
  • 1
  • 2