Have you ever striven to get the Fanatic gold badge on stack overflow? OR Has your progress reset when you were on the 99th streak? OR Have you ever wanted to automate your browsers to open a website periodically?
THEN, this article might help you !!
Introduction
The Fanatic badge is earned by visiting the stack overflow website consecutively for 100 days.
Problem
Visiting the stack overflow website routinely can prove difficult if you have no need to visit the website for just one day, or when you are on a code break / vacation.
Solution
I solved this issue by creating a cron job that opens my stack overflow profile while my system is on.
I will provide two solutions for Mac OS Monterey (here) and Ubuntu 20.04 Linux OS (here) respectively because I have tested the scripts on these platforms.
Let’s Code
# Mac OS
- Create an alias for your browser in terminal (I use Google Chrome) and save this in ~/.zshrc terminal configuration file
$ echo alias chrome="open -a 'Google Chrome'" >> ~/.zshrc
- Create a sh script that opens your stack overflow profile (or opens any website you want) and save as
cronjob.sh
.
2.5. Run zsh cronjob.sh
and make sure it opens up your stack overflow page on Google Chrome.
- Define and set the cron job
a. The below command opens a list of cron jobs in vim where each line represents a cron job
$ crontab -e
b. Press
i
to enter insert mode and on a new line enter:
0 01,11,23 * * * zsh /path/to/script/cronjob.sh
-
** Note: Change
/path/to/script/cronjob.sh
to the absolute path of the script file created in (step 2) . -
The above command can be interpreted in the following way:
0 - 0 minutes
01,11,23 - 1, 11 and 23 hours respectively # my active times
* - all days of the month (1-31)
* - all months (1-12)
* - all days of the week (0-6)
# this cron time config is followed by the command to be run
c. Press
Esc
to exit from insert mode and press:wq
to save and quit the vim environment. \
d. You should get a message like this:
crontab: installing new crontab
e. You can check your cron jobs by entering the following command
$ crontab -l # displays list of cron jobs
# Ubuntu
The default browser of Ubuntu 20.04 is Firefox BUT I will be using Google Chrome because of errors i encountered while running Firefox from cron
, also, the default shell on Linux is bash
. The commands to enter are basically similar with those entered above for Mac OS. But I’ll reiterate for clarity and reference purposes.
- Test google-chrome, cron and bash
$ google-chrome https://stackoverflow.com
# this should open your page BUT if it does not install chrome
# with this guide: https://itsfoss.com/install-chrome-ubuntu/
$ crontab -l # this should list your cron jobs
$ bash --version # prints out your bash version
- Create a bash script to open Google Chrome and save as
cronjob.sh
2.5. Run bash cronjob.sh and make sure it opens up your stack overflow page on Google Chrome.
- Define and set the cron job
$ crontab -e
# you might be prompted to select an editor, pick you favorite
- Enter on a new line the following command
0 01,11,23 * * * bash /path/to/cronjob.sh
- The above command can be interpreted in the following way:
0 - 0 minutes
01,11,23 - 1, 11 and 23 hours respectively # my active times
* - all days of the month (1-31)
* - all months (1-12)
* - all days of the week (0-6)
# this cron time config is followed by the command to be run
- You should get a message like this:
crontab: installing new crontab
- You can check your cron jobs by entering the following command
$ crontab -l # displays list of cron jobs
Conclusion
Cron jobs are really cool and powerful when properly used. Also, they are easy to setup and manage. You can run your .py, .swift, .java scripts (not just shell scripts) from cron.
There is a catch anyways.
Cron jobs would not wake your system from sleep mode. So you have to use a cron time configuration that best suits you i.e. when your system is going to be switched on. Read more on creating cron jobs at crontab.guru.
UPDATE
Today is the 4th of July and I’ve gotten the badge, use CRON job today!😌
Cheers🥂🎉