Linux package management and scheduled tasks, Linux package management tasks
Chapter 4 software query 1st check whether the software is installed
Rpm-qa | grep cron to check whether the software is installed.
[Root @ znix ~] # Rpm-qa | grep cron
Crontabs-1.10-33.el6.noarch
Cronie-1.4.4-16.el6_8.2.x86_64
Cronie-anacron-1.4.4-16.el6_8.2.x86_64
[Root @ znix ~] #
1.2 check whether the query is running
[Root @ znix ~] # Ps-ef | grep crond
Root 1570 1 0 Sep02? 00:00:00 crond
Root 14941 14780 0 00:00:00 pts/0 grep -- color = auto crond
1.3 How to install crond 1.3.1 where does crond belong?
Crond, which cannot be installed separately, is a feature included in the software package.
Yum needs to be installed during installation,
[Root @ znix ~] # Yum install crond
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* Base: mirrors.btte.net
* Extras: mirrors.btte.net
* Updates: mirrors.sohu.com
Base | 3.7 kB
Extras | 3.4 kB: 00
Updates | 3.4 kB: 00
No package crond available.
Error: Nothing to do
1.3.2 query the Software Package
[Root @ znix ~] # Rpm-qf 'which crond'
Cronie-1.4.4-16.el6_8.2.x86_64
1.3.3 how to view the content in the Software Package
Rpm-ql displays the information in the software package. The names must be consistent.
[Root @ znix ~] # Rpm-ql cronie
/Etc/cron. d
/Etc/cron. d/0 hourly
/Etc/cron. deny
/Etc/pam. d/crond
/Etc/rc. d/init. d/crond
/Etc/sysconfig/crond
/Usr/bin/crontab
/Usr/sbin/crond
/Usr/share/doc/cronie-1.4.4
/Usr/share/doc/cronie-1.4.4/AUTHORS
/Usr/share/doc/cronie-1.4.4/COPYING
/Usr/share/doc/cronie-1.4.4/ChangeLog
/Usr/share/doc/cronie-1.4.4/INSTALL
/Usr/share/doc/cronie-1.4.4/README
/Usr/share/man/man1/crontab.1.gz
/Usr/share/man/man5/crontab.5.gz
/Usr/share/man/man8/cron.8.gz
/Usr/share/man/man8/crond.8.gz
/Var/spool/cron
1.3.4 how to view an installed software
How does rpm query the content of a software package?
Yum cannot be installed. It may be part of the software package.
Check which software package crond belongs and use the absolute path when querying
1.4 query the software package of ifconfig
[Root @ znix ~] #
[Root @ znix ~] # Rpm-qf 'which ifconfig'
Net-tools-1.60-114.el6.x86_64
[Root @ znix ~] # Rpm-ql net-tools
/Bin/dnsdomainname
/Bin/domainname
Chapter 2 scheduled tasks 2nd what is a scheduled task
Execute commands or scripts at the same time ).
2.2 scheduled tasks in the system
[Root @ znix ~] # Ls-ld/etc/cron *
Drwxr-xr-x. 2 root 4096 Aug 10/etc/cron. d
Drwxr-xr-x. 2 root 4096 Aug 10/etc/cron. daily
-Rw -------. 1 root 0 Aug 24 2016/etc/cron. deny
Drwxr-xr-x. 2 root 4096 Aug 10/etc/cron. hourly
Drwxr-xr-x. 2 root 4096 Aug 10/etc/cron. monthly
-Rw-r --. 1 root 457 Sep 27 2011/etc/crontab
Drwxr-xr-x. 2 root 4096 Sep 27 2011/etc/cron. weekly
/Etc/cron. deny prohibit users from using scheduled tasks and put the user name in it
2.2.1 log cutting-log polling
Separate logs at intervals
[Root @ znix ~] # Ls/var/log/messages *
/Var/log/messages-20170828
/Var/log/messages-20170814/var/log/messages-20170904
/Var/log/messages-20170821
2.2.2 scheduled tasks for log polling
[Root @ znix cron. daily] # cat/etc/logrotate. d/syslog
/Var/log/cron
/Var/log/maillog
/Var/log/messages
/Var/log/secure
/Var/log/spooler
{
Sharedscripts
Postrotate
/Bin/kill-HUP 'cat/var/run/syslogd. pid 2>/dev/null' 2>/dev/null | true
Endscript
}
2.3 use crontab 2.3.1
-E Edit
-L view
-U: Set scheduled tasks for the specified user.
[Root @ znix cron. daily] # crontab -- help
Crontab: invalid option --'-'
Crontab: usage error: unrecognized option
Usage: crontab [-u user] file
Crontab [-u user] [-e |-l |-r]
(Default operation is replace, per 1003.2)
(Edit user's crontab)
(List user's crontab)
-R (delete user's crontab)
-I (prompt before deleting user's crontab)
-S (selinux context)
2.3.2 specify a user's scheduled task
-U indicates the user
[Root @ znix ~] # Crontab-u oldboy-l
# Oldboy
[Root @ znix ~] # Ll/var/spool/cron/
Total 8
-Rw ------- 1 root 8 Sep 7 :23 oldboy
-Rw ------- 1 root 77 Sep 4 20:43 root
2.4 use of scheduled tasks-2.4.1 check whether the software of the scheduled task can be used
[Root @ znix ~] # Ps-ef | grep cron
Root 1570 1 0 Sep02? 00:00:00 crond
[Root @ znix ~] # Service crond status
Crond (pid 1570) is running...
[Root @ znix ~] #/Etc/init. d/crond status
Crond (pid 1570) is running...
2.4.2 check whether the scheduled task is automatically started upon startup
[Root @ znix ~] # Chkconfig | grep cron
Crond 0: off 1: off 2: on 3: on 4: on 5: on 6: off
2.4.3 linux daemon (service)
[Root @ znix ~] # Chkconfig | egrep "crond | network | rsyslog | sshd | sysstat"
Crond 0: off 1: off 2: on 3: on 4: on 5: on 6: off scheduled task
Network 0: off 1: off 2: on 3: on 4: on 5: on 6: off network SERVICE
Rsyslog 0: off 1: off 2: on 3: on 4: on 5: on 6: off System Log
Sshd 0: off 1: off 2: on 3: on 4: on 5: on 6: off remote connection Server
Sysstat 0: off 1: on 2: on 3: on 4: on 5: on 6: off monitoring system performance and efficiency
2.5 timing task-related files 2.5.1 timing task logs
[Root @ znix ~] # Tail-2/var/log/cron
Sep 7 10:23:42 znix crontab [15409]: (root) LIST (oldboy)
Sep 7 10:25:01 znix CROND [15412]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2> & 1)
2.5.2 directory of the scheduled task
[Root @ znix ~] # Ls/var/spool/cron
2.5.3 prohibit users from using scheduled tasks
[Root @ znix ~] # Cat/etc/cron. deny
2.6 command logs viewed in linux
When viewing large files, try not to cat them. Use other commands to quickly find the desired content.
Grep/egrep
Less
Tail/tail-f
Head
2.7 basic format of scheduled tasks
* *** CMD
CMD
* Indicates that
/N Interval
2.7.1 time in a scheduled task
[Root @ znix ~] # Cat/etc/crontab
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
2.7.2 scheduled task-Synchronize the system time every five minutes
1. What command is used? First, test the command. If there is no error, write it to the scheduled task.
[Root @ znix ~] # Ntpdate ntp1.aliyun.com
7 Sep 10:52:57 ntpdate [15460]: adjust time server 182.92.12.11 offset 0.479762 sec
2. Write a scheduled task-use an absolute path to write a scheduled task. Add 2> & 1 to the end to indicate that the task is correct. All errors are output to the same file.
[Root @ znix ~] # Crontab-e
### Time tongbu
*/5 */usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2> & 1
3. Check the result. You can view the log or the command execution result.
[Root @ znix ~] # Tail-1/var/log/cron
Sep 7 10:55:01 znix CROND [15466]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2> & 1)
Instance 2-1 append its name to/oldboy/oldboy.txt every minute
* ***/Bin/echo clsn>/oldboy/oldboy.txt
Run the tail command to view the command execution status. The file content is displayed.
[Root @ znix ~] # Tail-f/oldboy/oldboy.txt
Clsn
Clsn
Clsn
View the log and view the command execution time.
[Root @ znix ~] # Tail-3/var/log/cron
Sep 7 12:05:01 znix CROND [15674]: (root) CMD (/bin/echo clsn>/oldboy/oldboy.txt 2> & 1)
Sep 7 12:06:01 znix CROND [15678]: (root) CMD (/bin/echo clsn>/oldboy/oldboy.txt 2> & 1)
Sep 7 12:07:01 znix CROND [15681]: (root) CMD (/bin/echo clsn>/oldboy/oldboy.txt 2> & 1)
2.8 note
Error:
* ***/Bin/echo "oldboy">/oldboy/oldboy.txt>/dev/null 2> & 1
Instance 2-2 cannot be used at the same time.
Commands/scripts in scheduled tasks are directed to null
/Dev/null 2> & 1
Commands/scripts in scheduled tasks are appended to files.
/Oldboy/oldboy.txt 2> & 1