Operating System - HP-UX
1832568 Members
4370 Online
110043 Solutions
New Discussion

Re: Who can help me about how to use "crontab" command

 
SOLVED
Go to solution
zocmiu
Occasional Contributor

Who can help me about how to use "crontab" command

Hi, My friend told me that HP-UX can do some job at one assigned time. I can do it. For example, I has wrote it:
#M H D M Y CMD
15 * * * * echo 'Woohoo, spring is here!'
My question is :
This job need be run four times at each hour, who can help me how to write the crontab file?
zocmiu
9 REPLIES 9
Michael Tully
Honored Contributor

Re: Who can help me about how to use "crontab" command

Hi,

All you need to do is place a comma after each
entry for the minutes.

For example you wanted to run a job at 15
minute intervals, at 1 minute past the hour
and again at 16 31 and 46 this is all you do.

1,16,31,46 * * * * /usr/local/bin/myjob

HTH
-Michael
Anyone for a Mutiny ?
Kenny Chau
Trusted Contributor

Re: Who can help me about how to use "crontab" command

Hi,

You can indicate the time you want in the all the field of time, eg.

If I want to run a job (in crontab) 4 times an hour, 4 times a day, I can specify that like this:

0,15,30,45 09,12,15,18 * * *

Hope this helps.
Kenny.
Kenny
Michael Tully
Honored Contributor

Re: Who can help me about how to use "crontab" command

Hi,

One further thought. To edit your crontab
correctly use this example, as root.

# crontab -l >/tmp/wrk1
Make your changes and save the file

# crontab /tmp/wrk1

Never use crontab -e as it is a recipe for
disaster.

Use the man pages as a further guide.

man cron (explanations of the minutes, hours etc.
man crontab

HTH
-Michael
Anyone for a Mutiny ?
Deepak Extross
Honored Contributor

Re: Who can help me about how to use "crontab" command

Hi,
A point to note: cron jobs are not associated with any particular terminal. Cron is normally used for non-interactive, background processes.
So don't be surprised if you don't see 'Woohoo, spring is here!' on your screen every 15 minutes. You will have more luck if you redirect the output like so - echo 'Woohoo, spring is here!' >> /tmp/cron_output


zocmiu
Occasional Contributor

Re: Who can help me about how to use "crontab" command

Thanks yours heartly answer.
However, if i want to run my job each three mintues, has some more good method? Can it be use like it: ( of course it is not right in HP-UX, this is only an idea )
#M H D M Y CMD
*/3 * * * * date
In fact, I need more complex. My job is first start at 18 mintue, then need start after each 15 mintue later.
I need your more help.
Thanx.
zocmiu
Michael Tully
Honored Contributor

Re: Who can help me about how to use "crontab" command

Hi,

You can start the job as many times as you wish.
All you need to is to place a comma after each
minute entry. You can run the same job every
minute if you wish.

e.g. A job every 3 minutes

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /usr/local/bin/myjob


HTH
-Michael
Anyone for a Mutiny ?
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Who can help me about how to use "crontab" command

Hi,

>> In fact, I need more complex. My job is first start at 18 mintue, then need start after each 15 mintue later.

Instead of using cron, you can use a nohup script job in the background:

# nohup my_job &

In your my_job script,
=====================================
#!/sbin/sh

# This will wait for 18 mins before running the initial task perform_initial_task.

sleep `expr 18 \* 60`
perform_initial_task

# This will wait for 15 mins from the completion endtime of perform_initial_task before running perform_periodic_task which proceeds to run every 15 mins (from the completion endtime of the last execution).

while sleep `expr 15 \* 60`
do
perform_periodic_task
done
=====================================
Hope this helps. Regards.

Steven Sim Kok Leong
zocmiu
Occasional Contributor

Re: Who can help me about how to use "crontab" command

Hi, Steven Sim Kok Leong:
thanks a lot.
zocmiu
Kenny Chau
Trusted Contributor

Re: Who can help me about how to use "crontab" command

I had an alternate solution. If you want to run the job forever, you can use "at" command. If you want to run it daily, you need to use a counter in the script that Steven provided so that it will stop in a period of time. eg.

perform_initial_task
counter=1
while [ $counter -le 40 ] # stop in 10 hours
do
sleep `expr 15 \* 60`
perform_periodic_task
(( counter = counter + 1 ))
done

If you want to start the job at 9:18am daily, you can use crontab job:
18 09 * * * script

No need to give points to me, I just get this from Steven's reply.

Regards,
Kenny.
Kenny