Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Cron, Batch And At

A.P. Lawrence
Expert Author
Published: 2003-04-04

WebProNews RSS Feed


These three commands are used to run commands at some other time. They differ in their usage, their environment, and their default actions, so are sometimes a source of confusion.

The "batch" command really just calls at with special flags set:



but "cron" is truly a totally separate command.

One of the common problems posted to the Unix newsgroups goes something like:

"I have a command script. If I run it from the command line or with "at", it works, but if I run it with "cron" it fails. Why?"

Not only is this a common question, but amazingly enough, it usually generates three or four wrong answers every time it appears. The correct answer is that it fails because "cron" runs with a different environment than what you have. You have a certain PATH, you have other environment variables set, and "at" deliberately notices all that and makes sure that when your command runs, all those things will be in place. The "cron" utility does not: it has its own environment, probably very different from yours.

Very often, it's just the PATH that is different. For example, root's environment usually has



but cron (in SCO OSR5) sets its environment to



Note: the man page for crontab says that the PATH will be "/bin:/usr/bin:" but the above PATH is what actually happens on my 5.0.5 system. The difference is especially significant because of the placement of the lone ":", which adds "." to cron's PATH. According to the manual, the "." would end up at end of the path, and thus would be the last place searched. It actually ends up at the beginning. As the first command found is the command executed, this can cause unexpected results. Cron does cd to the home directory of the user whose cron job is being run, so "." will always refer to that directory- unless the command script itself changes directories and then issues another command.

The other likely cause for failure is a missing environment variable. Cron only has:



Linux cron has the very nice feature of being able to set environment variables directly in the crontab file ( see man 5 crontab on a Linux system). It also allows you to control who mail is sent to when the commands generate output. Linux crontab files are found in non-standard (non-standard for Unix) places, so you should read the man pages ( or "info cron" ) carefully- it's a bit different than Unix.

See Bofcusm/1880.html for more on crontab and /etc/default/cron.

If your command script requires anything else, it will fail. If your script required Korn shell, it would also fail (if cron notices that you are not running /bin/sh when you change a crontab, it will remind you that it plans to use /bin/sh).

A simple way to fix this is to use "at" to set up your environment.You can, for example, type:



"at" will spit back a job number. Change directories to /usr/spool/cron/atjobs and you'll see that same number listed. Notice that it is owned by root with group of cron, and that the permissions are: ---Sr-S---. Copy that file somewhere else, then rm the file in the spool area, and take a look at the copy you made. It will look something like this:



Notice what great pains "at" has gone to to match your environment. Not only do you have all your environment variables, but your ulimit and umask have been matched and it has even cd'd you to the directory you were in when you issued the command. So now use that script from crontab rather than "fakecommand" directly. That is, if you copied the at script to /usr/local/bin/runfake, it is /usr/local/runfake that you would invoke from cron, not "fakecommand".

You probably know that you do not edit the crontab files directly. Some people use "crontab -e", but a more safe procedure is:



Or, if for another user:



Be careful with crontabs set to other users. Remember that cron cd's to the user's directory when it starts up your job. If the user's directory doesn't exist cron fails and sends mail to that user. That doesn't sound too horrible, does it? Well, on the older 3.2v4.2 release, there was some bug somewhere that sometimes caused /usr/sys to be removed. The "sys" user still existed, and still had its crontab file, but when cron tried to run, it couldn't cd to the non-existent home directory. This caused it to start sending mail to "sys" complaining. That wouldn't have been too bad, but who reads "the "sys" user's mail? Usually nobody, so the mail file would build up larger and larger. Eventually it would start to affect the performance of mmdf, and mmdf would get backed up- it couldn't clear out its own spool directories quite as fast as they were growing, which meant that the size of the directories in /usr/spool/mmdf started growing. The larger a directory is, the more time it takes to search it, so this made mmdf run even more slowly, which caused it to get more behind, which, of course, caused the directories to grow larger yet... and to add insult to injury, mmdf would itself start generating messages about mail it couldn't deliver (now that's dumb!), and those only added to the problem. Eventually this would get bad enough to affect performance, because mmdf was spending every spare cpu cycle available trying to deliver mail. So sar would show 0 idle time, the disk would be thrashing as mmdf tried to catch up, and performanced nose dived. What a mess, and all because of a missing directory.

The "at" and "batch" programs are much less complex. Batch takes no arguments; it just runs your program. The details of these are covered in the man pages.

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us