LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-14-2010, 06:32 PM   #1
ghurty
LQ Newbie
 
Registered: Dec 2010
Posts: 6

Rep: Reputation: 0
Running a cron every 7 minutes but to cary over the 7 minute count to the next hour?


Previously I had posted a question on how to make it run every 7 minutes between 7 and and 11pm.

However now I found out what I really need is every 7 minuted between 7:30 and 11pm BUT it has to be every 7 minutes, it cannot reset itself on the top of every hour, so the */7 wont work.


How can it that it will be every 7 minutes, so it will go at 7:30, 37,44,51, 58, 8:05, etc...


Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-14-2010, 06:50 PM   #2
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
You could write the script so that it loops continuously, performing its tasks every seven minutes.

Then use a cronjob to start it at 7 PM and kill it off at 11 PM.
 
Old 12-14-2010, 07:05 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could have a cron job that runs every minute. Checks the time, and quits if it isn't time to run yet, else it performs the action you want.

You could have an entry for each hour between 7pm and 11pm.
30-60/7 19 * *
3-60/7 20 * *
6-60/7 21 * *

Is there a particular reason that 7 minutes is so important. Every 6 minutes would be 10 per hour.

Last edited by jschiwal; 12-14-2010 at 07:22 PM.
 
Old 12-14-2010, 08:36 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,277

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
Sounds like the sort of thing only a teacher would dream up ...
 
Old 12-14-2010, 10:09 PM   #5
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
Quote:
Originally Posted by sleddog View Post
You could write the script so that it loops continuously, performing its tasks every seven minutes.

Then use a cronjob to start it at 7 PM and kill it off at 11 PM.
+1, and checks the time to see when to kill itself.
 
