aspose file tools*
The moose likes Linux / UNIX and the fly likes creating a cron file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "creating a cron file" Watch "creating a cron file" New topic
Author

creating a cron file

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
I have created a cron file, but it seems that it doesn't run. here's what I did:
1) created a cron job. If I do "crontab -l" (under my user) I get:
*/15 * * * * /home/aegonzal/myCommand.csh
it "means" that every 15 minutes it will execute my command.
2) in my crontab I have:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="my.emailo@myDomain.com"
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

my question is?
Am I supposed to touch the lines that are bold? I can't get my script to execute every 15 minutes. any hints.
all the cron is created under my name.
thanks!


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Andres,

Am I supposed to touch the lines that are bold?

They are system crontab files. I don't think you need to touch them.

I can't get my script to execute every 15 minutes. any hints.
all the cron is created under my name.

Make sure your crond (cron daemon) is running.
Also, check the email of your root account to see if there are any error messages there related to your cron job. It may be executing but failing for some reason.
Al
[ March 29, 2004: Message edited by: Alton Hernandez ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
thanks Alton.
The daemon is running. I'll check the email of my root account. I have checked my email (where the cron is running) but it doesn't tell me anything.
I'll check that when I get back to work.
thanks again!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
no mail for my user. not even for root. daemon is indeed running. any other suggestions?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Andres,
1, You said that you checked your email where the cron is running and it does not tell you anything. If you are getting an email from cron that means your job ran and probably failed. Do you think you can post the content of that email here?
2. Check your crontab file located at /var/spool/cron. It should be named after the account where it is suppose to run.
3. Modify your cron file. 'cd' to the directory where your script is located and pipe the output of your job to a log file, e.g.
*/15 * * * * cd /home/myaccount;./myjob > 1>x.log 2>&1
Then check the log file later, or see if the log file had been created at all.
Hope this helps.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

1, You said that you checked your email where the cron is running and it does not tell you anything. If you are getting an email from cron that means your job ran and probably failed. Do you think you can post the content of that email here?

I wasn't clear enough, sorry. I didn't get any email at all.

2. Check your crontab file located at /var/spool/cron. It should be named after the account where it is suppose to run.

it certainly is (aegonzal).

3. Modify your cron file. 'cd' to the directory where your script is located and pipe the output of your job to a log file, e.g.
*/15 * * * * cd /home/myaccount;./myjob > 1>x.log 2>&1

done.

Then check the log file later, or see if the log file had been created at all.

I will.. thanks!!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
the daemon is running (it created the log file). however, my shell didn't do what I was expected to do. So here are some questions.
1) I also got an email, this is some info:
From aegonzal@mydomain Tue Mar 30 16:00:00 2004
Return-Path: <aegonzal@mydomain>
Received: from xx (localhost.localdomain [127.0.0.1])
by xxx with ESMTP id i2V000Q4003445
for <aegonzal@domain>; Tue, 30 Mar 2004 16:00:00 -0800
Received: (from aegonzal@localhost)
by xxx (8.12.5/8.12.5/Submit) id i2V000pL003443
for aegonzal; Tue, 30 Mar 2004 16:00:00 -0800
Date: Tue, 30 Mar 2004 16:00:00 -0800
Message-Id: <200403310000.i2V000pL003443@domain>
From: root@domain (Cron Daemon)
To: aegonzal@domain
Subject: Cron <aegonzal@domain> cd /home/aegonzal; ./myCommand.csh > 1>x.log 2>&1
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/aegonzal>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=aegonzal>
Status: RO
/bin/sh: -c: line 1: syntax error near unexpected token `1'
/bin/sh: -c: line 1: `cd /home/aegonzal; ./myCommand.csh > 1>x.log 2>&1'

I fixed the syntax error, making it output to my x.log. The things that I don't understand are:
1) the FROM is root, that's ok. but the TO is to my local linux email account. I'd previously changed the cron configuration to email to adifferent email account (by modifying the MAILTO parameter in crontab), but it seems it's not picking it up.
2) myCommand.csh executes some lines that can only be executed under my user. Even though my user is the owner of the cron, it should be OK then, right? isn't it root that starts the daemon process?
thanks!
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Originally posted by Andres Gonzalez:
the daemon is running (it created the log file). however, my shell didn't do what I was expected to do. So here are some questions.
1) I also got an email, this is some info:
From aegonzal@mydomain Tue Mar 30 16:00:00 2004
Return-Path: <aegonzal@mydomain>
Received: from xx (localhost.localdomain [127.0.0.1])
by xxx with ESMTP id i2V000Q4003445
for <aegonzal@domain>; Tue, 30 Mar 2004 16:00:00 -0800
Received: (from aegonzal@localhost)
by xxx (8.12.5/8.12.5/Submit) id i2V000pL003443
for aegonzal; Tue, 30 Mar 2004 16:00:00 -0800
Date: Tue, 30 Mar 2004 16:00:00 -0800
Message-Id: <200403310000.i2V000pL003443@domain>
From: root@domain (Cron Daemon)
To: aegonzal@domain
Subject: Cron <aegonzal@domain> cd /home/aegonzal; ./myCommand.csh > 1>x.log 2>&1
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/aegonzal>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=aegonzal>
Status: RO
/bin/sh: -c: line 1: syntax error near unexpected token `1'
/bin/sh: -c: line 1: `cd /home/aegonzal; ./myCommand.csh > 1>x.log 2>&1'

I fixed the syntax error, making it output to my x.log. The things that I don't understand are:

when you say you fixed the error, I assume you mean you took out the first greater-than sign, correct?

