Sep 09 2016

3  6K+ {{}}
# What is Cron?
Cron is a time-based job scheduler in Unix-like computer operating systems. People use Cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
# What is a crontab?
Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. Crontab is the program used to install, deinstall or list the tables to drive the cron daemon. Each user can have their own crontab.
# How does a cron job look like?
* * * * * command

- where:
#1 * is: minutes between 0-59
#2 * is: hours between 0-23
#3 * is: day of the month between 1-31
#3 * is: month between 1-12
#5 * is: day of the week between 0-6 (0 means Sunday) or you can use the abbreviated day names instead of numbers: mon, tue, wed, thu, fri, sat, sun
# Okies, enough with the text book. Let's populate our crontab with cron job entries. First, we have to open our crontab for editing:
crontab -e

Note: If we have executed the command above for the first time, our system will create a default crontab for us and will ask to choose a text editor to operate with:
1. /bin/nano <---- easiest
2. /usr/bin/emacs24
3. /usr/bin/jmacs
4. /usr/bin/joe
5. /usr/bin/jpico
6. /usr/bin/jstar
7. /usr/bin/rjoe
8. /usr/bin/vim.basic
9. /usr/bin/vim.tiny
Choose a number and hit enter. I recommend you to stick with nano (1) since it really is the easiest among our options.
Okay, so we have our crontab and it's open for editing. Read and comprehend the commented lines below.
# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command
Now we have to enter our cron jobs at the end of the file.
Note that our lines don't start with a #.
# All right, so lets enter a job which checks if our ZNC is running in every ten minutes.
0,10,20,30,40,50 * * * * znc >/dev/null 2>&1

I don't want to go into details about what ">/dev/null 2>&1" means here. In a nutshell, the output (see: man stdout) of our cron jobs are normally sent to the crontab's owner via email to a given path. Here we are simply redirecting the output to /dev/null. If you are interested about what /dev/null does read more about it here:
# If we want to shorten our cron job line above a bit, we can use the one below instead:
*/10 * * * * znc >/dev/null 2>&1

- where */10 does the same as 0,10,20,30,40,50.
- */10 * * * * literally means every ten minutes in every hour, every day of the month, every month of the year and every day of the week, while 0,10,20,30,40,50 * * * * means the 0th, the 10th, the 20th, the 30th the 40th and the 50th minute of every hour, every day of the month, every month of the year and every day of the week.
# If we want to use a locally compiled ZNC instead of the one being preinstalled on xShellz, we have to provide the absolute path to it or else our cron job will start /usr/bin/znc by default:
*/10 * * * * /home/our_username/znc/znc >/dev/null 2>&1

# Also, since xShellz is using Vixie Cron daemon, we can use nonstandard predefined scheduling definitions like:
@hourly znc >/dev/null 2>&1
(runs hourly, equal to: 0 * * * *)
@daily znc >/dev/null 2>&1
(runs daily, equal to: 0 0 * * *)
@weekly znc >/dev/null 2>&1
(runs weekly, equal to: 0 0 * * 0)
@monthly znc >/dev/null 2>&1
(runs monthly, equal to: 0 0 1 * *)
@reboot znc >/dev/null 2>&1
(runs at startup)
# Let's see another example, now with Eggdrop.
*/10 * * * * cd /home/our_username/eggdrop && ./eggdrop oureggconfigfile.conf >/dev/null 2>&1

Note: Here we have entered our locally compiled Eggdrop, since it's better using our own installation instead of the one already on the system. Why? If we use the preinstalled Eggdrop we will experience complications regarding file paths when we want to use custom modules and scripts. How do we compile and configure Eggdrop from source? Well, that's another story. Check the forum for tutorials regarding that stuff.
# Let's see another example, now with Eggdrop. - Method 2: Using the autobotchk script
After we set up our Eggdrop and it's capable to run, issue the following commands one after another:
cd ~/eggdrop

cp scripts/autobotchk ~/eggdrop/

chmod u+x autobotchk

./autobotchk eggdrop.conf -noemail -10

This will setup crontab to run the botchk every 10 minutes and also to not send you e-mail saying that it restarted your bot.
That's all folks.
# References:
man cron & man crontab (c) Paul Vixie, Steve Greenland, Javier Fernandez-Sanguino, Christian Kastner
Kudos trttl! That was very exhaustive and newbie-friendly. Good job!
Crucio    Sep 10 2016
Really helpful tutorial.
Staff StephenS    Sep 15 2016
Great job trttl, if I haven't said so already :D
saapete    Oct 27 2016

Please Login or Sign Up to leave a reply.