odd recursion: calling "by hand" vs calling by cronscript...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
odd recursion: calling "by hand" vs calling by cronscript...
I have a cronscript which checks (then fixes) users directories for badly named files - meaning I don't want to allow spaces or ampersands in file or directory names & it will replace spaces with underscores and removes ampersands.
If I have a directory uploaded by a user like this:
then I call this script by typing it's name (I call it sr) in a bash shell like this:
cd /var/pub/ftp
sr
then I end up with:
/var/pub/ftp/username/top_level/second_level/third_level/ /var/pub/ftp/username/top_level/second_level/third_level/pork_beans
the program works by parsing directories then recursively calling itself to descend further until it has fix all files and directories below the directory it has been called.
I set it to run in a cronscript, and get different results! Here is what I get when it is called by cron:
/var/pub/ftp/username/top_level/second_level/third level/
/var/pub/ftp/username/top_level/second_level/third level/pork & beans
so, it doesn't successfully call itself when started by cron.
Here is the script for sr (called that originally for space replace)
and if you can give me a hint why this might be happening, I would be very grateful.
I just don't get why the recursion works when typed into bash, but not when called by cron
Running FC2, btw.
The cronjob belongs to root.
----
for i in *
do
if [ -d "$i" ] # if * is a directory
then
cd "$i" # descend into the directory
for y in *
do
tempa=$(echo $y | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
wait
if [ "$y" != "$tempa" ]
then
mv "$y" "$tempa"
wait
fi
if [ -d "$tempa" ] # if this is also a directory, call this program
then
cd "$tempa"
sr # this is the name of THIS program, must be in your PATH
wait
cd ..
fi
done
cd ..
fi
tempa=$(echo $i | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
wait
if [ "$i" != "$tempa" ]
then
mv "$i" "$tempa"
wait
fi
done
"I just don't get why the recursion works when typed into bash, but not when called by cron"
Cron jobs run with a skimpy PATH. You might have the case where sr is on the bash PATH but not on the cron PATH. If that is the problem then you can fix it either by setting the PATH in sr or by invoking sr using the full path name.
Also I do not think that your script includes the logic for the case where sr changes a file name to an already existing file name.
"Is there an easy way to import the bash PATH to the cron PATH at the beginning of a cron script?"
There is a place in the crontab entry where you name the command you wish to run. You can make this entry a series of commands separated by semi colons ;. Set the PATH and then invoke sr.
If you mean can you use the same PATH as set by root bash the answer is "I don't know". Cron runs as its own daemon. It is not the user root although cron runs as ID 0. Finding out what PATH the user root uses and setting that PATH in the cron daemon is probably more trouble than it is worth. Anyway, I don't know how to do it off the top of my head.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.