TheNetsky / Microsoft-Rewards-Script

Automated Microsoft Rewards script, using TypeScript, Cheerio and Playwright.
133 stars 33 forks source link

How To Schedule Run? #87

Closed royaltongue closed 2 months ago

royaltongue commented 4 months ago

I have a file run-bot.sh that contains the following:

cd ~/Documents/ms-rewards
npm run start

I've then added the following to crontab: 00 22 * * * sh /home/royaltongue/Documents/ms-rewards/run-bot.sh > /home/royaltongue/Documents/ms-rewards/cronlog.txt

However, the script never actually runs. When looking at the content of the log I created, it shows:

> microsoft-rewards-script@1.4.5 start
> node ./dist/index.js

Am I doing something wrong with setting this up?

Additionally, manually running sh /home/royaltongue/Documents/ms-rewards/run-bot.sh from a terminal runs the script fine.

gianlucalauro commented 4 months ago

In crontab just use something like: 00 22 * * * /path/to/nodejs /baseBotPath/dist/index.js

Usually the path for nodejs is /usr/bin/node. But if you use nvm you have to specify the nvm node path

royaltongue commented 4 months ago

When trying to run /usr/bin/node /home/royaltongue/Documents/ms-rewards/dist/index.js, the following gets returned:

/home/royaltongue/Documents/ms-rewards/dist/index.js:165
            const mobileSearchPoints = (await this.browser.func.getSearchPoints()).mobileSearch?.[0];
                                                                                                ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
TheNetsky commented 4 months ago

When trying to run /usr/bin/node /home/royaltongue/Documents/ms-rewards/dist/index.js, the following gets returned:

/home/royaltongue/Documents/ms-rewards/dist/index.js:165
            const mobileSearchPoints = (await this.browser.func.getSearchPoints()).mobileSearch?.[0];
                                                                                                ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

What's your node version?

royaltongue commented 4 months ago

21.6.1

mgrimace commented 3 months ago

I used 10 7,15 * * * cd /root/Microsoft-Rewards-Script && npm run start for base script just fine for daily runs, and now with docker I’m using the following script to add some randomness to the start time:

#!/bin/bash

MINWAIT=$((5*60))
MAXWAIT=$((50*60))
SLEEPTIME=$((MINWAIT+RANDOM % (MAXWAIT-MINWAIT)))
echo "sleeping for "$((SLEEPTIME / 60))"m ("$SLEEPTIME"s) ..." 
sleep $SLEEPTIME
echo "starting container"
docker start netsky

Make sure the script is executable

Set up cron to run the script daily using crontab -e and entering the following line, which should run the script at 5:30 am daily + the random sleeptime calculation above: 30 5,11 * * * /bin/bash /root/Microsoft-Rewards-Script/daily.sh

You could probably adapt the script above too for the regular script too