Re: newbie quesiton:how to open file in crob job?
by Happy-the-monk (Canon) on Mar 29, 2004 at 02:16 UTC
|
You did it right to express "current directory".
However, cron's current directory doesn't seem to be the one you had in mind: I guess cron will use the user's home.
You might want to chdir to the directory or use the full path of the file in open.
| [reply] |
|
|
| [reply] |
|
|
I thought cron will run jobs for root only.
If it is set up in such a way that it only runs for root, yes. That depends on how much root the sysadmin will trust you, I guess =)
Sören
| [reply] |
Re: newbie quesiton:how to open file in crob job?
by pbeckingham (Parson) on Mar 29, 2004 at 02:16 UTC
|
Your assumption of what is the "current directory" is wrong. Use an absolute path if possible, rather than a relative one.
| [reply] |
Re: newbie quesiton:how to open file in crob job?
by ambrus (Abbot) on Mar 29, 2004 at 09:03 UTC
|
Try to use at instead of cron. At is smart enough to save a lot of the caller process's attributes.
It will invoke the process as the same userid, from the same working directory, and even environment, umask. (Why isn't nice saved, by the way?)
| [reply] [d/l] |
Re: newbie quesiton:how to open file in crob job?
by iwanthome (Beadle) on Mar 29, 2004 at 02:48 UTC
|
Can I get the path infomation about current perl script ?
If not , I can't know which is the full path of the config file because I can't force other guys install the perl script on the desired directory.
thanks! | [reply] |
|
|
Check out "perldoc Cwd" -- the Cwd module is part of the core distribution (everyone who has perl5 has this module):
use Cwd;
# ...
my $cwd = getcwd();
# ...
Also, if you are confident that the config file is always installed in the same directory as the perl script, then you can check the vale of $0 (that's "dollar-sign, digit-zero"):
use File::Basename; # another core module
# ...
my $script_path = ( fileparse( $0 ))[1]; # look for config file in $s
+cript_path
# (update: as peckingham points out, "dirname()" is easier)
| [reply] [d/l] [select] |
|
|
use File::Basename;
my $cwd = dirname $0;
| [reply] [d/l] |
|
|
If you can't force them to specify where they are installing it, it is still fair game to have a config file where they have to list where they installed the app. And if you ship an install.pl or an install.sh script, or package an RPM, it's fair game for you to specify where your stuff gets installed. Sounds like you need to exert a little more control over your users. And then, even then, use absolute pathing versus a relative solution. Relative pathing has it's place, but avoid it when you can.
| [reply] |
Re: newbie quesiton:how to open file in crob job?
by thetimeboy (Novice) on Mar 29, 2004 at 13:50 UTC
|
The current directory ./ dotSlash is a handy shortcut that isn't guaranteed to refer the same directory everytime you run the script. If you try changing directory on the command line and then calling the script you would find the same problem.
You either have to tell the script the absolute path of the file to open (thats the clearest thing to do), guess the directory that cron considers its current working directory when executing perl scripts (you can discover this if you are interested by making your script write the results of `pwd` to an absolute filename for you to check later), or get your script to manually force itself to run from the place you want to represent with ./ by the command:
chdir ("/absolute/Path/Name/forYourDir"); # . is set
In short you must tell the script what . means, unless you only ever want to run it yourself from somewhere you will always be in when you do, or you know what the script will think it means and thats fine with you. | [reply] |
Re: newbie quesiton:how to open file in crob job?
by tbone1 (Monsignor) on Mar 29, 2004 at 13:36 UTC
|
One thing to remember is that your terminal session is done as a user who has logged in. Thus, your environment variables, default directory, etc, are set by /etc/profile, your own .profile, and so on. (If you use Korn shell; YMMV depending on your default shell.) crontab does not initialize this way, so if you need to use those things, you must explicitly run them.
This bites more than a few people, particularly those who are new to crontab.
--
tbone1, YAPS (Yet Another Perl Schlub)
And remember, if he succeeds, so what.
- Chick McGee
| [reply] |
Re: newbie quesiton:how to open file in crob job?
by CountZero (Bishop) on Mar 29, 2004 at 13:27 UTC
|
I'm not a *nix person, but could it be that cron runs with different permissions as a user?
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] [d/l] |
Re: newbie quesiton:how to open file in crob job?
by Plankton (Vicar) on Mar 29, 2004 at 15:53 UTC
|
There are a lot of ways to deal with this. You could do something like this in you crontab ...
* 12 * * * cd $HOME/nooner ; nooner.pl
| Plankton: 1% Evil, 99% Hot Gas. |
| [reply] [d/l] |
Re: newbie quesiton:how to open file in crob job?
by Vautrin (Hermit) on Mar 29, 2004 at 19:50 UTC
|
If you want a cross platform way of dealing with paths, check out File::Spec. For instance, if you wanted to change the specific directory ./foobar to an absolute, non platform specific (*nix) directory you could use the following:
use strict;
use warnings;
use File::Spec;
# print a path that is system independent
print "The dir is ", File::Spec->rel2abs("./foobar"), "\n";
You can do all sorts of cool things with File::Spec involving directories, allowing you to use Unix style names on Windoze and Visa versa. The only caveat is to use $ENV{HOME} for the home directory -- ~/ is only the home directory for some shells, so it is not properly translated.
Want to support the EFF and FSF by buying cool stuff? Click here.
| [reply] [d/l] [select] |