1) the FROM is root, that's ok. but the TO is to my local linux email account. I'd previously changed the cron configuration to email to adifferent email account (by modifying the MAILTO parameter in crontab), but it seems it's not picking it up.

I've never administered cron, so I'm not sure what you mean by modifying the MAILTO parameter in crontab, but: if that's not working, and the mail is going to the user account's local unix email, try puting a .forward file (text file containing a forwarding email address) in the user's home directory.

2) myCommand.csh executes some lines that can only be executed under my user. Even though my user is the owner of the cron, it should be OK then, right? isn't it root that starts the daemon process?

root starts the process, but it runs as whichever user the crontab is for. I don't remember if there is a distinction between the "real" and "effective" user ids of the process, it might be root as "real" and your id as "effective", or it might run it truly as your id (both).
also note that you shouldn't need to be including the "cd /home/aegonzal;" as part of your cron command. The user's home directory is the default "current working directory" when a process is launched by cron.
I wasn't clear as to whether you only had these questions remaining, or if you were still having problems with the script. If you're still having problems once you've got the cron successfully launching your script, they'd be from one of two things:
1. the script itself, or
2. the differences in the environment when you invoke it from the command line vs. when it is invoked by cron.
If you don't already know, your .profile (or .login, or whatever you use to configure your shell environment) is not run to initialize the environment that your crons run in [I]unless you explicitly source it ( . /home/aegonzal/.profile ) in your script... and then you have to watch out for shell-interactive commands and things that require a terminal[I].
As a good way of showing what the differences are between your shell environment and that of your crons, write a simple script that dumps the environment to a file, and run that script from your cron and from the command line, then diff the files. In Korn shell it would be something like this:

Hope this helps.
-- Jon
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
You were right. My shell is doing some scp (which requires key authentication). It works ok if I do it manually, but when using cron it's all bad. it turned out that I had a lot of missing environment entries; so what I did was (not sure if it's correct):
*/15 * * * * . /etc/profile ; ./myShell.csh
ant it worked. the only problem is when sending emails, I can't get it to send emails to my personal account.
[JE]I've never administered cron, so I'm not sure what you mean by modifying the MAILTO parameter in crontab, but: if that's not working, and the mail is going to the user account's local unix email, try puting a .forward file (text file containing a forwarding email address) in the user's home directory.
the crontab file has a parameter called MAILTO. I read somewhere that you specify in that place where you want the log to be emailed. but it doesn't work .
I didn't understand your .forward solution. can you explain to me.
thanks guys!!!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
BTW, this is the documentation

The main configuration file for cron, /etc/crontab, contains the following lines:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
The first four lines are variables used to configure the environment in which the cron tasks are run. The value of the SHELL variable tells the system which shell environment to use (in this example the bash shell), and the PATH variable defines the path used to execute commands. The output of the cron tasks are emailed to the username defined with the MAILTO variable. If the MAILTO variable is defined as an empty string (MAILTO=""), email will not be sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443


/bin/sh: -c: line 1: syntax error near unexpected token `1'
/bin/sh: -c: line 1: `cd /home/aegonzal; ./myCommand.csh > 1>x.log 2>&1'

The error, at least, told you that your cron job is being run.

1) the FROM is root, that's ok. but the TO is to my local linux email account. I'd previously changed the cron configuration to email to adifferent email account (by modifying the MAILTO parameter in crontab), but it seems it's not picking it up.

The /etc/crontab has nothing to do with your own cron file. Perhaps you need to add the MAILTO parameter to your own cron file.

2) myCommand.csh executes some lines that can only be executed under my user. Even though my user is the owner of the cron, it should be OK then, right? isn't it root that starts the daemon process?

Yes, it is root that starts the crond but the script will be run under the account that owns the cron.
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Originally posted by Andres Gonzalez:
so what I did was (not sure if it's correct):
*/15 * * * * . /etc/profile ; ./myShell.csh
ant it worked. the only problem is when sending emails, I can't get it to send emails to my personal account.
[JE]I've never administered cron, so I'm not sure what you mean by modifying the MAILTO parameter in crontab, but: if that's not working, and the mail is going to the user account's local unix email, try puting a .forward file (text file containing a forwarding email address) in the user's home directory.
the crontab file has a parameter called MAILTO. I read somewhere that you specify in that place where you want the log to be emailed. but it doesn't work .
I didn't understand your .forward solution. can you explain to me.
thanks guys!!!

I'm not sure if this works on all UNIX systems, it might be a widely-supported standard, or it might not...
create a file called ".forward" in /home/aegonzal, containing one line of text which is your email address that you want your UNIX mail forwarded to (e.g. Andres.Gonzalez@javaranch.com). You will probably want to test that you can send email from your account to an internet email address. One way to do that:
mailx -s "test message" andres.gonzalez@javaranch.com
that will take input (up until a CTRL-D) as the body of the message, and send it to andres.gonzalez@javaranch.com with subject "test message". If you don't get the message (using your proper email address.... ) then you'll need to look into your email configuration before you can hope to get the .forward to work.
Good luck.
-- Jon
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

guess what ?
[AH]
The /etc/crontab has nothing to do with your own cron file. Perhaps you need to add the MAILTO parameter to your own cron file.

why did it take you so long to say this?
I added the MAILTO parameter into my cron, and it worked. the MAILTO in contab file has nothing to do with it.
I was just ready to start with the .forward option, when I got an email into my mailbox.
thanks a lot guys, I learned a lot!
 
jQuery in Action, 2nd edition
 
subject: creating a cron file