Link to home
Start Free TrialLog in
Avatar of Rebelnorth
Rebelnorth

asked on

Newbie Problems with cron

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
:)

SOLUTION
Avatar of periwinkle
periwinkle
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
https://www.experts-exchange.com/questions/21375540/user-cron-shell-ecript.html

Regards,
Rajendra.

Avatar of sekargopi
sekargopi


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
Avatar of Rebelnorth

ASKER

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?

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
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.
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.
The logs have no information past a couple months ago.  
Any other ideas?

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
I actually dont have a mail server running.  SO I cant do that.
:(
Ian

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
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
:)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still working on this... will resolve once some other steps necessary to test have been completed...