Cron (Slovenský)

From ArchWiki
Revision as of 20:44, 17 January 2011 by Rixx (Talk | contribs) (Created page with "Category:Slovenský <!-- i18n, flags --> <!-- {{i18n|cron}} (hide i18n until at least 1 translation exists --> <!-- Overview --> {{Article summary start}} {{Article summary t...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary link Template:Article summary end

Cron je plánovač úloh pre GNU/Linux a mnoho iných operačných systémov. Umožňuje automaticky opakovať úlohy spúšťaním daných príkazov v daný čas. Môže byť využitý pre široký rozsah aplikácii, od jednoduchých opakujúcich sa úloh až po zálohy systému.

Inštalácia

Existuje mnoho implementácií cron-u, ktoré sú prístupné a môžete si z nich vybrať. Template:Package Official (Dillon's Cron) je dostupný v [core] repozitáry a je inštalovaný ako súčasť base skupiny balíčkov.

# pacman -S dcron

Poprípade si môžete nainštalovať Template:Package Official z [community] alebo Template:Package AUR alebo Template:Package AUR z AUR; všetky poskytujú široký rozsah funkcií a možností konfigurácie.

Na Gentoo Linux Cron Guide nájdete viacej porovnaní medzi spomenutými implementáciami.

Úvodná konfigurácia

Používatelia a autoštart

Cron môže pre väčšinu užívateľov pracovať "out-of-box". Ak chcete používať crontab, používatelia musia byť členom určenej skupiny, ale v Arch-u je táto skupina users, ktorej členom by mali byť všetci používatelia. Ak by z nejakých dôvodov užívatelia neboli členmi danej skupiny, môžeme ich pridať nasledujúcim príkazom:

# gpasswd -a username users

a mali by bať schopný editácie svojich vlastných crontab-ov.

Aby ste sa uistili, že cron sa bude spúšťať pri štarte systému, pridajte crond do riadku DAEMONS v Template:Filename.


Chyby úloh

Počas spúšťania daných úloh sa môžu vyskytnúť chyby. Keď sa to stane, cron zaregistruje stderr výstup úlohy a znaží sa ho poslať defaultne cez sendmail. Na log-ovanie týchto správ môžete použiť prepínač -M pre cornd a napísať si svoj vlastný skript alebo nainštalovať základný SMTP subsystém

# pacman -S esmtp procmail

Po inštalácii si vytvorte súbor Template:Filename s týmto obsahom:

 mda "/usr/bin/procmail -d %T"

Otestujte pomocou:

$ sendmail user_name < message.txt
$ cat /var/spool/mail/user_name

Všetky chybové výstupy budú presmerované do Template:Filename Thats all! All error output of jobs now will be redirected to Template:Filename

Formát zápisu do crontab

Základný formát je:

<minute> <hour> <day_of_month> <month> <day_of_week> <command>
  • minute nadobúda hodnoty od 0 do 59.
  • hour nadobúda hodnoty od 0 do 23.
  • day_of_month nadpbúda hodnoty od 1 do 31.
  • month nadobúda hodnoty od 1 do 12.
  • day_of_week nadobúda hodnoty od 0 do 6, kde 0 je nedeľa.


Multiple times may be specified with a comma, a range can be given with a hyphen, and the asterisk symbol is a wildcard character. Spaces are used to separate fields. For example, the line:

*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/i_love_cron.sh

Will execute the script Template:Codeline at five minute intervals from 9 AM to 4:55 PM every weekday of the month except during the summer months (June, July, and August). More examples and advanced configuration techniques can be found below.

Basic commands

Crontabs should never be edited directly; instead, users should use the crontab program to work with their crontabs.

To view their crontabs, users should issue the command:

$ crontab -l

To edit their crontabs, they may use:

$ crontab -e

To remove their crontabs, they should use:

$ crontab -d

If a user has a saved crontab and would like to completely overwrite their old crontab, he or she should use:

$ crontab saved_crontab_filename

To overwrite a crontab from the command line (Wikipedia:stdin), use

$ crontab - 

To edit somebody else's crontab, issue the following command as root:

# crontab -u username -e

This same format (appending "-u username" to a command) works for listing and deleting crontabs as well.

Examples

The entry:

01 * * * * /bin/echo Hello, world!

runs the command Template:Codeline on the first minute of every hour of every day of every month (i.e. at 12:01, 1:01, 2:01, etc.)

Similarly,

*/5 * * jan mon-fri /bin/echo Hello, world!

runs the same job every five minutes on weekdays during the month of January (i.e. at 12:00, 12:05, 12:10, etc.)

As noted in the Crontab Format section, the line:

*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/i_love_cron.sh

Will execute the script Template:Codeline at five minute intervals from 9 AM to 5 PM (excluding 5 PM itself) every weekday (Mon-Fri) of every month except during the summer (June, July, and August).

More information

The cron daemon parses a configuration file known as crontab. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use Template:Filename or the Template:Filename directory, depending on which cron implementation they choose).

There are slight differences between the crontab formats of the different cron daemons. The default root crontab for dcron looks like this:

Template:File

These lines exemplify one of the formats that crontab entries can have, namely whitespace-separated fields specifying:

  1. @period
  2. ID=jobname (this tag is specific to dcron)
  3. command

The other standard format for crontab entries is:

  1. minute
  2. hour
  3. day
  4. month
  5. day of week
  6. command

The crontab files themselves are usually stored as Template:Filename. For example, root's crontab is found at Template:Filename

See the crontab man page for further information and configuration examples, dcron's manpages are available here and this (note: yacron removed from repos?) forum post has lots of info.

Running X apps

If you find that you can't run X apps from cron jobs then put this before the command:

export DISPLAY=:0.0 ;

That sets the DISPLAY variable to the first display; which is usually right unless you like to run multiple xservers on your machine.

If it still doesn't work then you need to use xhost to give your user control over X11:

# xhost +si:localuser:$(whoami)

I put it in my gnome `Startup Applications' like this:

bash -c "xhost +si:localuser:$(whoami)"

Asynchronous job processing

If you regularly turn off your computer but don't want to miss jobs, there are some solutions available (easiest to hardest):

Dcron 
Vanilla dcron (the default cron daemon in Arch) now supports asynchronous job processing. Just put it with @hourly, @daily, @weekly or @monthy with a jobname, like this:
@hourly         ID=greatest_ever_job      echo This job is very usefull.
Cronwhip (AUR, forum thread)
Script to automatically run missed cron's jobs; works with the default cron implementation, dcron.
Anacron (AUR)
Full replacement for dcron, processes jobs asynchronously.