Solved

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

Posted on 2003-12-10
12
1,468 Views
Last Modified: 2010-08-05
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
 
0
Comment
Question by:Aschneider00
  • 6
  • 5
12 Comments
 
LVL 11

Expert Comment

by:turn123
ID: 9916035
I've run a perl script buy giving the full path and it worked. e.g. /usr/home/turn/myscript
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9916307
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.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9916337
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.

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Aschneider00
ID: 9917000
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
 
0
 

Author Comment

by:Aschneider00
ID: 9917098
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
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9917175
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.
0
 

Author Comment

by:Aschneider00
ID: 9917770
#!/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
0
 

Author Comment

by:Aschneider00
ID: 9917775
Oh,
I tried putting in /var/www/html as the destination path and I dont think it worked... let me know what u think

0
 

Author Comment

by:Aschneider00
ID: 9917784
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...
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
ID: 9917849
OK, it's definitely the relative path for your destination path. What happens when you use /var/www/html ?

Why are you using a Perl script? The task is so simple that you can just write the copy command into a shell script:

#!/bin/sh

cp /home/USER/.mozilla/default/9c0cjyn4.slt/bookmarks.html /var/www/html

# end of script

Perl does not buy you anything in this case. Store this script e.g. as "copy.sh" (without the quotes) and make it executable (chmod 755 copy.sh). Then use this in your crontab file.
0
 

Author Comment

by:Aschneider00
ID: 9918187
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???

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9919321
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).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Guacamole cut and paste issue 3 95
Fetching data from Multiple Linux Server using Shell Scripting 55 153
how to rebuild XFS volume from LV 19 89
AWS ELB 5 95
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question