Under unix system ..
I want to run a perl script A.pl in every Saturday. I tried the code below, it works. But I am wondering does there exists any other code can work but does not keep checking the time in a busy loop?
while(1)
{
#Time = localtime();
if( $Time[6] eq "6" )
{
`perl A.pl`;
}
}
Under Unix, running programs periodically is the job of cron. Users can edit their jobs into the crontab using the crontab -e command (set your EDITOR environment variable to your favorite editor).
The format of the crontab is described in crontab(5) which you read with
man 5 crontab
The most common gotcha for new cron users is that cron jobs are run with a minimal environment; not even PATH is exported so you have to set PATH in your program.
So where does your program's output go? Any output on stdout or stderr will be mailed to your account. If you want it somewhere else, your crontab line must redirect the output, e.g. with a crontab entry like this (note: sh syntax, not csh syntax!):
# Quarter past 8 on Saturdays.
15 20 * * 6 /home/user/bin/A.pl >/home/user/A.stdout 2>/home/user/A.stderr
Put it in crontab as follows:
0 0 * * 6 /path/to/perl A.pl
This is, of course, assuming you're on Unix. I have no clue about Windows.
If you are using a unix/linux system you can put your A.pl script in the crontab.
Related
I am able to execute scrip from command line.
I'm executing it like this:
/path/to/script run
But while executing it from cron like below, the page is not comming:
55 11 * * 2-6 /path/to/script.pl run >> /tmp/script.log 2>&1
The line which is getting a webpage uses LWP::Simple:
my $site = get("http://sever.com/page") ;
I'm not modyfing anything. The page is valid and accessible.
I'm getting enpty page only when I execute this script from crontab. I am able to execute itfrom command line!
Crontab is owned by root. And job is executed as root.
Thanks in advance for any clue!
It's difficult to say what might be causing this, but there are differences between your environment, and the environment created by crontab.
You could try running it through a shell with appropriate args to construct your user environment:
55 11 * * 2-6 /bin/tcsh -l /path/to/script.pl run >> /tmp/script.log 2>&1
I'm assuming you are running it by cron with your own user ID of course. If you aren't, then obviously you should try running it manually with the user ID that cron is using to run it.
If it's not a difference in environment variables (e.g. those that specify a proxy to use), I believe you are running afoul of SElinux. Among other things, it prevents background applications (e.g. cron jobs) from accessing the internet unless you explicitly allow them to do so. I don't know how to do so, but you should be able to find out quite easily.
my $systemDateState = system "date";
my $systemXXXXState = system "xxxx";#xxxx is a program
print "$systemDateState\n";
print "$systemXXXXState\n";
This perl script prints two zero. It works from the command line. But when executing this script in cron job, it returns one zero and one -1.
-1 means xxxx never executed. I trid to execute xxxx directly in cron job. xxxx executed normal. And perl script system date in cron job works. Why perl script system xxxx in cron job never executed?
The case here is because the lines end. It's set Path & xxxx in a.sh and set Path & perl script.pl in b.sh. a.sh in the cron job is working, b.sh is not. The difference is a.sh in Unix-format, b.sh in Win-format. I change the format of b.sh, it is working too. -_-||
try to edit your cronjob to force the right directory. For Example:
cd cronjobs && perl cronjob.pl
my perl script is not executing in crontab,
I dont know how to confirm this but Im not seeing the script result or output.
But in the cron log I see a entry like
Jul 28 12:35:01 dvsbi-build crond[13469]: (root) CMD (PATH=/usr/local/bin:/usr/sbin/usr/lib perl /dm2/www/html/isos/preFCS5.3/autodownload.pl)
And I set the cron job as
35 12 * * 2-6 PATH=/usr/local/bin:/usr/sbin/usr/lib perl /dm2/www/html/isos/preFCS5.3/autodownload.pl
Im not able to see the perl script result. In script I have redirected all the debug statements to a LOG file and I dont see the log file update.
the perl script run fine manually from anywhere in terminal by giving path like
perl /dm2/www/html....../autoDownload.pl
Also I have given full path to the log files inside the script.
You should use the full path to perl in cron. Some implementations of cron restrict usage of environment variables.
Try to find out where perl is located on your system with which perl like sergio commented.
Given perl is in /usr/bin try the following:
35 12 * * 2-6 /usr/bin/perl /dm2/www/html/isos/preFCS5.3/autodownload.pl
I have a script that I'm trying to run from cron. When I run it from bash, it work just fine. However when I let cron do it's thing, I get a:
myscript.sh: line 122: syntax error: unexpected end of file
What I want is a way to run a command as if it was a cron job, but do it in my shell.
As a side note: does anyone know what would be differnt under cron? (the script already has a #!/bin/sh line)
To answer my own question: I added this to my crontab:
* * * * * bcs for ((i=$(date +\%M); i==$(date +\%M) ;)) ; do find ~/.crontemp/ -name '*.run' -exec "{}" ";" ; sleep 1; done`
and created this script:
#!/bin/sh
tmp=$(mktemp ~/.crontemp/cron.XXXXX)
mknod $tmp.pipe p
mv $tmp $tmp.pre
echo $* '>' $tmp.pipe '1>&2' >> $tmp.pre
echo rm $tmp.run >> $tmp.pre
chmod 700 $tmp.pre
mv $tmp.pre $tmp.run
cat $tmp.pipe
rm $tmp.pipe
With that, I can run an arbitrary command with a delay of not more than one second.
(And yes, I know there are all kinds of security issue involved in that)
the problem was a fi vs. if problem. Doh!
When a script works interactively and fails in cron it's almost always a PATH problem. The default PATH in a cron job process is much much shorter than in an interactive session. The typical result is a "not found" error for some system utility you're trying to run that is not on the PATH in cron.
I would guess that some command you're trying to run is not on the path, therefore the file it was supposed to create is empty and the command that's trying to read that file is giving you this error message.
You may have a "%" in your crontab.
You must escape it (with "\") or it is changed in a newline character.
There are a number of things it could be - output will be redirected elsewhere; environment variables will almost certainly be different, etc. On the information you've given, it could be related to a difference between bash and /bin/sh (which on some systems - including Debian/Ubuntu flavors of Linux - are different, and support slightly different syntax). Cron will usually run the command you give to it using /bin/sh.
Try running:
/bin/sh -c '<command>'
where <command> comes from your crontab. (Of course, if that command uses '' quotes you will need to modify it accordingly...)
I have a bunch of Perl scripts that all run fine, yet need to have use Plibdata; up top.
I set up a cron job that runs (I get the confirmation email from root) and it spits back the following error message:
Can't locate Plibdata.pm in #INC (#INC contains: /install/lib /opt/perl58/lib/5.8.8/IA64.ARCHREV_0-thread-multi /opt/perl58/lib/5.8.8 /opt/perl58/lib/site_perl/5.8.8/IA64.ARCHREV_0-thread-multi /opt/perl58/lib/site_perl/5.8.8 /opt/perl58/lib/site_perl .) at ./x line 5.
BEGIN failed--compilation aborted at ./x line 5.
Line 5 is... you guessed it.... use Plibdata;
I am also attempting to set the environment as such:
use lib "$ENV{CARSPATH}/install/lib";
so maybe if I found the location of this plibdata, I could explicitly direct it that way?
My cron commands will be executed using /usr/bin/sh says crontabs...
Any suggestions?
This script works from the command line.
You don't say what Plibdata is. You also don't state if this works at your command prompt. I assume that it does.
Try this:
perl -MPlibdata -e 1
Assuming that doesn't spit the same error, try this:
perl -MPlibdata -le 'print $INC{"Plibdata.pm"}'
That will tell you where. (It's probably in your PERL5LIB env var if this works.) Then you can just add the appropriate "use lib" to the directory Plibdata.pm is in.
Also, be sure you're using the same perl in both locations - command line ("which perl") and in the cron job (try "BEGIN { print $^X }" at the top of your script).
Cron uses a different user env than your env when logged in. Are you able to run the script from the command line? If so, just set your env variables inside the cron above your current commands.
Clearly, Plibdata.pm is not installed in the default module paths on your system:
/install/lib /opt/perl58/lib/5.8.8/IA64.ARCHREV_0-thread-multi /opt/perl58/lib/5.8.8 /opt/perl58/lib/site_perl/5.8.8/IA64.ARCHREV_0-thread-multi /opt/perl58/lib/site_perl/5.8.8 /opt/perl58/lib/site_perl
You have three choices:
Install Plibdata.pm in a known Perl system path (site_perl is the classic option).
Make the PERL5LIB shell environment (or the equivalent command line -I option for Perl) include the installation path of the module.
Use use lib in your script. Remember that the use lib action is done at compile time, so your variable in the path may not be initialised. Try using the variable in a BEGIN block like this:
my $env;
BEGIN {
$env = $ENV{CARSPATH};
}
use lib "$env/install/lib";
Running your program from a wrapper script as others have suggested is probably my preferred method, but there may be a few other solutions:
If you're using a modern cron you may be able to do something like this in your crontab entry:
* * * * * CARSPATH=/opt/carsi x
replacing the asterisks with the appropriate schedule designators.
This will set CARSPATH for the x process and allow the use lib statement that passes the environment variable to work.
You can also, depending on your shell and cron implementation, store your environment setup in a file and do something like:
* * * * * source specialenv.sh && x
Where specialenv.sh contains lines like (for bash)
export CARSPATH=/opt/carsi
You may also be able to set environment variables directly in the crontab, should you choose to do so.
cron does not setup an environment for you when it runs your code, so the environment variable $CARSPATH does not exist. I suggest only running shell scripts from cron, setting the environment inside of the shell script and then running the program you really wanted to run.
example wrapper script:
#!/bin/bash
source ~username/.bash_profile
cd ~username
./script.pl
If you are using ksh or sh you may need to say
#!/bin/sh
. ~username/.profile
cd ~username
./script.pl
Remember to replace username with your username on the system. Also, if the script is not in your home directory you will want to execute it with the path to it, not ./.
You say source, or period space, to load a given shell script in the current shell. You load it in the current shell so that any environment settings stay with the current shell.
~/.bash_profile, ~/.bashrc, ~/.profile, /etc/profile, etc. are all files that commonly hold your environment setup. Which one you are using depends heavily on your OS and who set it up.
Although its not an 'answer', I resolved this issue by using DBI instead of the Plibdata.
Which is kind of milky because now I will have to change a couple scripts around... ahhhhh I wish there was something I could do to make the Plibdata work
I'm still going to try Chas. Owens answer to see if that works
didn't work for me... "interpreter "/bin/bash" not found"maybe it would work with people who have that interpreter
* * * * * CARSPATH=/opt/carsi ./x
works