Old 10-07-2013, 04:28 PM   #6
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
I know that this has been asked before, but is there any way to have a cron job run every 7 minutes repeatedly? I suspect that one could write a script and that could be a good solution, but is that the simplest way? Can you avoid having to write a script (I'm not the strongest scripter)
 
Old 10-07-2013, 05:51 PM   #7
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,333
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
Quote:
Originally Posted by syg00 View Post
Sounds like the sort of thing only a teacher would dream up ...
I know, huh?
 
Old 10-07-2013, 05:59 PM   #8
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
Talking

Quote:
Originally Posted by Habitual View Post
I know, huh?
Yeah, it's not though. My boss is having me find out to help train me better, so any help would be appreciated
 
Old 10-07-2013, 07:53 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
Min=$(date +%M)
if (( $Min >= 53 ));do
    Min=$(( $Min + 7 )) 
    Min=$(( $Min - 60 ))
    Mins=($Min)
    until (( $Min >= 53 ));do
        Min=$(($Min + 7))
        Mins+=($Min)
    done
    NewCron="$(sed 's/ /,/g' <<< "${Mins[@]}" )"
    ( crontab -l | sed 's#.*\* \* \* \* /Your/Cron/Commands#'$NewCron' \* \* \* \* /Your/Cron/Commands#' ) | crontab
fi
bit scrappy, but you get the idea
 
1 members found this post helpful.
Old 10-08-2013, 12:51 AM   #10
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 163

Rep: Reputation: 60
I would run a script by cron every minute. The script counts up to 7 and then triggers the action and resets the counter. The counter is stored in a file. This could be as simple as appending a newline to the file every minute, and when the file size reaches 7 bytes, rm the file and execute the action.

I ran the following script by the command line, not by cron, and confirmed that it works. To run this via cron, I think you might like to place the counter file in an appropriate directory (for example, make it /var/local/timekeeper/count), and replace the second echo command with the desired command. If it is possible that the command will take longer than 1 minute, you must execute the command in a subshell. In my script, this is accomplished by ending the command line with an ampersand.
Code:
#!/bin/bash
echo >> count_to_7
if [ "$(cat count_to_7 | wc -c)" -ge "7" ]
then
    rm count_to_7
    echo "We have 7." &
fi
For extra credit, this method can easily be adapted to keep multiple counters with any number of different periods. For example, this script will independently trigger actions every 7, 11, and 13 minutes:
Code:
#!/bin/bash
list_of_periods="7 11 13"
for period in $list_of_periods
do
    echo >> count_to_$period
    if [ "$(cat count_to_$period| wc -c)" -ge "$period" ]
    then
        echo "We have $period." &
        rm count_to_$period
    fi
done
Every 1001 minutes (since 7*11*13=1001), all 3 actions will be triggered at the same time.
 
2 members found this post helpful.
Old 10-08-2013, 10:13 AM   #11
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
Thanks Firerat and Beryllos! That's awesome. I haven't worked much with bash, so I'll look into the language you used!
 
Old 10-08-2013, 10:52 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Some links to get you started


http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide
http://www.gnu.org/software/bash/manual/bashref.html
 
1 members found this post helpful.
Old 10-08-2013, 12:58 PM   #13
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,333
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
Quote:
Originally Posted by Nickjpost View Post
Yeah, it's not though. My boss is having me find out to help train me better, so any help would be appreciated
What a Great Boss, really, most wouldn't.
 
Old 10-08-2013, 06:36 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,437

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
You might invoke this little script every minute during the given time period:
Code:
#!/bin/bash
SevenPM=$(($(date -d 19:00:00 +%s)/60))
Now=$(($(date +%s)/60))
if (( ((Now-SevenPM) % 7)==0 )); then
    :     #do something
else
    :     #don't
fi
It doesn't need to maintain a file to keep track of when it last ran.

Last edited by rknichols; 10-08-2013 at 06:39 PM.
 
1 members found this post helpful.
Old 10-08-2013, 07:13 PM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
the brief, i.e. the thread title
Quote:
Running a cron every 7 minutes but to cary over the 7 minute count to the next hour?
Code:
#!/bin/bash
Min=$(date +%M)
if (( $Min >= 53 ));do
    Min=$(( $Min + 7 )) 
    Min=$(( $Min - 60 ))
    Mins=($Min)
    until (( $Min >= 53 ));do
        Min=$(($Min + 7))
        Mins+=($Min)
    done
    NewCron="$(sed 's/ /,/g' <<< "${Mins[@]}" )"
    ( crontab -l | sed 's#.*\* \* \* \* '$0'#'$NewCron' \* \* \* \* '$0'#' ) | crontab
fi
# now run rest of the script for the cron job
echo "Job Done!"
the last cronjob of the hour sets up the next hour's jobs

OK, the script doesn't fit the full brief in the OP ( 7:30 am to 11pm )

so..
Code:
#!/bin/bash
Min=$(date +%M)
Hour=$(date +%H)
if (( $Min >= 53 ));do
    Min=$(( $Min + 7 ))
    Min=$(( $Min - 60 ))
    # assumes always start at 07:30.. just +30min for rollover
    (( $Hour == 23 )) && Min=30
    Mins=($Min)
    until (( $Min >= 53 ));do
        Min=$(($Min + 7))
        Mins+=($Min)
    done
    NewCron="$(sed 's/ /,/g' <<< "${Mins[@]}" )"
    ( crontab -l | sed 's#.*7-23 \* \* \* '$0'#'$NewCron' 7-23 \* \* \* '$0'#' ) | crontab
fi
# now run rest of the script for the cron job
echo "Job Done!"

Another feature springs to mind
That is the setup of the initial crontab, with the appropriate 'mins' as if it had been started at 07:30
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep string from logs of last 1 hour on files of 2 different servers+calculate count saurabhmehan Linux - Newbie 6 09-06-2010 12:22 AM
Connection refused after 15 to 30 minutes, sometimes an hour lmu9@yahoo.com Linux - Networking 1 12-17-2009 10:13 AM
Cron job issue - every hour works, but specific hour fails lunarleviathan Linux - Newbie 6 11-20-2009 12:19 AM
crontab: minute hour or hour minute rjo98 Linux - Newbie 2 11-04-2009 03:09 PM
Ethernet disconnects every few minutes then reconnects after a minute DragonM15 Linux - Networking 1 01-28-2009 09:19 PM


All times are GMT -5. The time now is 11:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration