Link to home
Start Free TrialLog in
Avatar of Aschneider00
Aschneider00

asked on

CRON job in Linux using a perl script....

I have a perl scrip that I am using to copy a file from one directory to another..... when I execute it :

$ perl perlscript.pl

It works fine...  

I want this script to run every day .... I do not know how to tell linux that it is a perl script when it executes otherwise it wont work....

Please advise.... I have webmin installed and cant figure it out in there either.
Thanks
 
Avatar of turn123
turn123
Flag of United States of America image

I've run a perl script buy giving the full path and it worked. e.g. /usr/home/turn/myscript
You have to add this line as the first line of the script:

#!/usr/bin/perl
(or #!/usr/local/bin/perl, or wherever your perl executable is installed).

In addition to this, you have to make the file executable: chmod 755 perlscript.pl

And then, you have to add a crontab entry for this file.
I'm back with my crontab instructions:
Run the following command:
crontab -e

This will bring up your editor with the crontab file loaded.
Add a line like this:
0 2 * * * /path/to/your/perl/script

This will run the program every night at 2:00am. The five fields are:

minute (0-59)
hour (0-23)
day of month (1-31)
month (1-12)
day of week (0-7)

This is also the order in which these fields are specified. If you want to always run the command, you use '*'.
So 0 2 * * * means: 0 minutes after 2 hours (2:00am) on every day, every monty and every day of the week.

Avatar of Aschneider00
Aschneider00

ASKER

so all i have to do is make it executable and can run it as a CRON job?
so I dont have to put "perl" in front of the file  when I type

$ perl perlscript

???

 I will go try it
Thanks
 
I changed the permissions and now I can execute it with

./perlscript.pl

but is there another way in  a gui that I can have it run.... for somereason I tried webmin and it is giving me this error...

cp: cannot create regular file `../html/bookmarks.html': No such file or directory

I am copying the bookmarks page from mozilla to the html directory for apache  " /var/www/html '
the script resides in "/ var/cgi-bin "

apache is the group that I have made the whole www owner of....

dont know what else.... I'd really like to get it setup in webmin,,,,,,

all i had was /var/www/cgi-bin/perlscript.pl in webmin and it should execute... ut it goves me that error.

Thanks in advance
Can you post your script?

Does your program use relative paths (e.g. ../html/bookmarks.html like you quoted) for the destination? If so, change the directory first. If you don't call chdir, your current directory will be used by the script as the basis for all relative paths.
#!/usr/bin/perl
use strict;
use CGI qw(:cgi);
#use CGI qw/:standard/;


my $file = "/home/USER/.mozilla/default/9c0cjyn4.slt/bookmarks.html";
my $destinationdir = "../html";

{
`cp $file $destinationdir/`;
}

this is whats in the file.... noithing that would mess with security... so here ya go
Oh,
I tried putting in /var/www/html as the destination path and I dont think it worked... let me know what u think

the folder structure for apache is as follows

                           www
                         /          \
                 cgi-bin     html      

I believe the script moves into the html folder correctly, because the script does work, its just about getting it to run automatically...
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried writing the shell script and I must not have had the syntx right.... Thanks!!

Reason I wanted to mess with perl was

1.  I just took a class with Unix and that was the current topic
2.  Usually isnt it good for autmoated things???

Shure, in general Perl is much better than a shell script, but in this case, because the task is so simple (just one command), it's easier to just write the shell script.
ALso, in your Perl script, you don't have to use the CGI stuff: You are not writing a CGI script (for a web server).