My Cronjob is not running

I created a script,

size=`du -sm`
size=`echo $size | sed 's/.$//'`
size1='30720'
if [ $size -gt $size1 ]
then
{
find /ask/tarballs -type f -name "*.tgz" -mtime +30 -exec ls -l {} \;
find /ask/tarballs -type f -name "*.tgz" -mtime +30 -exec rm -f {} \;
}
else
echo "Directory size doesnt exceed Threshold Limit"
fi

==============
which is in the directory path /ask/tarballs

so i created a cronjob like

crontab -e

inside that i gave the command

01 * * * * /ask/tarballs/tar1.sh

then pressed Esc button and saved as :wq
so that it should run every minute..

But the script is not running as per the scheduled task..

Could you help me with this..?

It won't run every minute.

With this setup it will run every hour and 1 minute.g 16.01 , 17.01 etc.
Setup for every minute is all `*'

* * * * * /ask/tarballs/tar1.sh

Also please use code tags when posting code.

what is your cron log says ?

check under /var/log

Jobs run by cron run in background with no terminal context. Any output from your cron will not go to a terminal. It will go to unix mail for the user who owns the cron (unless you redirect the output to a log file).

Please post what Operating System and version you have and what Shell the script was written to use. The default Shell for cron is usually /bin/sh which on some O/S like Solaris is a very old Bourne Shell.

Please mention which user owns this crontab and post the output from:

ls -lad /ask/tarballs/tar1.sh

Its showing
You have new mail in /var/spool/mail/root

But how to check that mail..??

And also the script hasn't ran at all.. coz it doesnt delete the file which is 30 days older

---------- Post updated at 05:28 PM ---------- Previous update was at 05:21 PM ----------

@ methyl : i dont understand wat u r saying.. as am a newbie to Unix.. Could u kindly convey message simply as i could understand.. Sorry, I think u could understand.. Thanks for Understanding

---------- Post updated at 05:33 PM ---------- Previous update was at 05:28 PM ----------

Inside the path

/var/spool/mail/root

The cronjob results showed as below

/bin/sh: /ask/tarballs/tar1.sh: Permission denied

Why is this so..?? could u please help me with that

If you were logged in as "root" then the cron is owned by root.

#To find out your Operating System and version:
uname -a
#To find out your normal Shell
echo $SHELL

The mail file is a text file. You can read it with "more" or "tail". In your case at 12:01 GMT you should find it quite easily:

tail -200 /var/spool/mail/root | more

There are proper ways of reading the mail but we are probably just looking for an error message or other output from your cron.

The permissions of the script file are important. It must be executable by the owner of the cron.

ls -ald /ask/tarballs/tar1.sh

(POSTS CROSSED)
You need to make the script file executable.

chmod 755  /ask/tarballs/tar1.sh

Also note that the error message in your post came from /bin/sh which is the default Shell from cron.
If the default Shell from your user is different you will need a shebang line at the top of your script.

The permission is

-rw-r--r-- 1 root root 294 Feb 19 23:26 /ask/tarballs/tar1.sh

The cronjob results showed as below

/bin/sh: /ask/tarballs/tar1.sh: Permission denied

The script is not executable.
I refer you to the "chmod" command in my post #6.

Make your script first executable or else use sh to run

chmod 755  /ask/tarballs/tar1.sh

You can traverse the output of cron to a file say for your error o/p

* * * * * /ask/tarballs/tar1.sh  >> /tmp/tar-log

--Shirish

This will indeed work providing that the messages come from the script and not from cron itself. The problem error from post #1 would still come out in root mail.
Personally I would put the logfile redirect inside the script and keep crontab as uncluttered as possible.

Cronjob is working now actually. But the problem i got now is,
Actually while running the script tar1.sh as a cronjob, only the else part is running.
i.e
Directory size doesnt exceed Threshold Limit
even though size is greater than size1

but when we execute the script as sh tar1.sh, its deleting the expected files

so wat could be the problem..?? could u please help me with this..?

Can you share below

Add below at top of your script

set -x 

and traverse your output to a file in cron and post the file o/p

--Shirish

Remember that you are running from a root cron.
When running from cron the current working directory is "/".

You probably need a "cd" at the start of the script, for example:

cd  /ask/tarballs       # Or whatever directory you are measuring
size=`du -sm`

Note: None of my systems have the "-m" switch to "du" so I don't know what the output looks like.

1 Like