You are not logged in.

#1 2008-06-02 01:48:09

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[solved] Cron Jobs

I would like to run a simple shell script every hour. I have crond in my /etc/rc.conf. How can I go about configuring it to run the script every hour? And would it be any different to run a PHP script every hour?

Last edited by tony5429 (2008-06-07 01:17:22)

Offline

#2 2008-06-02 02:04:31

nj
Member
Registered: 2007-04-06
Posts: 93

Re: [solved] Cron Jobs

$ crontab -e
0 * * * * /bin/sh /path/to/script

cron will email you any output from the command, so consider redirecting the output of the script if there is any

0 * * * * /bin/sh /path/to/script 2>&1 > /dev/null

For php, just use /usr/bin/php /path/to/script. Also see 'man crontab' for more examples and explanation of the syntax.

Offline

#3 2008-06-02 02:51:34

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [solved] Cron Jobs

Alternatively, you can symlink the script to the /etc/cron.hourly directory:

ln -s /path/to/script.sh /etc/cron.hourly/myscript.sh

There's also /etc/cron.daily and /etc/cron.yearly smile

Offline

#4 2008-06-02 06:21:22

edward.al
Member
Registered: 2008-05-26
Posts: 12

Re: [solved] Cron Jobs

there is another way to do it

* */1 * * * /bin/sh /path/to/script

is there any different between them??

Offline

#5 2008-06-02 06:42:30

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [solved] Cron Jobs

edward.al wrote:

there is another way to do it

* */1 * * * /bin/sh /path/to/script

is there any different between them??

Yes - that will still run every minute, of every hour tongue

Offline

#6 2008-06-02 06:54:00

edward.al
Member
Registered: 2008-05-26
Posts: 12

Re: [solved] Cron Jobs

fukawi2 wrote:
edward.al wrote:

there is another way to do it

* */1 * * * /bin/sh /path/to/script

is there any different between them??

Yes - that will still run every minute, of every hour tongue

*/60 * * * * /bin/sh /path/to/script

it run every 60 minutes, not every hour at minute 0 ?:/

Offline

#7 2008-06-02 08:42:16

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [solved] Cron Jobs

edward.al wrote:
*/60 * * * * /bin/sh /path/to/script

it run every 60 minutes, not every hour at minute 0 ?:/

Yes, that will... What you originally posted wouldn't smile

Offline

#8 2008-06-06 08:13:43

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [solved] Cron Jobs

Thanks! It works! One question though... I am creating the shell script directly in /etc/cron.hourly. How do I tell that not to send me an e-mail of the output each time it runs?

Offline

#9 2008-06-06 11:28:03

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [solved] Cron Jobs

Inside your script, you'll have to redirect everything that produces output to /dev/null

For example:

echo "this is output" > /dev/null

If you want to suppress error output too, use the &> redirector:

echo "this is output" &> /dev/null

If I have a script like that, I find it easier to add it to the crontab (using crontab -e) and just redirect the output of the whole script:

*/60 * * * * /path/to/script > /dev/null

Last edited by fukawi2 (2008-06-06 11:28:31)

Offline

#10 2008-06-06 15:22:59

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [solved] Cron Jobs

Perfect; thanks!

Offline

#11 2008-06-07 00:26:02

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [solved] Cron Jobs

Hrm... actually I am running into a problem... In each case below, I am showing the contents of my shell script (/etc/cron.hourly/shcrontest.sh)...

This works, but sends me an e-mail each time:

mkdir /home/karam/shcrontestworked

This fails to ever create the directory:

echo "mkdir /home/karam/shcrontestworked" > /dev/null

This also fails to ever create the directory:

echo "mkdir /home/karam/shcrontestworked" &> /dev/null

I do have it set to chmod +x (-rwxr-xr-x). I am sure I am missing something very simple here. Can anyone help?

Offline

#12 2008-06-07 01:16:23

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [solved] Cron Jobs

Got it!

mkdir /home/karam/shcrontestworked > /dev/null 2>&1

Last edited by tony5429 (2008-06-07 07:24:10)

Offline

#13 2008-06-07 10:44:46

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [solved] Cron Jobs

That's it - sorry, I can see how I was confusing in my last post smile

Offline

#14 2010-02-15 01:18:35

veranyon
Member
From: Odintsovo/Russian Federation
Registered: 2009-07-08
Posts: 107
Website

Re: [solved] Cron Jobs

hm..
neat..

Last edited by veranyon (2010-02-15 01:24:53)


ArchLinux x86_64 /AMD Phenom x4 9550/4 GB RAM/ASUS M3N78-EM/GeForce 9800 GT/DELL 2408 VFP
[img]http://img17.imageshack.us/img17/3443/111bzf.gif[/img]

Offline

#15 2010-02-15 01:54:17

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: [solved] Cron Jobs

Holy ancient thread Batman!

Offline

Board footer

Powered by FluxBB