Setup a cron job and specified the start and end time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Setup a cron job and specified the start and end time
# 1  
Old 10-19-2011
Setup a cron job and specified the start and end time

Hi guys,

How can I specify the start and end time of a cron job. And my start time and end time are specified by minutes. For example, I want to set up a cron runs every 3 minutes from 18:40 to midnight. How can i do this please? Many thanks

Best regards,
Clu
# 2  
Old 10-19-2011
If I were doing this, I'd kick off a script at 18:40, just once, and let it run your programme/script every three minutes until midnight.

Code:
#!/usr/bin/env ksh

while true
do
    d=$( date +%H )
    if (( $d == 0 ))
    then
        exit
    fi

    echo "run your command here"
    sleep 180
done

Your crontab entry would be simple:
Code:
40 18 * * * /path/to/your/script.ksh >/tmp/script.log 2>&1

If the command you are running takes several minutes to run, and you want it to start right at the next three minute mark, run it asynchronously (use &) so that the waking from the sleep is 3 minutes from when the programme started and not three minutes from when the programme ended.

Last edited by agama; 10-19-2011 at 08:55 PM.. Reason: clarification
# 3  
Old 10-19-2011
Hi agama,

Many thanks for this. We can't change the script that we are going to schedule in the cron. Does it mean this can't be done in one line of cron? Thanks.

Can
# 4  
Old 10-19-2011
You wouldn't need to change the script, add a new one that invokes the one you were going to start from cron. If you cannot add a new script, then you'll end up with two entries in the crontab.

Code:
40-59/3 18 * * * /path/to/script >>/tmp/script.log 2>&1
*/3 19-23 * * * /path/to/script >>/tmp/script.log 2>&1

Note that some versions of cron do not support */3 notation.

Last edited by agama; 10-19-2011 at 10:16 PM.. Reason: simplification and typo correction
This User Gave Thanks to agama For This Post:
# 5  
Old 10-19-2011
Hi agama,

Ideally I'd like to create a new script to invoke the required script as recommended, in case I can't I'll go with your second suggestion.

Many thanks for your help.

Best regards,
clu
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

How to end script in a cron job?

I've created a script to copy backup files from an HP-UX 11iv3 system to an NFS share on another machine. I want to schedule the script to run via cron. The script is simply three lines of cp /backups/Backup /shared/Backup. I've saved the script as a .sh file and call it with KSH. Do I need to... (3 Replies)
Discussion started by: jduehmig
3 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

setup cron job

HI I am new to unix . I wanted to create a cron job that runs a script every 10 min .So when I enter vi /etc/crontab I get the following output SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * *... (1 Reply)
Discussion started by: ptappeta
1 Replies

4. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

5. Solaris

How to setup a cron job to run every 45 minutes in Solaris 5.10

How to setup a cron job to run every 45 minutes in Solaris 5.10 (9 Replies)
Discussion started by: reyazan
9 Replies

6. Shell Programming and Scripting

Start time/end time and status of crontab job

Is there anyway to get the start time and end time / status of a crontab job which was just completed? Of course, we know the start time of the crontab job since we are scheduling. But I would like to know process start and time recorded somewhere or can be fetched from a command like 'ps'. ... (3 Replies)
Discussion started by: thambi
3 Replies

7. UNIX for Dummies Questions & Answers

Setup Cron Job

Hello, I would like setup a CRON job to rename my server's name (host name) every day. For some reason it changes its own name every couple days to server-2.local fron server.local. Thank you! -Chris (3 Replies)
Discussion started by: christo16
3 Replies

8. UNIX for Advanced & Expert Users

cron does not start the need job

Hi, can you please help me with one problem? There is some server, there are a lot of AIX applications which run on this server under different UserIDs. These applications starts via crontab. From time to time some application doesn't start. What can this "not-action" be dependent on? At... (5 Replies)
Discussion started by: Anta
5 Replies

9. Shell Programming and Scripting

Cron job at system start up

I want to know if there is a way to make a certain set of programs start in order at system startup with cron or something else i dont know about. (3 Replies)
Discussion started by: rcunn87
3 Replies
Login or Register to Ask a Question