Cron is an application for running programs on a regular basis, but its configuration file is edited using a command called crontab.
You can have a crontab for each user. If you edit the crontab as a user named myapp, it becomes myapp's crontab.
Use the "-e" option to edit the crontab.
The editor will start.
If you want to switch the editor to use, execute the select-editor command.
How to write crontab
With crontabe, you can specify minutes, hours, days, months, and weeks to run the program on a regular basis.
Minute hour day month week command
For example, if you want to run it every 15 minutes, write:
15 * * * * foo.pl
The minimum unit of execution is 1 minute.
"*" Means each unit. If "*" is specified at the minute position, it means every minute.
If you run it every minute, it will be as follows.
* * * * * foo.pl
It is also possible to specify multiple items by separating them with commas.
15,30,45 * * * * foo.pl
You can use the "/" symbol to write something like every 15 minutes.
* / 15 * * * * foo.pl
The crontab sample is detailed below.
Use user Perl with crontab
One of the highlights of crontab is that user Perl installed with perlbrew or plenv will not be used by cron.
This is because when running from cron, the shell is not started, so ".bashrc" and ".bash_profile" that describe the settings of perlbrew and plenv are not read.
The simplest solution is to rewrite PATH in crontab to the value of the environment variable PATH that the user has.
First, as the user who installed perlbrew, display the value of the PATH environment variable.
env | grep -P'^ PATH'
You will see some environment variables that include PATH, so look for something called PATH. (This is an example when the user name is myapp.)
PATH = / home / myapp / perl5 / perlbrew / bin: /home/myapp/perl5/perlbrew/perls/perl-5.20.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin : / usr / bin: / sbin: / bin: / usr / games: / usr / local / games
Edit crontab with the following command.
Then write the above PATH value to the top of crontab.
PATH = / home / myapp / perl5 / perlbrew / bin: / home / myapp / perl5 / perlbrew / perls / perl-5.16.2 / bin: / usr / kerberos / bin: / usr / local / bin: / bin: / usr / bin: / home / myapp / bin #Crontab settings continue ...
Now you can use the user Perl installed by cron.
Add the Perl library path
If you have the library path, set "PERL5LIB" as follows. This description is required when reading an application created with Mojolicious from a batch file.
PERL5LIB = / home / myapp / webapp / lib
Change the destination of emails when a cron error occurs MAILTO
Cron works by sending an email if there is output in the standard error output.
You can use MAILTO to change the destination of your email with cron . Add the following line inside the crontab .
If not set, it will be in the local mailbox of the user who ran the cron.
MAILTO = email@example.com
When setting multiple email addresses, separate them with commas.
MAILTO = firstname.lastname@example.org,email@example.com
It is convenient to set an email address because you can know the error of batch processing executed by cron.
Back up and save the contents of crontab
You can display the contents with the -l option. You can redirect and back up your contents.
crontab -l> crontab_prod
If you manage this with Git together with the source code, you can avoid the risk of losing all the crontab settings in the worst case.
Read settings from file into crontab
You can load the settings from a file into crontab by specifying a file as an argument to crontab.
crontab file name
Start / stop / restart / status check of cron, cron execution log
Please refer to the following articles for cron start / stop / restart / status check and cron execution log.