Confused with my Cron job

2019-09-04 19:08发布

问题:

I have a perl script which Im planning to run every minute. I have set the cron job as

* * * * * PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl 

I assume the script is executing every minute only because I see a entry like below when I do cat cron in /var/log/

Jul 26 04:57:01 dmvbu-build crond[773]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)

Jul 26 04:58:01 dmvbu-build crond[687]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)

But my problem is I have statements like

print LOG "connecting to website\n"

where LOG is a file descriptor to a file named log.txt which is located at dm2/www/html/isos/pre5.3/ (same place as autoDownload.pl)

But I dont see this log.txt file updating with new informations after I see the entry in the cron log file But I see this file updating when I run the code manually

回答1:

The cron line should be

* * * * * PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib perl /dm2/www/html/isos/pre5.3/autoDownload.pl

Note the lack of a space after the = and the lack of a semicolon before perl.



回答2:

You have to remove the extra space after the = in PATH.

PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib


回答3:

Provide the absolute Path to the logfile (/dm2/www/html/isos/pre5.3/log.txt instead of just log.txt) when open()-ing, otherwise you have to ensure that the "current working directory" of cron is where you want it to be.

Also check that the user under which this command is executed has write-permissions to the file.



回答4:

Its much easier to debug a program when you have the output/errors. You should also use an absolute path to perl in either the top of you script or on the cron line. You should then be able to get rid of any $PATH messyness.

# Make sure you script is executable
chmod a+x /dm2/www/html/isos/pre5.3/autoDownload.pl

Try adding a MAILTO line to cron above your process.

MAILTO="me@example.com"
* * * * * /usr/bin/perl /dm2/www/html/isos/pre5.3/autoDownload.pl

or alternatively logging the cron output to a file to watch for errors. Get rid of /usr/bin/perl in the crontab by making sure its the 1st line of the script #!/usr/bin/perl.

* * * * * /dm2/www/html/isos/pre5.3/autoDownload.pl &> /tmp/autoDownload.log


标签: linux perl cron