Jump to content

Cron jobs


Recommended Posts

Quick question...

 

How do I make a cron job to repeat every 4 weeks as opposed to the same day each week/month.

 

Thanks.

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

I'm using DirectAdmin to maintain my crons (see screenshot below)

 

At the moment I have to change those dates in the cron manually. I'm not so fussed by the filename as I can happily live with yymmdd-showname-showname.mp3 as it'll be edited before the next time it airs anyway.

 

How would I set that to increment and activate on the 28th run?

 

3eSCKgp.png

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

Have it run one day per week on the day you want it to run.

The first part of the code should look like this:

 

$date = json_decode(file_get_contents("weekcount.json"),true);
if($date['count'] < 4){
$date['count']++;
file_put_contents("weekcount.json", json_encode($date['count']));
}
if($date['count'] == 4){
$date['count'] = 0;
file_put_contents("weekcount.json", json_encode($date['count']));
...
}
}

 

Not sure if that will compile, I just did it from memory. Basically, keep the # of weeks in a file and read it and update it as necessary.

Link to comment
Share on other sites

Well I was replying to this topic before everything died, so here goes.

 

You can kind of do this with raw cron jobs but you also kind of can't.

 

Cron jobs can run every 28 days, but they always have to have a base point to start with relative to the current month (in terms of days).

 

So, for example, a cron job set to run at:

* * 1/28 * *

 

It will run every 28 days, relative to the first day of the month. So it will run on the 1st and 29th of each month, which isn't quite what you want. Unfortunately you just can't do this, so you would need an addition to your script to do it for you.

Link to comment
Share on other sites

Bugger. I was worried that would be the case. I've got about 80 crons set up to record live airings of radio shows at specific times and save them to a folder on the webserver. However as we have 4 weekly schedule it means every few weeks I need to make sure I reset the dates on the crons again for the next live show. This works but occasionally I forget and then I need to snag the recording from one of the two backup locations which requires physical access to the studio. I'd rather do this maintenance remotely from my home.

 

I was hoping it was possible to just set them up once and then repeat every 28 days.

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

How about another cron job to reset the dates on the crons? :P

Is that possible? Sounds a bit obvious but it could work?

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

Could you explain the reason for needing to repeat/reset every 4 weeks?

 

Edit: I have read the whole thread, I don't understand why this solution is required if fixed date live shows are say every friday night.

Link to comment
Share on other sites

Hey Merc, here is a simpler way to do it than what has been offered so far. It's in bash, because your cronjobs were all .sh, but it should be simple to adjust to whatever language you want.

 

 


#!/bin/bash

START=`date --date="2012-12-21" +"%G%V"` #get the year and week number of the starting date. This will be in the format "201251"
TODAY=`date +"%G%V"` #get the year and week number of the current date in the same format.
let "DIFF=($TODAY-$START)%4" #Subtract the two. This leaves the number of weeks between the two dates. Then, get mod4 of it.

if [[ $DIFF == 0 ]] ; then #If the mod4 ==0, then it is a multiple of 4 weeks after the start date.
echo "Fourth Week"
#Do stuff here.,like call the script that will do the work
fi

 

A leap year might screw this up, but I'm not certain. I do know that this is accurate up until May of 2015.

 

Make sure to set the cron job up to run on the correct day of the week and this will work for you.

 

00 17 * * 5 <script>

Will run it every Friday at 5:00pm. If it happens to be the fourth week after your start date, it will run the script you tell it to.

~M

Link to comment
Share on other sites

How about another cron job to reset the dates on the crons? :P

Is that possible? Sounds a bit obvious but it could work?

 

I was just making a bad joke :P

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

Could you explain the reason for needing to repeat/reset every 4 weeks?

 

Edit: I have read the whole thread, I don't understand why this solution is required if fixed date live shows are say every friday night.

 

Let me explain a little more. We are a community radio station where everyone is a volunteer. Our schedule is quite complex.

 

There are two live shows every Wednesday (easy to make a cron) however there are also some shows on wednesday which are only on every 2 weeks or even once a month. Saturday and Sundays have live shows but those shows are only on once every 4 weeks. There is also a monday show every 4 weeks too. One off random specials during the week aren't an issue as they are not regular.

 

We run a 4 weekly schedule (http://frome.fm/wp-c...-v18-Sheet1.pdf) the ones with red backgrounds are live shows.

 

You can see my frustration :(

 

Misplacedme: I'll look into that later as I need to pop out now. :)

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

Perhaps,

 

test.cron

* * * * * /home/test/scripts/fortcronjob.sh 10 2 >> /home/test/scripts/fortcronjob.log 2>&1 && date >> /home/test/scripts/test.cron.log 2>&1

 

WT4StV1.png

 

fortcronjob.sh

dir_data="/home/test/.fortcronjob/"
if [ $# != 2 ]
then
echo "Usage: unique_id week_modulus"
exit 1
fi
instance_id=$1
instance_mod=$2
counter=0
exit_value=1
file_data="$dir_data$1"
if [ ! -e "$file_data" ]
then
echo "id:$instance_id - no data, starting from 0"
else
source "$file_data"
fi
counter=$(( $counter % $instance_mod ))
if [ $counter == 0 ]
then
echo "id:$instance_id - true"
exit_value=0
else
echo "id:$instance_id - t-$(( $instance_mod - $counter ))"
fi
counter=$(( $counter + 1 ))
echo "counter=$counter" > "$file_data"
exit $exit_value

 

mkdir ~/.fortcronjob/

edit fortcronjob.sh to refer to ~/.fortcronjob/ as an absolute path( non relative )

chmod fortcronjob.sh (I think it just needs +x for others)

Give each cronjob task a seperate id and required modulus value

Make sure you leave in the stdout/stderr redirection for the fortcronjob.sh script otherwise it'll spam your mail

* * * * * /home/test/scripts/fortcronjob.sh ID MODULUS >> /home/test/scripts/fortcronjob.log 2>&1 && COMMAND HERE

 

brb dinner

 

Edit:

 

I used a number for ID, but it can be any valid file name.

 

You'll need to implement each counter on the week it should start, else you'll need to go around editing the counter files.

 

Every 4weeks Monday = MODULUS 4, with the counter @ 0 on that week.

Every 2weeks Wednesday = MODULUS 2, with the counter @ 0 on one of the weeks.

Link to comment
Share on other sites

Wouldn't that require me to make a script for each individual show though? At the moment it's the same basic script that just runs as different times naming the file.

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

Wouldn't that require me to make a script for each individual show though? At the moment it's the same basic script that just runs as different times naming the file.

 

Same script for each show, different arguments for each cronjob.

 

We're using the && operator (command?) to process 2 commands.

script1.sh && script2.sh

If script1.sh exits with success, then it'll process script2.sh. If script1.sh exits with an error, then script2.sh will not be processed.

 

* * * * * /home/test/scripts/fortcronjob.sh ID MODULUS >> /home/test/scripts/fortcronjob.log 2>&1 && COMMAND HERE

 

Your current cron command replaces COMMAND HERE at the ende

 

Maybe:

 

Monday @ 8pm, every 4 weeks from the first monday you implement the counter.

 

0 20 * * MON * /home/test/scripts/fortcronjob.sh "soulshow_monday_every_4_weeks" 4 >> /home/test/scripts/fortcronjob.log 2>&1 && /home/test/lagain.sh "Ivor's Show" soulshow "ivor_soulshow.mp3" 70

Link to comment
Share on other sites

Thanks. Sadly I'm off to Italy for a little while but I'll run some tests on my return. I might need to set it up to record a replay (to avoid messing up a regular recording) for 4 weeks and see what happens :)

612d9da508.png

Mercifull.png

Mercifull <3 Suzi

"We don't want players to be able to buy their way to success in RuneScape. If we let players start doing this, it devalues RuneScape for others. We feel your status in real-life shouldn't affect your ability to be successful in RuneScape" Jagex 01/04/01 - 02/03/12

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.