Where to store cronjob script?

18

3

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

Posted 2013-01-13T23:44:28.717

Reputation: 1 151

you might find some by default on /usr/sbin – tony gil – 2014-11-04T19:04:31.757

1Why 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 – 2013-01-13T23:48:27.760

1There is really no "answer" to this questions....just many many preferences. – mdpc – 2013-01-14T01:27:28.553

Answers

19

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

Posted 2013-01-13T23:44:28.717

Reputation: 10 321

At least in my installation (ubuntu 14.04), programs in /usr/local/bin are not available to cron jobs. – juacala – 2018-02-03T16:38:50.127

@juacala you'll need to give an absolute path, as cron's environment $PATH isnt the same as your user's. – Sirex – 2018-02-06T19:47:26.763

"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 – 2013-01-13T23:49:20.460

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 – 2013-01-13T23:53:03.083

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:<your group> @ 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 – 2013-01-13T23:54:53.933

6@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 – 2013-01-14T00:13:06.023

if this is a personal script (noone else needs run it) terdon is correct. – Sirex – 2013-01-14T00:15:48.487