Adding a shell script to the daily Cron

Associate
Joined
18 Nov 2008
Posts
2,430
Location
Liverpool
Hey guys, trying to rotate a log using a shell script (It's necessary, I can't use logrotate for my purpose unfortunately). So I've writted the shell script and tested it to see it works fine when manually executed.

So now I want to add it to run with the daily Cron, but I can't figure out how to do it. If I do crontab -e it opens it up for editing, I edit it and close it to see it says "no crontab for root - using an empty one", and then notice it has not made the changes.

I have also tried doing it through Webmin but it wants me to input the command there which I assume isn't right for what I want as the script is multiple lines long?

Basically, how do I add my .sh file to the daily Cron with the above problems?

Thanks!
 
Last edited:
Hi, I'm assuming you're using Linux here?

I would simply use the crontab -e, then add the script like so:

0 22 * * * /usr/local/bin/backup_fusion.sh >/dev/null 2>&1

Here, my script runs at 10pm every night as an example.
 
If I do that and come out of it though it says it has not made the changes (Small typo in OP)

EDIT: And yes, it is CentOS
 
As it's CentOS have you tried putting your script into /etc/cron.daily/ ? Looking at the CentOS box I am logged into here /etc/crontab indicates anything in that directory should be run, as root, at 04:02 each day ...

Normally you would do as sedmonds suggests though.

Are you trying to run the script in cron as root or another user?
Is root, or the other user, in /etc/cron.allow ?
Do you have a /var/spool/cron/ directory and is there a file in there named as per the user you are creating the cronjob as with just rw permissions for the user ?
Is cron enabled for your current runlevel (chkconfig --list cron) ?
 
As it's CentOS have you tried putting your script into /etc/cron.daily/ ? Looking at the CentOS box I am logged into here /etc/crontab indicates anything in that directory should be run, as root, at 04:02 each day ...
I was originally going to do this, but I'd like it to run at a specified time not 04:02 (Which I guess is the CentOS standard as that's the time on mine as well.

Are you trying to run the script in cron as root or another user?
Root

Is root, or the other user, in /etc/cron.allow ?
There currently isn't an allow, only an empty deny

Do you have a /var/spool/cron/ directory and is there a file in there named as per the user you are creating the cronjob as with just rw permissions for the user ?
There is only a "tmp.XXXXDN86MW" in there?

Is cron enabled for your current runlevel (chkconfig --list cron) ?
Erm: error reading information about service cron: No such file or directory
Is what I get when I try that?
 
Can you try (I'm always in as root - tut tut)

Code:
nano /etc/crontab

I do the above to edit my contab with a few .sh 's in.

Code:
00 22 * * 3 root /root/Desktop/backupscript.sh
*/10 * * * * root /root/Desktop/poweroffhost.sh
 
Last edited:
that brings up the content which are:

Code:
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
 
That's right. Now add your lines in using nano (it's a terminal text editor)

Or you can log in via the gui and browse to /etc/crontab and use the standard gui editor instead

e.g.
Code:
00 22 * * 3 root /destinationtoyoursh/file.sh

will run at 10pm on a wednesday.

My entire crontab for ref:

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

00 22 * * 3 root /root/Desktop/backupscript.sh
#*/10 * * * * root /root/Desktop/poweroffhost.sh

# * * * * * root command to be executed
# - - - - -
# | | | | |
# | | | | +----- day of week (0 - 6) (Sunday=0)
# | | | +------- month (1 - 12)
# | | +--------- day of        month (1 - 31)
# | +----------- hour (0 - 23)
# +------------- min (0 - 59)

# Example
# 00 22 5,15,25 * * root /root/Desktop/backupscript
# This will run the backup script (with root permissions) at 10pm on the 5th, 15th and 25th of every month.
 
Oh! I didn't realise you could add it directly into the file! I know how to write it as I found a great guide so I can do that bit, I just didn't realise you could add it directly into that file!

Thanks very much, I'll do a test one now :)
 
Erm: error reading information about service cron: No such file or directory
Is what I get when I try that?

Try "chkconfig --list crond" followed by "service crond status" to confirm the daemon is currently running.

You can use /etc/crontab but you will need to enter a user profile to run as...

e.g.

33 * * * * root /some/script/here.sh

If you use "crontab -e" as root then it would be...

33 * * * * /some/script/here.sh

If you do not already have a user based crontab then once you have saved the file you should see "crontab: installing new crontab" assuming you use the CLI.
 
I've added:
Code:
0-59 0-23 * * * /root /directory/to/shell/script/rotatelog.sh

But it's not working, whereas it does when manually executed.
 
Last edited:
Sorry just edited my previous post, I have done that but it's still not doing anything.


EDIT: Misread your post and realised my mistake! Applied yours now will update with progress.
 
It's still not working, I've checked the cron log to see the last 3 entries are what I want:
Code:
May 12 16:33:01 localhost crond[16101]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
May 12 16:34:01 localhost crond[16163]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
May 12 16:35:01 localhost crond[16207]: (root) CMD (/directory/to/shell/script/rotatelog.sh)

But it's still not doing anything?
 
Are you trying to run it every minute of every hour? In which case you need

*/1 * * * * root /directory/to/shell/script/rotatelog.sh

I THINK, it's been a while :P

*edit*
Can you tell us what you want running and when?
 
Last edited:
It's still not working with that entered and the service restarted.

I'm trying to call this .sh file at 7:05AM every day, but for the purpose of making sure it works at all I'm making it run every minute so I can check that it creates the file as the contents of the .sh indicates.

If I run it manually it works, but through this it doesn't :(

EDIT: Not sure if this is relevant, but with the code as:
Code:
22 0-23 * * * /directory/to/shell/script/rotatelog.sh
It was making log entries of:
Code:
May 12 16:33:01 localhost crond[16101]: (root) CMD (/directory/to/shell/script/rotatelog.sh)

Whereas with:
Code:
*/1 * * * root /directory/to/shell/script/rotatelog.sh
It makes no log entries.
 
Last edited:
Looking at my crontab:

Every 10 mintues:

*/10 * * * * root /root/Desktop/poweroffhost.sh

Everyone 1 minute:

*/1 * * * * root /root/Desktop/poweroffhost.sh

What's your cron log saying with the above?
 
Yeah just spotted that, I replaced it with yours with the contents edited, I get:
Code:
May 12 16:53:01 localhost crond[17398]: (root) CMD (/directory/to/shell/script/rotatelog.sh)
In the log but it still doesn't do anything.

This was with:
Code:
*/1 * * * * root /directory/to/shell/script/rotatelog.sh
 
Just try something like: */1 * * * * root /usr/bin/wall Hello World!
To make sure this is a cron issue and not anything with your script.
 
Back
Top Bottom