Microsoft-Rewards-Farmer
Microsoft-Rewards-Farmer copied to clipboard
[NEW FEATURE] Run once a day automatically
I've made another change to the original code so that we can have the process started on a machine and run once a day starting at one hour.
The time of day at which the process should be executed and whether or not it has to be executed this way and not just once would be informed through the parameter at startup, so if this parameter is not passed it would be executed only once .
It is important to note that I have tried to put the process in a linux "cron" and it does not run correctly when started from "cron" so that is why I have included this functionality so that it is python that executes the process with the periodicity that we want.
Do you find it interesting that it includes another pull request with this new functionality?
Greetings
can u send me the new code please i need it, it keeps on being stuck at "logged in" for me
I'll be on vacation for a few days. When I come back I'll include the code, although it's really very easy what you have to add. I put here briefly the most important code points.
import schedule .......
if name == "main":
# Execute "mainProcess" everyday at 13:00
schedule.every().day.at("13:00").do(mainProcess)
# Loop
while True:
# Check if there are tasks pending to be executed
schedule.run_pending()
time.sleep(1)
Greetings
I use this as my crontab entry:
0 6,8 * * * /usr/bin/python3 /home/msbot/Microsoft-Rewards-Farmer/main.py
the entry follows the order: cron schedule python location script location
tips: use cronguru to create your preferred schedule. I run it twice daily, once at 6 am an again at 8 am as a backup to catch anything it missed the first time, for example finishing searches, etc. Note the location to main.py
will be different depending where you have it installed.
That being said, would you consider using your code + an -everyday
run argument to enable it manually? That might work for folks who prefer to do this without cron and leave the script running, e.g., python3 main.py -everyday
I use the script on a headless ubuntu server, and would prefer to have the automatic scheduling via a service, cron, or ideally a docker container.
The following batch file works nicely with Windows Scheduler to run the script as a scheduled task, assuming you're using a venv in the repo path. Just set up a new task to run daily, with the Action definition as follows:
Start a Program: cmd
Add arguments: /c <filename>.bat
Start in: <repo_path>
set ORIG_PATH=%CD%
set VENV_FOLDER=<venv_name>
set VENV_PATH=%ORIG_PATH%\%VENV_FOLDER%
call %VENV_PATH%\Scripts\activate.bat
python main.py
call %VENV_PATH%\Scripts\deactivate.bat
exit /B 0
I use this as my crontab entry:
0 6,8 * * * /usr/bin/python3 /home/msbot/Microsoft-Rewards-Farmer/main.py
the entry follows the order:
cron schedule python location script location
tips: use cronguru to create your preferred schedule. I run it twice daily, once at 6 am an again at 8 am as a backup to catch anything it missed the first time, for example finishing searches, etc. Note the location to
main.py
will be different depending where you have it installed.That being said, would you consider using your code + an
-everyday
run argument to enable it manually? That might work for folks who prefer to do this without cron and leave the script running, e.g.,python3 main.py -everyday
I use the script on a headless ubuntu server, and would prefer to have the automatic scheduling via a service, cron, or ideally a docker container.
OK, I'll try "cron" again, although in previous versions of the "Farm" project it didn't work well for me on my ubuntu. If it works well I report it and we could leave this "new feature" out. Thank you so much
OK, I'll try "cron" again, although in previous versions of the "Farm" project it didn't work well for me on my ubuntu. If it works well I report it and we could leave this "new feature" out. Thank you so much
No problem and I hope it works for you! Cron never worked well for me either with any other farm projects because, for whatever reason, the script would never properly read the account .json or log files when it ran via cron. This project seems to work for me though via cron and I'm not actually sure why.
here's a script you can use to call python3 main.py
with logging. I don't take credit for it, I've left the SO link in the comments. Its actually rather handy to use for any cronjob that isn't working since it logs all errors that would normally be sent to the terminal while the commands were entered and while the script(s) were ran
Instructions:
-
In the same location as main.py (/Microsoft-Rewards-Farmer/main.py) save the following file as something like rewardcronjob.sh.
-
edit line 41 of rewardcronjob.sh for your paths for python and main.py (its the line right after
main() {
-
in the same folder, use
crontab -e
to add something like0 9 * * * /usr/bin/bash /path/to/Microsoft-Rewards-Farmer/rewardcronjob.sh
this will run everyday at 9am (depending on time zone)
I use cronitor to monitor cronjobs so my entry looks more like this:
0 9 * * * cronitor exec morning-rewards /usr/bin/bash /path/to/Microsoft-Rewards-Farmer/rewardcronjob.sh
Don't forget to edit the first line after main() {
now u have a cronjob to run everyday at 9am and log the programs output and any errors to a file.
#! /usr/bin/bash
# -------------- cron job automatic logger code START --------------
# See my ans: https://stackoverflow.com/a/60157372/4561887
FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[-1]}")"
SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")"
SCRIPT_FILENAME="$(basename "$FULL_PATH_TO_SCRIPT")"
# Automatically log the output of this script to a file!
begin_logging() {
mkdir -p ~/cronjob_logs
# Redirect all future prints in this script from this call-point forward to
# both the screen and a log file!
#
# This is about as magic as it gets! This line uses `exec` + bash "process
# substitution" to redirect all future print statements in this script
# after this line from `stdout` to the `tee` command used below, instead.
# This way, they get printed to the screen *and* to the specified log file
# here! The `2>&1` part redirects `stderr` to `stdout` as well, so that
# `stderr` output gets logged into the file too.
# See:
# 1. *****+++ https://stackoverflow.com/a/49514467/4561887 -
# shows `exec > >(tee $LOG_FILE) 2>&1`
# 1. https://superuser.com/a/569315/425838 - shows `exec &>>` (similar)
exec > >(tee -a "$HOME/cronjob_logs/${SCRIPT_FILENAME}.log") 2>&1
echo ""
echo "====================================================================="
echo "Running cronjob \"$FULL_PATH_TO_SCRIPT\""
echo "on $(date)."
echo "Cmd: $0 $@"
echo "====================================================================="
}
# --------------- cron job automatic logger code END ---------------
# THE REST OF THE SCRIPT GOES BELOW THIS POINT.
# ------------------------------------------------------------------
main() {
/usr/bin/python3 /home/my/path/Microsoft-Rewards-Farmer/main.py
echo "= DONE."
echo ""
}
# ------------------------------------------------------------------------------
# main program entry point
# ------------------------------------------------------------------------------
begin_logging "$@"
time main "$@"
If someone wants to write the script to create this file (with the edit for proper path in main()) then add the cronjob for the created script I can make a pull request to add it to the project or it'd at least it be simple for charlesbel to add so the feature can be added quicker
I have tested the execution of the script with "cron" in ubuntu and it works correctly. I think the best solution is to run it with cron.
The steps and the modification in the file have been the following:
From command line: "crontab -e"
Once the file is open, include at the end something similar to the following:
"10 9,11,13 * * * cd /home/user/msrewards && /usr/bin/python3 main.py"
Save the file with ctrl+a and ctrl+x The included command would run the program three times, once at 9:10 a.m., once at 11:10 a.m., and once at 1:10 p.m.
If deemed appropriate, the "new feature" can be closed
All the best
I have tested the execution of the script with "cron" in ubuntu and it works correctly. I think the best solution is to run it with cron.
The steps and the modification in the file have been the following:
From command line: "crontab -e"
Once the file is open, include at the end something similar to the following:
"10 9,11,13 * * * cd /home/user/msrewards && /usr/bin/python3 main.py"
Save the file with ctrl+a and ctrl+x The included command would run the program three times, once at 9:10 a.m., once at 11:10 a.m., and once at 1:10 p.m.
If deemed appropriate, the "new feature" can be closed
All the best
Thanks for following up with this and the updated cron, I had never thought of using cd
, and &&
to get to the directory first which should solve some of the logging and potential login issues.
I'd suggest, for what its worth, to create a PR for the readme to add these instructions so they aren't buried here, I'm sure it'd be very helpful for anyone coming in.
my only suggestion to the wording would be to use the repo name as the folder and possibly generic instructions to maximize accessibility (though my suggestions may be too-much-info, so you can ignore too), e.g.,
From command line: "crontab -e"
Once the file is open, include at the end something similar to the following:
1 9,11,13 * * * cd /home/user/Microsoft-Rewards-Farmer && /usr/bin/python3 main.py
This example command would run the program three times, once at 9:10 a.m., once at 11:10 a.m., and once at 1:10 p.m.
The example follows the order: [cron schedule] [absolute path to Microsoft-Rewards-Famer folder] && [absolute path to python] main.py
- cron: use cronguru to create your preferred schedule.
- Python: use
which python
to find the python directory, it is typically/usr/bin/python3
Save the file with ctrl+a and ctrl+x
Thanks again!