Linux System Task Scheduler and system service management

Source: Internet
Author: User

One, the Linux system's task plan

The crontab command is used to submit and manage the tasks that the user needs to perform periodically, similar to scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default, and the Crond process is started automatically, and the Crond process periodically checks for the tasks to be performed every minute. If there is a task to perform, the task is automatically performed.
The operation of the Cron Task Scheduler function is done through the crontab command.
The common options are:

-U: Specify a user, the no-u option is the current user;
-E: Make planning tasks;
-L: Lists scheduled tasks;
-R: Deletes a scheduled task.

Instance:
1. Create a Scheduled task:

[[email protected] ~]# crontab -eno crontab for root - using an empty one

Using CRONTAB-E to write a task plan, which is actually using the VIM tool to open the Crontab configuration file, we write the following:

01 10 05 02 1 echo "OK" > /root/crob.log

The numbers for each field are represented from left to right: minutes, hours, days, months, weeks, and the command line. The above example means: 10:01 on February 5 (the day must be 1) to execute the command echo "ok" > /root/cron.log .
This is the time range:

1, sub-range 0-59, time range 0-23, day range 0-31, month range 0-12, week 1-7
2, the available format 1-5 represents a range of 1 to 5
3, usable format of 1 or 2 or 3
4, the available format */2 represents the number divisible by 2, such as the hour, that is, every 2 hours

crontab -eThis file is actually opened /var/spool/cron/username (/var/spool/cron/root is opened if it is root). The Vim editor is used, so if you want to save it, enter it in command mode: Wq. However, you must not directly edit the file, because it may be wrong, so be sure to use crontab -e to edit.
2. View the Scheduled tasks:

[[email protected] ~]# crontab -l01 10 05 02 1 echo "OK" > /root/cron.log

3, to ensure the start of Crond services:

[[email protected] ~]# ps aux | grep crond     //查看crond启动状态root        535  0.0  0.0 126264  1640 ?        Ss   1月25   0:01 /usr/sbin/crond -nroot       9166  0.0  0.0 112676   976 pts/0    R+   16:28   0:00 grep --color=auto cron[[email protected] ~]# systemctl stop crond.service      //停止crond服务[[email protected] ~]# ps aux | grep cronroot       9183  0.0  0.0 112676   972 pts/0    R+   16:29   0:00 grep --color=auto cron[[email protected] ~]# systemctl start crond.service     //开始crond服务[[email protected] ~]# ps aux | grep cronroot       9193  0.0  0.0 126232  1580 ?        Ss   16:29   0:00 /usr/sbin/crond -nroot       9195  0.0  0.0 112676   976 pts/0    S+   16:29   0:00 grep --color=auto cron
Second, Linux system service Management 1, chkconfig service management tools

The Chkconfig command checks and sets the system's various services. This is a program developed by Red Hat in accordance with the GPL rules, which can query which system services the operating system performs at each level of execution, including a variety of resident services. Keep in mind that Chkconfig does not immediately automatically disable or activate a service, it simply changes the symbolic connection.
Chkconfig is mainly used for centos6,linux system all preset services can be viewed through the /etc/init.d/ directory:

Only a few files can be seen, because CentOS7 no longer lasts 6 of service management scenarios, but this command is still available.
Command instance:

 chkconfig --list         //列出所有的服务来及每级别开启状态 chkconfig --level 3 network off      //network服务3级别关闭 chkconfig --level 345 network off     //network服务345级别奢望i关闭 chkconfig --del network    //删除服务 chkconfig --add network   //增加服务

Level code Meaning:

Level 0 means: shutdown
Level 1 means: Single user mode
Level 2: Multi-user command line mode with no network connection
Level 3 means: Multi-user command line mode with network connection
Level 4 means: not available
Level 5 means: Multi-user mode with graphical interface
Level 6 means: reboot

2. SYSTEMD Service Management

The SYSTEMCTL command is a system Service Manager directive that actually combines both the service and Chkconfig commands. All system services are listed first:

systemctl list-units --all --type=service

The corresponding script files for these services are in the /usr/lib/systemd/system/ directory.
Several common service-related commands:

systemctl enable crond.service    //让服务开机启动systemctl disable crond    //不让开机启动systemctl status crond     //查看状态systemctl stop crond       //停止服务systemctl start crond      //启动服务systemctl restart crond   //重启服务systemctl is-enabled crond     //检查服务是否开机启动
1. Unit
ls /usr/lib/systemd/system //系统所有unit

Unit type:

Service: System Services
Target: A group of more than one unit
Device: Hardware Device
Mount: File system mount point
AutoMount: Automatic mount point
Path: File or Path
Scope: External process not initiated by SYSTEMD
Slice: Process Group
Snapshot:systemd Snapshot
Socket: interprocess communication socket
Swap:swap file
Timer: Timer

Each of these types of files is a unit, and it is the unit that makes up the various resources of the system.
Unit-related commands:

systemctl list-units   //列出正在运行的unitsystemctl list-units --all   //列出所有,包括失败的或者inactive的systemctl list-units --all --state=inactive   //列出inactive的unitsystemctl list-units --type=service   //列出状态为active的servicesystemctl is-active crond.service    //查看某个服务是否为active
2. Target

The system uses target to manage the unit for ease of management.
A few commands about target:

systemctl list-unit-files --type=target   //查看所有targetsystemctl list-dependencies multi-usrt.target //查看指定target下面有哪些unitsystemctl get-default //查看系统默认的targetsystemctl set-default multi-user.target   //设置默认target

Relationship between service, unit, and Target:

1. A service belongs to one type of unit
2, multiple unit composed of a target
31 target contains multiple service, you can see the /usr/lib/systemd/system/sshd.service [Install] section, define the target

Linux System Task Scheduler and system service management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.