Schedule Task
Define Schedule Task. Its Principle and Type
CronTAB
Getting ready
$ vagrant up $ vagrant ssh
How to do it
Set task by crontab with command line and Its Type of Syntax
For install crontab
$ sudo yum install cronie -y
Start the service
$ sudo systemctl enable crond
$ sudo systemct start crond
From the man pages crontab,it maintains crontab files for individual users.
use help for crontab
# crontab --help
crontab key option for basic
$ crontab --help
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging
Now how we create cron job
$ sudo vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
$ date
Sat May 28 05:18:20 EDT 2016
For simple print message for one user
$ sudo crontab -u vagrant -e
54 14 28 5 6 /usr/bin/echo "helllo"
:wq
$ sudo crontab -u vagrant -l
54 14 28 5 6 /usr/bin/echo "helllo"
For check mail by command line
$ sudo yum install mailx
$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/vagrant": 1 message
> 1 (Cron Daemon) Sat May 28 05:28 26/909 "Cron <vagrant@localho"
& 1
Message 1:
From [email protected] Sat May 28 05:28:01 2016
Return-Path: <[email protected]>
X-Original-To: vagrant
Delivered-To: [email protected]
From: "(Cron Daemon)" <[email protected]>
To: [email protected]
Subject: Cron <vagrant@localhost> /usr/bin/echo "helllo"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=7>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/1000>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/vagrant>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=vagrant>
X-Cron-Env: <USER=vagrant>
Date: Sat, 28 May 2016 05:28:01 -0400 (EDT)
Status: RO
helllo
&
At EOF
& q
Held 1 message in /var/spool/mail/vagrant
For Broadcast message
$ sudo crontab -u root -e
50 5 28 5 6 /usr/bin/wall "helllo"
:wq
$
Broadcast message from [email protected] (Sat May 28 05:50:01 2016):
helllo
For continuously job
$ crontab -e
*/2 9-18 * * 1-6 date >>/home/student/myjob
:wq
$ crontab -l
*/2 9-18 * * 1-6 date >>/home/vagrant/myjob
$ tail -f myjob
Sat May 28 06:26:12 EDT 2016
Sat May 28 06:32:01 EDT 2016
Sat May 28 06:34:01 EDT 2016
Sat May 28 06:36:01 EDT 2016
Sat May 28 06:38:01 EDT 2016
Sat May 28 06:40:01 EDT 2016
For create custom cron job by script
$ sudo vim /root/abc.sh
echo "Backup`date`"
tar cvf /home/vagrant/abc.tar /home
cp /etc/fstab /home/vagrant/
yum install httpd -y
:wq
$ sudo crontab -u root -e
59 5 28 5 6 . /root/abc.sh
:wq
$ sudo crontab -u root -e
59 5 28 5 6 . /root/abc.sh
$ sudo mail
delete the job
$ sudo crontab -u vagrant -r
How it work
NTP(Network Time Protocol)
Getting ready
$ vagrant up
$ vagrant ssh
How to do it
From the man pages chronyd chrony background daemon.
Install NTP
$ sudo yum install chrony
Start the service NTP
$ sudo systemctl enable chronyd
$ sudo systemctl start chronyd
Set NTP with command line for global time.
$ vim /etc/chrony.conf
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server centos7server iburst
$ sudo systemctl restart chronyd
$ chronyc sources -V
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ns1.fibergrid.in 2 6 17 31 +8939us[ +17ms] +/- 145ms
^+ 125.62.193.121 2 6 17 31 -13ms[ -13ms] +/- 232ms
^+ 123.108.200.124 3 6 17 31 -29ms[ -29ms] +/- 239ms
^* ns2.fibergrid.in 2 6 17 31 +16ms[ +25ms] +/- 149ms
How it work
At
Getting ready
$ vagrant up
$ vagrant ssh
How to do it
From the man pages at, it queue, examine or delete jobs for later execution.
Install at
$ sudo yum install at
Start the service
$ sudo systemctl enable atd
$ sudo systemctl start atd
$ sudo systemctl status atd
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled)
Active: active (running) since Sat 2016-05-28 07:53:16 EDT; 5s ago
Main PID: 16434 (atd)
CGroup: /system.slice/atd.service
└─16434 /usr/sbin/atd -f
May 28 07:53:16 localhost.localdomain systemd[1]: Started Job spooling tools.
Use at with Command line For task
$ at now +2min
at> tar czvf /home/abc.gz.tar /home
at> <EOT>
job 1 at Sat May 28 07:54:00 2016
$ at now +3 min
at> touch /hom/vagrant abc.txt
at> <EOT>
job 2 at Sat May 28 07:58:00 2016
$ atq
1 Sat May 28 07:54:00 2016 a root
2 Sat May 28 07:58:00 2016 a root
$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/vagrant": 3 messages 2 new
1 (Cron Daemon) Sat May 28 05:28 26/909 "Cron <vagrant@localhost> /usr/bin/echo "helllo""
>N 2 root Sat May 28 07:54 27/846 "Output from your job 1"
N 3 root Sat May 28 07:58 14/570 "Output from your job 2"
&
$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/vagrant": 3 messages 1 unread
1 (Cron Daemon) Sat May 28 05:28 26/909 "Cron <vagrant@localhost> /usr/bin/echo "helllo""
2 root Sat May 28 07:54 28/857 "Output from your job 1"
>U 3 root Sat May 28 07:59 15/580 "Output from your job 2"
& 2
Message 2:
From [email protected] Sat May 28 07:54:00 2016
Return-Path: <[email protected]>
X-Original-To: vagrant
Delivered-To: [email protected]
Subject: Output from your job 1
To: [email protected]
Date: Sat, 28 May 2016 07:54:00 -0400 (EDT)
From: [email protected] (root)
Status: RO
tar: Removing leading `/' from member names
/home/
/home/vagrant/
/home/vagrant/.bash_logout
/home/vagrant/.bash_profile
/home/vagrant/.bashrc
/home/vagrant/.ssh/
/home/vagrant/.ssh/authorized_keys
/home/vagrant/.ssh/known_hosts
/home/vagrant/.bash_history
/home/vagrant/.viminfo
/home/vagrant/abc.tar
/home/vagrant/fstab
/home/vagrant/myjob
&
$ ls /home/
abc.gz.tar vagrant