Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Newbie Problems with cron

Posted on 2005-05-09
15
Medium Priority
?
416 Views
Last Modified: 2013-12-15
Hello,
Im trying to set up a simple cron job to test and learn the ropes.

I have a test executable in the / directory called test.  Its a script.  The contents are:
#!/bin/sh
cd /
touch test.1

In order to test it, I log in as root.  And type the following in crontab -e:

20 * * * * /test

Which is I assume, run the command test in the / directory on the 20th (or whatever I use) minute of each hour.  

The script works when executed outside the cron job, so Im confused why the cron job isnt doing it.

Any help will be appreciated.
Ian
:)

0
Comment
Question by:Rebelnorth
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 15

Assisted Solution

by:periwinkle
periwinkle earned 399 total points
ID: 13964543
Is the cron daemon running on your server?

What does /var/log/cron (or the like) say as far as errors?
0
 
LVL 4

Expert Comment

by:rajendraone
ID: 13966021
This is because there is problem that cron job could not include some of the environment variables.
Try the following:

You should include these lines after #!/bin/bash at your script.

. $HOME/.bash_profile
. $HOME/.bashrc

and also try giving the full path name:
/bin/touch test.1
If it doesnot work then try giving the full pathname at the commands inside your script.

EXACTLY the same problem i faced earlier. And i solved it myself. Please follow the link below to see my postings and details on how it got solved.
http://www.experts-exchange.com/Operating_Systems/Linux/Linux_Administration/Q_21375540.html

Regards,
Rajendra.

0
 
LVL 2

Expert Comment

by:sekargopi
ID: 13976650

most likely issue of your problem is the script does not have executable permission

run chmod 755 /test

then check, it should work. also in the /test program mention /bin/bash instead of /bin/sh

also as the other person mentioned, always use full path to the applications you call within the script. it is not mandatory but it is a good coding skill

Regards,
Gopi
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Rebelnorth
ID: 13977732
Ok I looked further and I think there's a larger problem.

I used ps aux | grep crond to determine if the cron daemon was running and it is.

But when I use crontab -e as root and put this line in my crontab...

13 * * * * touch /test.1

It doesnt do anything (Im using a time set ahead of the current time). Im stumped.  I have no other way of verifying that cron is working?

How would I do that?
0
 
LVL 2

Expert Comment

by:sekargopi
ID: 13978854

just check the local mail box of the root user (/var/spool/mail/root) generally crond sends the command output to this mail account. check whether any errors are reported there

next try to give full path to the command touch and see whether it solves.

your current schedule will invoke the program only at 13th minute of every hour, so put something like 1 * * * * /bin/touch /test.1 to make it run every minute so that you will get more info early.

Regards,
Gopi
0
 
LVL 4

Expert Comment

by:rajendraone
ID: 13983953
Why don't you check the log of the cron job?
try this one:

#tail -f /var/log/cron

Wait for the time your crond should run. This will display the logs if your cron is running.

If you didnt get what i mean then just try a simple command at the cron file, not the script.
*/5 * * * * root /bin/touch somefile
This should create a new file every five minutes.

If you find your cron job is working try my answer i gave earlier. I think you should edit your shell script. Follow my answer i gave earlier and tell me if it worked or not.

Rajendra.
0
 
LVL 4

Expert Comment

by:rajendraone
ID: 13983959
Try giving full path name at your crontab -e.

13 * * * * /bin/touch /test.1

Check your logs and confirm if it worked or not.


Rajendra.
0
 

Author Comment

by:Rebelnorth
ID: 13985779
The logs have no information past a couple months ago.  
Any other ideas?
0
 
LVL 2

Expert Comment

by:sekargopi
ID: 13985925

have you checked the mail, if no mail has come then try this

run crontab -e

then add,
MAILTO=<your email account>  
before your job line, save and close

next time when cron executes it will send email to this account. try to increase the occurance of the event by making it to execute every minute.

PS: the email account should be reachable. you can give something like root@localhost if you have local mail server running.

Gopi
0
 

Author Comment

by:Rebelnorth
ID: 13987132
I actually dont have a mail server running.  SO I cant do that.
:(
Ian
0
 
LVL 2

Expert Comment

by:sekargopi
ID: 13987391

ok try to give email id of any other mail server on the local network.

cron can actually send email to that account, just ensure you give full email id

Gopi
0
 

Author Comment

by:Rebelnorth
ID: 13987890
unfortunately there is no local mailserver that would except mail from that host.  Is there any way I can send this output to a log file?  Im assuming error status is what your looking for right?

Ian
:)
0
 
LVL 2

Assisted Solution

by:sekargopi
sekargopi earned 399 total points
ID: 13988271

did you check whether /var/spool/mail/root file is created and contains some information(with latest time stamp). if so, it is more likely that crond is writing output there. you can do less of that file and find out output information

also are you sure the file is not created in /test.1 with latest time stamp

why dont you try something like this:

create a file /test.sh
#!/bin/bash

echo hello world >> /tmp/test


then run chmod 755 /test.sh

edit your crontab with crontab -e
and add the entry

1 * * * * /test.sh

wait for a minute and see whether /tmp/test file is created with hello world message. i know i sound odd here in asking you test the other way, but let us try this.

Gopi
0
 
LVL 4

Accepted Solution

by:
rajendraone earned 402 total points
ID: 13989109
Instead of doing crontab -e, do as:
#vi /etc/crontab
add the line as follow;

03 03 * * * root /bin/touch /root/myfile
Give your convient time.

And check if the file has been created. And check the log as well if it shows any mesages.
If you want to run the job as root you can place it here at /etc/crontab as shown above.
If it worked try the one woth your script. And tell me if it doesnot work.

Be clear first:
1) Is your cron job working? (Any job, try with simple job)
2) Is your script working running it alone outside cron?
3) Is your script working running it within cron?
0
 

Author Comment

by:Rebelnorth
ID: 14439505
Still working on this... will resolve once some other steps necessary to test have been completed...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

571 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