LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Best folder to put scripts to be run by cron as various users (https://www.linuxquestions.org/questions/linux-newbie-8/best-folder-to-put-scripts-to-be-run-by-cron-as-various-users-4175453907/)

anon091 03-13-2013 09:00 AM

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 :(

suicidaleggroll 03-13-2013 09:01 AM

It doesn't matter. It's entirely up to you since you'll be hard coding the location in the cron tab anyway.

shivaa 03-13-2013 09:06 AM

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

anon091 03-13-2013 09:13 AM

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)

schneidz 03-13-2013 09:29 AM

^ 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.

anon091 03-13-2013 09:36 AM

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

shivaa 03-13-2013 09:47 AM

Quote:

Originally Posted by rjo98 (Post 4910773)
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.

anon091 03-13-2013 10:03 AM

Yeah, that's what I planned on doing, was more just curious about what PATH was set to.

Janus_Hyperion 03-13-2013 10:10 AM

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.

suicidaleggroll 03-13-2013 11:35 AM

Quote:

Originally Posted by nonamedotc (Post 4910802)
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.

lleb 03-13-2013 04:38 PM

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.

chrism01 03-13-2013 11:56 PM

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.

anon091 03-20-2013 02:32 PM

Thanks to everyone for your replies.


All times are GMT -5. The time now is 04:14 PM.