Mengotomatiskan Tugas Sistem
Tugas, juga dikenal sebagai jobs, dapat dikonfigurasi untuk berjalan secara otomatis dalam jangka waktu tertentu, pada tanggal tertentu, atau ketika rata-rata beban sistem menurun di bawah 0,8.
Fedora diprakonfigurasi untuk menjalankan tugas-tugas sistem penting untuk menjaga sistem diperbarui. Misalnya, basis data slocate yang digunakan oleh perintah locate diperbarui setiap hari. Administrator sistem dapat menggunakan tugas otomatis untuk melakukan pencadangan berkala, memantau sistem, menjalankan skrip khusus, dan sebagainya.
Fedora dilengkapi dengan utilitas tugas otomatis berikut: cron, anacron, at, dan batch.
Setiap utilitas dimaksudkan untuk menjadwalkan jenis pekerjaan yang berbeda: sementara Cron dan Anacron menjadwalkan pekerjaan berulang, At dan Batch menjadwalkan pekerjaan satu kali (masing-masing mengacu pada Cron dan Anacron dan At dan Batch).
Fedora mendukung penggunaan systemd.timer
untuk melaksanakan pekerjaan pada waktu tertentu. Lihat man systemd.timer(5)
untuk informasi lebih lanjut.
Cron dan Anacron
Both Cron and Anacron are daemons that can schedule execution of recurring tasks to a certain point in time defined by the exact time, day of the month, month, day of the week, and week.
Pekerjaan Cron dapat berjalan sesering setiap menit. Namun, utilitas mengasumsikan bahwa sistem berjalan terus menerus dan jika sistem tidak menyala pada saat pekerjaan dijadwalkan, pekerjaan tidak dijalankan.
On the other hand, Anacron remembers the scheduled jobs if the system is not running at the time when the job is scheduled. The job is then executed as soon as the system is up. However, Anacron can only run a job once a day.
Memasang Cron dan Anacron
Untuk memasang Cron dan Anacron, Anda perlu memasang paket cronie untuk Cron dan paket cronie-anacron untuk Anacron (cronie-anacron adalah sub-paket cronie).
Untuk menentukan apakah paket sudah dipasang pada sistem Anda, jalankan perintah berikut:
rpm -q cronie cronie-anacron
Perintah mengembalikan nama lengkap paket cronie dan cronie-anacron jika sudah terpasang, atau memberi tahu Anda bahwa paket tidak tersedia.
Untuk memasang paket ini, gunakan perintah dnf dalam bentuk berikut sebagai root
:
dnf install package
Misalnya, untuk memasang Cron dan Anacron, ketik yang berikut di shell prompt:
~]# dnf install cronie cronie-anacron
For more information on how to install new packages in Fedora, see Installing Packages.
Menjalankan Layanan Crond
Pekerjaan cron dan anacron keduanya dipilih oleh layanan crond
. Bagian ini memberikan informasi tentang cara memulai, menghentikan, dan memulai ulang layanan crond
, dan menunjukkan cara mengonfigurasinya untuk memulai secara otomatis pada waktu boot.
Starting and Stopping the Cron Service
Untuk menentukan apakah layanan berjalan, gunakan perintah berikut:
systemctl status crond.service
Untuk menjalankan layanan crond
di sesi saat ini, ketik yang berikut di shell prompt sebagai root
:
systemctl start crond.service
Untuk mengonfigurasi layanan agar memulai secara otomatis pada waktu boot, gunakan perintah berikut sebagai root
:
systemctl enable crond.service
Mengonfigurasi Pekerjaan Anacron
Berkas konfigurasi utama untuk menjadwalkan pekerjaan adalah berkas /etc/anacrontab
, yang hanya dapat diakses oleh pengguna root
. Berkas berisi yang berikut:
SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
Tiga baris pertama menentukan variabel yang mengkonfigurasi lingkungan di mana tugas anacron berjalan:
-
SHELL
- lingkungan shell yang digunakan untuk menjalankan pekerjaan (dalam contoh, shell Bash) -
PATH
- path ke program yang dapat dieksekusi -
MAILTO
- nama pengguna yang menerima keluaran dari pekerjaan anacron melalui surelJika variabel
MAILTO
tidak ditentukan (MAILTO=
), surel tidak dikirim.
Dua variabel berikutnya memodifikasi waktu yang dijadwalkan untuk pekerjaan yang ditentukan:
-
RANDOM_DELAY
- jumlah maksimum menit yang akan ditambahkan ke variabelpenundaan dalam menit
yang ditentukan untuk setiap pekerjaanNilai penundaan minimum ditetapkan, secara baku, ke 6 menit.
Jika
RANDOM_DELAY
, misalnya, diatur ke12
, maka antara 6 dan 12 menit ditambahkan kepenundaan dalam menit
untuk setiap pekerjaan di anacrontab tertentu.RANDOM_DELAY
juga dapat diatur ke nilai di bawah6
, termasuk0
. Saat diatur ke0
, tidak ada penundaan acak yang ditambahkan. Ini terbukti berguna ketika, misalnya, lebih banyak komputer yang berbagi satu koneksi jaringan perlu mengunduh data yang sama setiap hari. -
START_HOURS_RANGE
— interval, ketika pekerjaan terjadwal dapat dijalankan, dalam jamJika interval waktu terlewatkan, misalnya karena kegagalan daya, pekerjaan yang dijadwalkan tidak dieksekusi hari itu.
Baris yang tersisa dalam berkas /etc/anacrontab
mewakili pekerjaan terjadwal dan mengikuti format ini:
periode dalam hari tundaan dalam menit pengenal kerja perintah
-
periode dalam hari
— frekuensi eksekusi pekerjaan dalam hariNilai properti dapat didefinisikan sebagai bilangan bulat atau suatu makro (
@daily
,@weekly
,@monthly
), di mana@daily
menunjukkan nilai yang sama dengan bilangan bulat 1,@weekly
sama dengan 7, dan@monthly
menentukan bahwa pekerjaan dijalankan sebulan sekali terlepas dari panjang bulan. -
tundaan dalam menit
— berapa menit anacron menunggu sebelum melaksanakan pekerjaanNilai properti didefinisikan sebagai bilangan bulat. Jika nilai diatur ke
0
, tidak ada penundaan yang berlaku. -
job-identifier
— nama unik yang mengacu pada pekerjaan tertentu yang digunakan dalam berkas log -
perintah
— perintah yang akan dieksekusiPerintah dapat berupa perintah seperti ls /proc >> /tmp/proc atau perintah yang mengeksekusi skrip ubahan.
Setiap baris yang dimulai dengan tanda hash (#) adalah komentar dan tidak diproses.
Examples of Anacron Jobs
Contoh berikut menunjukkan berkas /etc/anacrontab
sederhana:
SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=30 # the jobs will be started during the following hours only START_HOURS_RANGE=16-20 #period in days delay in minutes job-identifier command 1 20 dailyjob nice run-parts /etc/cron.daily 7 25 weeklyjob /etc/weeklyjob.bash @monthly 45 monthlyjob ls /proc >> /tmp/proc
Semua pekerjaan yang ditentukan dalam berkas anacrontab
ini ditunda secara acak selama 6-30 menit dan dapat dieksekusi antara pukul 16:00 dan 20:00.
The first defined job is triggered daily between 16:26 and 16:50 (RANDOM_DELAY is between 6 and 30 minutes; the delay in minutes
property adds 20 minutes). The command specified for this job executes all present programs in the /etc/cron.daily/
directory using the run-parts script (the run-parts scripts accepts a directory as a command-line argument and sequentially executes every program in the directory). See the run-parts
man page for more information on the run-parts script.
The second job executes the weeklyjob.bash
script in the /etc/
directory once a week.
The third job runs a command, which writes the contents of /proc
to the /tmp/proc
file (ls /proc >> /tmp/proc) once a month.
Mengonfigurasi Pekerjaan Cron
The configuration file for cron jobs is /etc/crontab
, which can be only modified by the root
user. The file contains the following:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
The first three lines contain the same variable definitions as an anacrontab
file: SHELL
, PATH
, and MAILTO
. For more information about these variables, see Configuring Anacron Jobs.
In addition, the file can define the HOME
variable. The HOME
variable defines the directory, which will be used as the home directory when executing commands or scripts run by the job.
The remaining lines in the /etc/crontab
file represent scheduled jobs and have the following format:
minute hour day month day of week username command
The following define the time when the job is to be run:
-
minute
— any integer from 0 to 59 -
hour
— any integer from 0 to 23 -
day
— any integer from 1 to 31 (must be a valid day if a month is specified) -
month
— any integer from 1 to 12 (or the short name of the month such as jan or feb) -
day of week
— any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)
The following define other job properties:
-
username
— specifies the user under which the jobs are run. -
command
— the command to be executed.The command can be either a command such as ls /proc /tmp/proc or a command which executes a custom script.
For any of the above values, an asterisk (*) can be used to specify all valid values. If you, for example, define the month value as an asterisk, the job will be executed every month within the constraints of the other values.
A hyphen (-) between integers specifies a range of integers. For example, 1-4
means the integers 1, 2, 3, and 4.
A list of values separated by commas (,) specifies a list. For example, 3,4,6,8
indicates exactly these four integers.
The forward slash (/) can be used to specify step values. The value of an integer will be skipped within a range following the range with /integer
. For example, the minute value defined as 0-59/2
denotes every other minute in the minute field. Step values can also be used with an asterisk. For instance, if the month value is defined as */3
, the task will run every third month.
Setiap baris yang dimulai dengan tanda hash (#) adalah komentar dan tidak diproses.
Users other than root
can configure cron tasks with the crontab utility. The user-defined crontabs are stored in the /var/spool/cron/
directory and executed as if run by the users that created them.
To create a crontab as a specific user, login as that user and type the command crontab -e to edit the user’s crontab with the editor specified in the VISUAL
or EDITOR
environment variable. The file uses the same format as /etc/crontab
. When the changes to the crontab are saved, the crontab is stored according to the user name and written to the file /var/spool/cron/username
. To list the contents of the current user’s crontab file, use the crontab -l command.
The /etc/cron.d/
directory contains files that have the same syntax as the /etc/crontab
file. Only root
is allowed to create and modify files in this directory.
Do not restart the daemon to apply the changes
The cron daemon checks the |
Mengontrol Akses ke Cron
To restrict the access to Cron, you can use the /etc/cron.allow
and /etc/cron.deny
files. These access control files use the same format with one user name on each line. Mind that no whitespace characters are permitted in either file.
If the cron.allow
file exists, only users listed in the file are allowed to use cron, and the cron.deny
file is ignored.
If the cron.allow
file does not exist, users listed in the cron.deny
file are not allowed to use Cron.
The Cron daemon (crond
) does not have to be restarted if the access control files are modified. The access control files are checked each time a user tries to add or delete a cron job.
The root
user can always use cron, regardless of the user names listed in the access control files.
You can control the access also through Pluggable Authentication Modules (PAM). The settings are stored in the /etc/security/access.conf
file. For example, after adding the following line to the file, no other user but the root
user can create crontabs:
-:ALL EXCEPT root :cron
The forbidden jobs are logged in an appropriate log file or, when using crontab -e, returned to the standard output. For more information, see the access.conf.5
manual page.
Black and White Listing of Cron Jobs
Black and white listing of jobs is used to define parts of a job that do not need to be executed. This is useful when calling the run-parts script on a Cron directory, such as /etc/cron.daily/
: if the user adds programs located in the directory to the job black list, the run-parts script will not execute these programs.
To define a black list, create a jobs.deny
file in the directory that run-parts scripts will be executing from. For example, if you need to omit a particular program from /etc/cron.daily/
, create the /etc/cron.daily/jobs.deny
file. In this file, specify the names of the programs to be omitted from execution (only programs located in the same directory can be enlisted). If a job runs a command which runs the programs from the /etc/cron.daily/
directory, such as run-parts /etc/cron.daily, the programs defined in the jobs.deny
file will not be executed.
To define a white list, create a jobs.allow
file.
The principles of jobs.deny
and jobs.allow
are the same as those of cron.deny
and cron.allow
described in section Controlling Access to Cron.
At dan Batch
While Cron is used to schedule recurring tasks, the At utility is used to schedule a one-time task at a specific time and the Batch utility is used to schedule a one-time task to be executed when the system load average drops below 0.8.
Memasang At dan Batch
Untuk menentukan apakah paket at sudah terpasang pada sistem Anda, jalankan perintah berikut:
rpm -q at
The command returns the full name of the at package if already installed or notifies you that the package is not available.
To install the packages, use the dnf command in the following form as root
:
dnf install package
For example, to install both At and Batch, type the following at a shell prompt:
~]# dnf install at
For more information on how to install new packages in Fedora, see Installing Packages.
Menjalankan Layanan At
The At and Batch jobs are both picked by the atd
service. This section provides information on how to start, stop, and restart the atd
service, and shows how to configure it to start automatically at boot time.
Starting and Stopping the At Service
Untuk menentukan apakah layanan berjalan, gunakan perintah berikut:
systemctl status atd.service
To run the atd
service in the current session, type the following at a shell prompt as root
:
systemctl start atd.service
Untuk mengonfigurasi layanan agar memulai secara otomatis pada waktu boot, gunakan perintah berikut sebagai root
:
systemctl enable atd.service
Disarankan agar Anda mengonfigurasi sistem Anda untuk memulai layanan |
Mengonfigurasi Suatu Tugas At
Untuk menjadwalkan pekerjaan satu kali pada waktu tertentu dengan utilitas At, lakukan hal berikut:
-
Pada baris perintah, ketik perintah at TIME, di mana TIME adalah waktu ketika perintah akan dijalankan.
Argumen TIME dapat didefinisikan dalam salah satu format berikut:
-
HH:MM
menentukan jam dan menit yang tepat; Misalnya,04:00
menentukan 4:00 pagi. -
midnight
menyatakan pukul 12:00 malam. -
noon
menyatakan pukul 12:00 siang. -
teatime
menyatakan 4:00 sore. -
Format
BULANTANGGALTAHUN
; Misalnya,Januari 15 2012
menyatakan hari ke-15 di bulan Januari pada tahun 2012. Nilai tahun adalah opsional. -
MMDDYY
,MM/DD/YY
, orMM.DD.YY
formats; For example,011512
for the 15th day of January in the year 2012. -
now + TIME
where TIME is defined as an integer and the value type: minutes, hours, days, or weeks. For example,now + 5 days
specifies that the command will be executed at the same time five days from now.The time must be specified first, followed by the optional date. For more information about the time format, see the
/usr/share/doc/at-<version>/timespec
text file.If the specified time has past, the job is executed at the time the next day.
-
-
In the displayed
at>
prompt, define the job commands:-
Type the command the job should execute and press Enter. Optionally, repeat the step to provide multiple commands.
-
Enter a shell script at the prompt and press Enter after each line in the script.
The job will use the shell set in the user’s
SHELL
environment, the user’s login shell, or /bin/sh (whichever is found first).
-
-
Once finished, press Ctrl+D on an empty line to exit the prompt.
If the set of commands or the script tries to display information to standard output, the output is emailed to the user.
To view the list of pending jobs, use the atq command. See Viewing Pending Jobs for more information.
You can also restrict the usage of the at command. For more information, see Controlling Access to At and Batch for details.
Configuring a Batch Job
The Batch application executes the defined one-time tasks when the system load average decreases below 0.8.
To define a Batch job, do the following:
-
On the command line, type the command batch.
-
In the displayed
at>
prompt, define the job commands:-
Type the command the job should execute and press Enter. Optionally, repeat the step to provide multiple commands.
-
Enter a shell script at the prompt and press Enter after each line in the script.
If a script is entered, the job uses the shell set in the user’s
SHELL
environment, the user’s login shell, or /bin/sh (whichever is found first).
-
-
Once finished, press Ctrl+D on an empty line to exit the prompt.
If the set of commands or the script tries to display information to standard output, the output is emailed to the user.
To view the list of pending jobs, use the atq command. See Viewing Pending Jobs for more information.
You can also restrict the usage of the batch command. For more information, see Controlling Access to At and Batch for details.
Melihat Tugas Tertunda
To view the pending At and Batch jobs, run the atq command. The atq command displays a list of pending jobs, with each job on a separate line. Each line follows the job number, date, hour, job class, and user name format. Users can only view their own jobs. If the root
user executes the atq command, all jobs for all users are displayed.
Opsi Baris Perintah Tambahan
Opsi baris perintah tambahan untuk at dan batch mencakup yang berikut:
Opsi | Deskripsi |
---|---|
|
Baca perintah atau skrip shell dari berkas alih-alih menentukannya di prompt. |
|
Kirim surel ke pengguna saat pekerjaan selesai. |
|
Tampilkan waktu pekerjaan dijalankan. |
Mengontrol Akses ke At dan Batch
Anda dapat membatasi akses ke perintah at dan batch menggunakan berkas /etc/at.allow
dan /etc/at.deny
. Berkas kontrol akses ini menggunakan format yang sama yang mendefinisikan satu nama pengguna pada setiap baris. Ingatlah bahwa tidak ada spasi yang diizinkan di kedua berkas tersebut.
Jika berkas at.allow
ada, hanya pengguna yang tercantum dalam berkas yang diizinkan untuk menggunakan at atau batch, dan berkas at.deny
diabaikan.
Jika at.allow
tidak ada, pengguna yang terdaftar di at.deny
tidak diizinkan menggunakan at atau batch.
Daemon at (atd) tidak harus dimulai ulang jika berkas kontrol akses dimodifikasi. Berkas kontrol akses dibaca setiap kali pengguna mencoba menjalankan perintah at atau batch.
Pengguna root
selalu dapat menjalankan perintah at dan batch, terlepas dari konten berkas kontrol akses.
Sumber Daya Tambahan
Untuk mempelajari lebih lanjut tentang mengonfigurasi tugas otomatis, lihat dokumentasi terpasang berikut:
-
halaman man
cron(8)
berisi ikhtisar cron. -
halaman man
crontab
di bagian 1 dan 5:-
Halaman manual di bagian 1 berisi ikhtisar berkas
crontab
. -
Halaman man di bagian 5 berisi format untuk berkas dan beberapa contoh entri.
-
-
Halaman manual
anacron(8)
berisi ikhtisar anacron. -
Halaman manual
anacrontab(5)
berisi ikhtisar berkasanacrontab
. -
Halaman manual
run-parts(4)
berisi ikhtisar skrip run-parts. -
/usr/share/doc/at/timespec
berisi informasi terperinci tentang nilai waktu yang dapat digunakan dalam definisi pekerjaan cron. -
Halaman manual
at
berisi deskripsi at dan batch dan opsi baris perintah mereka.