简体   繁体   中英

Confused with my Cron job

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

您必须删除PATH =之后的多余空间。

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

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM