cron not running as scheduled
Hi there and thanks in advance for help
I've seen various posts regarding this but can't find an answer within them. The most recent run was manual 45 minutes after the scheduled run.
Cron is 0 21 * * *, should be running every night at 10pm my local time
I had this running perfectly hourly when i was at my PC, but when my PC was shut down overnight the job hadnt run.
Any advice on how to ensure the schedule persists and i dont need to keep checking it would be great
ProjectID: 1953b972-5402-489d-86ae-ce6957da4da8
30 Replies
Project ID:
1953b972-5402-489d-86ae-ce6957da4da8
You mention running hourly but your cron runs daily at 21:00 UTC?
can you provide a little more clarity here?
sorry i changed the schedule after it was running successfully hourly
okay and have you had it skip a run at 21:00 UTC?
yeah it didnt run last night so i ran it manually 45 minutes late which is why it appears successful
ive also ran it manually several times today
hmmm 21:00 utc should not be a congested time of day
i had this exact problem on a previous deployment
maybe it was a fluke, if it happens once more, report back and I will escalate this to the team
i cant find my previous ticket unfortunately, but i expect it isnt a fluke as the behaviour is identical to a 6hourly cron i deployed a few months ago
if you're easily able to see my previous tickets you might find a similar ticket
it would have been on 5510af11-7ac6-4c46-b964-e91edd728850
as a community member i have no such access
are you still trying to run a bot on a cron?
yeah i am :catPeek:
is that really bad
why??
im not a dev so probably through ignorance
it just seemed that i wanted to trigger the bot to run as i would manually at specific times through the day
bots need to run 24/7 if you want to limit when your bot responds that needs to be done through code, absolutely do not use cron for this
ok, can you tldr why that is please?
ill look into coding this properly based on the advice given previously, thanks for your time
bots are long lived processes, what kind of bot is this
it posts a random announcement based on the contents of a postgres db
how is this content put into the database?
its static
but a js script in github updates that the announcement has been made to avoid duplicate announcements
and the js script calls the bot too
does the bot do its thing and then exit?
yes, after completing the posting task, i use client.destroy()
does it exit though?
in discord i see the bot go offline, but im not sure how else i would test what youre asking
i mean you wouldnt need to test anything, you wrote the code right?
well i didnt 'exit' explicitly, but im not sure of the difference between exiting and destroying the discord client and resources
client.once('ready', async () => {
console.log('Bot is ready!');
await fetchAndPost();
client.destroy();
process.exit(0);
});
would this work better?
or should i just go back to the drawing board and figure out continuous running of the bot
i think this would be best for an in code schedular
ok brody, thank you
implemented this in my code, seems to work great after removing the external cron
let lastRunDate = null;
client.once('ready', () => {
console.log('Bot is ready!');
setInterval(checkAndRun, 60 * 1000); // Check every minute
});
client.login(BOT_TOKEN);
async function checkAndRun() {
const now = new Date();
const ninePM = new Date(now);
ninePM.setUTCHours(21, 0, 0, 0); // Set to 9pm UTC
if (now > ninePM && (!lastRunDate || lastRunDate.getDate() !== now.getDate())) {
lastRunDate = now;
await fetchAndPost();
}
}
awesome!