LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2013, 10:00 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Rep: Reputation: 46
Best folder to put scripts to be run by cron as various users


Hi everyone. I've read multiple things in online articles so figured i'd post my question here for clarification.

I'm running RHEL, but where is the "proper" place to store user created scripts, that will be executed by cron by various user accounts on the server?

In my googling, it appears the most common places to dump your scripts into are /usr/local/bin or /usr/local/scripts (but it appears a scripts folder isn't created by default on a new install I did).

Some articles also mentioning to check the paths for cron, but I'm not sure how to do that either
 
Old 03-13-2013, 10:01 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It doesn't matter. It's entirely up to you since you'll be hard coding the location in the cron tab anyway.
 
2 members found this post helpful.
Old 03-13-2013, 10:06 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Not necessory to put scripts in /usr/local/bin or /usr/local/scripts directories. You can create a simple direcotry in your home dir. and put all your scripts in that.
Code:
~$ mkdir /home/username/myscripts
~$ mv script1 script2... /home/username/myscripts
Since your concern is to run those scripts using cron, so it doesn't matter where they are. Just make crontab entries properly.

On the other hand, if you want to run those scripts without using their absolute path, simply add parant directory of those scripts in your PATH variable, as:
Code:
~$ export PATH=$PATH:/home/username/myscripts
 
1 members found this post helpful.
Old 03-13-2013, 10:13 AM   #4
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
Thanks for the quick replies guys.

but how do I see what paths are current in my PATH now for cron? I'd still put the whole path in for the scripts into the crontab, but just curious since that would save me some typing if /usr/local was already in there, then I could put them in bin or create scripts to put them in, and I think I could omit the full path (if I wanted to in theory)
 
Old 03-13-2013, 10:29 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
^ its probably a good idea to always use the full path when running something in cron since cron doesnt always create the same environment variables as a normal user.

you can try scheduling something like /bin/echo "cron path = $PATH" > /path/to/some/file to see if it prints its $PATH variable.
 
1 members found this post helpful.
Old 03-13-2013, 10:36 AM   #6
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
ok, I will try that. I was going to put the full paths in anyway, but was just curious how to see what is actually in PATH anyway
 
Old 03-13-2013, 10:47 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by rjo98 View Post
ok, I will try that. I was going to put the full paths in anyway, but was just curious how to see what is actually in PATH anyway
If cron entries are your only concern, then just do it like:
Code:
~$ crontab -e
x x x x x /home/username/myscripts/scriptname.sh
Where x is any value in crontab, at which you want to schedule your script.
 
1 members found this post helpful.
Old 03-13-2013, 11:03 AM   #8
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
Yeah, that's what I planned on doing, was more just curious about what PATH was set to.
 
Old 03-13-2013, 11:10 AM   #9
nonamedotc
Member
 
Registered: Mar 2011
Location: /
Distribution: Fedora (typically latest release or development release)
Posts: 370

Rep: Reputation: Disabled
This is what I get.

Code:
echo "cron path=$PATH" > pth && cat pth
cron path=/usr/libexec/lightdm:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin
As for the scripts location, I just put them in a specially designated folder ~/personal/scripts. If multiple users need it, I use /usr/local/bin.

I have also used /opt/scripts in the past. But I do not know if there are reasons to not to do this.
 
Old 03-13-2013, 12:35 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by nonamedotc View Post
This is what I get.

Code:
echo "cron path=$PATH" > pth && cat pth
cron path=/usr/libexec/lightdm:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin
As for the scripts location, I just put them in a specially designated folder ~/personal/scripts. If multiple users need it, I use /usr/local/bin.

I have also used /opt/scripts in the past. But I do not know if there are reasons to not to do this.
As the post that suggested this indicated, that command must be run from WITHIN your cron. Running it on the command line does nothing but echo your current PATH, which is completely different than cron's PATH.

When done correctly, you should get something like:
Code:
cron path = /usr/bin:/bin
That's what it looks like on my system. This is why the location of nearly every command in your script must be hard coded, because only those commands that are located in /usr/bin or /bin will be found by cron.

Last edited by suicidaleggroll; 03-13-2013 at 12:36 PM.
 
Old 03-13-2013, 05:38 PM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
an other option is to add your PATH= to the start of your crontab.

Code:
[ray@centos ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ray/bin
then at the head of your crontab make it look like this:
Code:
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ray/bin
* * * * * /path/to/foo.sh
if the script resides in the above PATH you can just execute it:
Code:
* * * * * foo.sh
but ONLY if you put the path in the crontab.

for me if it is a universal script (something that anyone on the system should be able to run, then Ill just place the script in /usr/bin and set visudo permissions for that specific script. the user can just execute foo.sh or what ever its called without the full path.
 
Old 03-14-2013, 12:56 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
If its definitely personal scripts, then keep them in the user's dirs.
If its a generic script that affects others, then probably /usr/local/bin or /opt/scripts.
If a cron job for a group/project, use the projects bin dir if it has one.
 
1 members found this post helpful.
Old 03-20-2013, 03:32 PM   #13
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
Thanks to everyone for your replies.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script run fine from ./ but when put in cron does not run j-me Linux - Server 6 06-16-2012 11:02 AM
What time do the scripts in /etc/cron.daily run? abefroman Linux - Server 3 01-06-2010 04:01 PM
Problem with cron in another users home folder tanveer Linux - General 3 04-08-2009 12:50 PM
Where to put Bash scripts written to run at various times to examine Alsa sound probs xologist Linux - Software 3 08-13-2008 11:09 AM
can echo be used for popups in shell scripts run by cron? dr_zayus69 Linux - Software 4 04-12-2005 07:30 PM


All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration