This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

bind cron weekly help

Help for those learning Tcl or writing their own scripts.
Post Reply
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

bind cron weekly help

Post by juanamores »

Need a process is active:
monday to thursday
from 02:01AM to 6PM every 20 minutes
from 6:01PM to 02:00AM the next day every 10 minutes
Fridays, Saturdays and Sundays
from 4:01AM to 7PM every 15min
from 7:01PM to 4:00AM the next day every 5min
(please see EDIT)

I read the eggdrop documentation but did not understand how it works. :oops:

The proc:
bind cron - {?????} advertise

Code: Select all

proc advertise { nick uhost hand chan arg } {
global advertisetext status1 topicount
		if {($status1 != "ON") || ($topicount == 0 )}  { return  
		} else {
			if {![file exists radiochans1.txt]} { return 
			} else {
				set fname "radiochans1.txt"
				set fp [open $fname "r"]
				set data [read -nonewline $fp]
				close $fp
				set lines [split $data "\n"]  
				foreach chan [channels] {
						if {$lines != "" } {
							if {([lsearch -exact [string tolower $lines] [string tolower $chan]] != -1)} { putserv "privmsg $chan :$advertisetext" }
						}
				}
			}
		}	
}
If you can explain how it is done, so the next codes I will modify without help, I have 10 more processes to change.

EDIT:
Reviewing the consultation, I made an inconsistency between Sunday and Monday:

Monday must be
:
from 04:01AM to 6PM every 20 minutes
from 6:01PM to 02:00AM (tuesday)the next day every 10 minutes
Tuesday and Wednesday
from 02:01AM to 6PM every 20 minutes
from 6:01PM to 02:00AM the next day every 10 minutes
Thursday
from 02:01AM to 6PM every 20 minutes
from 6:01PM to 04:00AM (friday)the next day every 10 minutes
Fridays, Saturdays and Sundays
from 4:01AM to 7PM every 15min
from 7:01PM to 4:00AM the next day every 5min
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
w
willyw
Revered One
Posts: 1196
Joined: Thu Jan 15, 2009 12:55 am

Re: bind cron weekly help

Post by willyw »

juanamores wrote: ...
I read the eggdrop documentation but did not understand how it works. ...
...
If you can explain how it is done, so the next codes I will modify without help, I have 10 more processes to change.
...
The eggdrop tcl-commands.doc does not go into depth on this. It simply says that to refer to the crontab manual.

What I do:
Google for terms such as, "crontab examples".

You will find lots of help that way. With the various specifics that you require, I think you will find ways to accomplish them, with different lines for each in your crontab.

I hope this helps.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

I did so, not if you work well, I have not had time opportunity to test it.
If there is a better way to do it without many if ... I appreciate the help.
Please, see if logical and properly bind cron command syntax.

Code: Select all

bind cron - {* * * * *} cron:semanal

proc cron:semanal {min hour day month weekday} {
	if {![file exists radiochans1.txt]} {
		set fs [open radiochans1.txt w]
		puts $fs ""
		close $fs
	} else {
		set fname "radiochans1.txt"
		set fp [open $fname "r"]
		set data [read -nonewline $fp]
		close $fp
		set lines [split $data "\n"]  
		foreach chan [channels] {
			if {$chan == "" } continue 
			if {[botonchan $chan] != 1} {break;return}
		}
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 00 && [string trimleft $weekday 0] == 1} { bind cron - {*/20 * 00-18 * 1} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 18 && [string trimleft $weekday 0] == 1} { bind cron - {*/10 * 18-24 * 1} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 00 && [string trimleft $weekday 0] == 2} { bind cron - {*/20 * 00-18 * 2} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 18 && [string trimleft $weekday 0] == 2} { bind cron - {*/10 * 18-24 * 2} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 02 && [string trimleft $weekday 0] == 3} { bind cron - {*/20 * 02-18 * 3} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 18 && [string trimleft $weekday 0] == 3} { bind cron - {*/10 * 18-24 * 3} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 02 && [string trimleft $weekday 0] == 4} { bind cron - {*/20 * 02-18 * 4} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 18 && [string trimleft $weekday 0] == 4} { bind cron - {*/10 * 18-24 * 4} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 00 && [string trimleft $weekday 0] == 5} { bind cron - {*/20 * 00-19 * 5} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 19 && [string trimleft $weekday 0] == 5} { bind cron - {*/10 * 19-24 * 5} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 00 && [string trimleft $weekday 0] == 6} { bind cron - {*/20 * 00-19 * 6} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 19 && [string trimleft $weekday 0] == 6} { bind cron - {*/10 * 19-24 * 6} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 00 && [string trimleft $weekday 0] == 7} { bind cron - {*/20 * 00-19 * 7} advertise }
		if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 19 && [string trimleft $weekday 0] == 7} { bind cron - {*/10 * 19-24 * 7} advertise }
	}
}
EDIT:
I had doubts, I did not know how to put 18 - 23:59, because I think it does not allow the syntax of this command.
i.e:

Code: Select all

if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 18 && [string trimleft $weekday 0] == 1} { bind cron - {*/10 * 18-24 * 1} advertise } 
If I put 18-00 and would be in the next day. :?

EDIT2:
The more I read about it the less I understand. :?
The example below, it means the advertise command will run every 10 minutes between 18 and 23:59 every Monday ?

Code: Select all

bind cron - {0-59/10 18-23 * * 1} advertise 
=======================================
If my reasoning is correct, the examples below would be fine.
Sunday every 10 min 0:00 - 03:59

Code: Select all

if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 0 && [string trimleft $weekday 0] == 0} { bind cron - {0-59/10 0-3 * * 0} advertise }
Sunday every 20 min 4:00 - 18:59

Code: Select all

  if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 0 && [string trimleft $weekday 0] == 0} { bind cron - {0-59/20 4-18 * * 0} advertise }
Sunday every 10 min 19:00 - 23:59

Code: Select all

  if {[string trimleft $min 0] == 00 && [string trimleft $hour 0] == 19 && [string trimleft $weekday 0] == 0} { bind cron - {0-59/10 19-23 * * 0} advertise }
=======================================
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Re: bind cron weekly help

Post by juanamores »

willyw wrote: What I do:
Google for terms such as, "crontab examples".
This issue is very confusing, the examples I saw on Google for the days used interchangeably: 0-6 or 1-7 (1 digit) or Mon Tue etc ..
But in the debuggin the code returns me to day with 2 digits and you can not use abbreviations.
00 or 07 Sunday
01 Monday
etc ....
I could only test is that the syntax of the command if I put before, are wrong.
This is the correct syntax according to testing.

Code: Select all

 if {$min == "47" && $hour == "01" && $weekday == "06"} {
Examples are wrong, not working properly.
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Fixed

Post by juanamores »

I found a solution, it is not very "orthodox", but is all I could to do with my limited knowledge of Tcl.
I share it to other users who may have the same doubts and I close this topic if no better ideas.

Code: Select all

bind cron - {* * * * *} cron:semanal

proc cron:semanal {min hour day month weekday} {
global canal_admin
	if {![file exists radiochans1.txt]} {
		return
	} else {
				set fname "radiochans1.txt"
				set fp [open $fname "r"]
				set data [read -nonewline $fp]
				close $fp
				set lines [split $data "\n"]  
				set onch1 0
				foreach chan [channels] {
					if {([lsearch -glob -ascii -nocase $lines $chan] != -1) && ([onchan $::botnick $chan])} { set onch1 [expr {$onch1 + 1}] }
				}
				if {$onch1 == 0} return  
				if {$min == "00" && $hour == "04" && $weekday == "00"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "00"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "00" && $weekday == "01"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "01"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "00" && $weekday == "02"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "02"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "00" && $weekday == "03"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "03"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "00" && $weekday == "04"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "04"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "04" && $weekday == "05"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "05"} {unbinds;bind cron - {*/10 * * * *} advertise }
				if {$min == "00" && $hour == "04" && $weekday == "06"} {unbinds;bind cron - {*/20 * * * *} advertise }
				if {$min == "00" && $hour == "18" && $weekday == "06"} {unbinds;bind cron - {*/10 * * * *} advertise }
			}
}


proc unbinds { } {
	foreach ele [binds cron] {
        foreach {type flags name hits proc} $ele {
                if {[string match "advertise" $proc]} {
                  unbind $type $flags $name $proc
                  break
                }
		}
	} 
}
I used unbind command because to put range time, not getting the desired results, by joining the previous timer to the next.
I tried:

Code: Select all

bind cron -|- {*/10 00-04 * * *} advertise
At 04:00 the process stopped , but continued to run advertising for 10 minutes (queue) and joined to the next.

The process is activated this way:
Sundays 04:00-18:00 every 20 min
Sundays 18:00-23:59 every 10 min
From Monday to Thursday from 00:00-18:00 every 20 min
From Monday to Thursday from 18:00-23:59 every 10 min
Friday 00:00-04:00 every 20 min
Friday 04:00-18:00 every 10 min
Friday 18:00-23:59 every 10 min
Saturday from 00:00-04:00 every 10 min
Saturday from 04:00-18:00 every 20 min
Saturday from 18:00-23:59 every 10 minutes until Sunday at 04:00 am returning every 20 min.
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

What about

Code: Select all

bind cron - {*/20 4 * * 0,5,6} advertise
bind cron - {*/20 0 * * 1,2,3,4} advertise
bind cron - {*/10 18 * * *} advertise
Not 100% sure so give this a try. :)

And in the advertise function do that unbind of the previous cron binds.
Once the game is over, the king and the pawn go back in the same box.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

caesar wrote:What about

Code: Select all

bind cron - {*/20 4 * * 0,5,6} advertise
bind cron - {*/20 0 * * 1,2,3,4} advertise
bind cron - {*/10 18 * * *} advertise
Not 100% sure so give this a try. :)

And in the advertise function do that unbind of the previous cron binds.
The idea would be not to use

Code: Select all

bind cron - {* * * * *} cron:semanal
and do unbind in the same process advertise ?
Something like that ?:

Code: Select all

bind cron - {*/20 4 * * 0,5,6} advertise
bind cron - {*/20 0 * * 1,2,3,4} advertise
bind cron - {*/10 18 * * *} advertise

proc advertise { nick uhost hand chan arg } {
unbinds
#more stuff....#
}

proc unbinds { } {
   foreach ele [binds cron] {
        foreach {type flags name hits proc} $ele {
                if {[string match "advertise" $proc]} {
                  unbind $type $flags $name $proc
                  break
                }
      }
   }
} 
This does not unbind me the same cron I'm activating?
How I can make the code distinguishes the previous cron binds and not unbind the current cron I'm activating?

I thought of something to distinguish by the number of hits, if value of hits is zero, it is the current process and performed a break and return in the process unbinds.
It might work or is there a better way to do this?

Code: Select all

proc unbinds { } {
   foreach ele [binds cron] {
        foreach {type flags name hits proc} $ele {
                if {($proc eq "advertise") && ($hits == 0)}  { break;return }
                if {($proc eq "advertise") && ($hits != 0)} {
                  unbind $type $flags $name $proc
                  break
                }
      }
   }
} 
EDIT:
My idea will not work for the following reason:
The first time the "unbinds" and "advertise" process run smoothly.
But the second time the number of hits of that process will have value 1 and unbind the current bind cron.
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Fixed

Post by juanamores »

I fixed.
I returned to cron:weekly but with much less lines and working properly. :D

Code: Select all

bind cron - {* * * * *} cron:weekly 


proc cron:weekly {min hour day month weekday} {
	if {($min == "50" && $hour == "23") && ($weekday == "01" || $weekday == "02" || $weekday == "03" || $weekday == "04")} \
   {unbinds;bind cron - {*/10 0 * * 1,2,3,4} advertise}
	if {($min == "50" && $hour == "03") && ($weekday == "00" || $weekday == "05" || $weekday == "06")} \
   {unbinds;bind cron - {*/20 4 * * 0,5,6} advertise }
	if {($min == "50" && $hour == "17") && ($weekday == "01" || $weekday == "02" || $weekday == "03" || $weekday == "04")} \
   {unbinds;bind cron - {*/10 18 * * *} advertise }
}

proc unbinds { } {
   foreach ele [binds cron] {
        foreach {type flags name hits proc} $ele {
                if {[string match "advertise" $proc]} {
                unbind $type $flags $name $proc
                putlog "UNBIND $type $flags $name $proc"
                 break
                }
        }
   }
}  
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
Post Reply